在expandable recyclerview中展开/折叠组

时间:2018-01-15 13:22:49

标签: android android-recyclerview expandablerecyclerview

我正在使用this库来实现可扩展的RecyclerView。我实现了它,它工作正常。但我想要的是,如果我点击特定视图,该组应该只展开。现在发生的事情是,如果我点击RecyclerView项目的任何地方,该组将展开。我有ImageView,当我点击ImageView时,只有该组才会展开。我该如何实现呢?

RecyclerViewAdapter.java:

public class GenresSongAdapter extends ExpandableRecyclerViewAdapter<SongDetailsViewHolder, OptionViewHolder> {
    private Context mContext;
    public GenresSongAdapter(Context context, List<? extends ExpandableGroup> groups) {
        super(groups);
        mContext = context;
    }

    @Override
    public SongDetailsViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_song, parent, false);
        return new SongDetailsViewHolder(view);
    }

    @Override
    public OptionViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_artists, parent, false);
        return new OptionViewHolder(view);
    }

    @Override
    public void onBindChildViewHolder(OptionViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
        ArtistsModel artistsModel = (ArtistsModel) group.getItems().get(childIndex);
        holder.ArtistText.setText(artistsModel.getArtistsName());
    }

    @Override
    public void onBindGroupViewHolder(SongDetailsViewHolder holder, int flatPosition, ExpandableGroup songInfo) {
        holder.setSongDetails(mContext, songInfo);
    }
}

SongDetailsViewHolder.java

class SongDetailsViewHolder extends GroupViewHolder {
    public TextView songName;
    public TextView artistName;
    public TextView duration;
    public ImageView iv_artwork;

    public SongDetailsViewHolder(View itemView) {
        super(itemView);
        songName = (TextView) itemView.findViewById(R.id.SongName);
        artistName = (TextView) itemView.findViewById(R.id.ArtistName);
        duration = (TextView) itemView.findViewById(R.id.duration);
        iv_artwork = (ImageView) itemView.findViewById(R.id.iv_artwork);
    }

    public void setSongDetails(Context mContext, ExpandableGroup songInfo) {
        if (songInfo instanceof SongInfo) {
            songName.setText(songInfo.getTitle());
            artistName.setText(((SongInfo) songInfo).getArtistName());
            duration.setText(((SongInfo) songInfo).getSongDuration());
            Picasso.with(mContext).load(((SongInfo) songInfo).getImageURI()).placeholder(R.drawable.ic_launcher).into(iv_artwork);
        }
    }
}

OptionViewHolder.java

class OptionViewHolder extends ChildViewHolder {
    TextView ArtistText;

    public OptionViewHolder(View itemView) {
        super(itemView);
        ArtistText = itemView.findViewById(R.id.ArtistText);
    }
}

1 个答案:

答案 0 :(得分:0)

我在这里做了一些猜测,因为我现在不能用你的设置做一个测试项目。

您不需要修改&#34; {@ 1}}正如@Selvin所暗示的那样;就像你已经使用ExpandableRecyclerViewAdapter那样覆盖类就足够了。只需添加到此课程。我的方法需要两个步骤(但事实证明这些步骤可以采用单一方法):

  1. 禁用内置的展开式点击行为和
  2. GenresSongAdapter(其OnGroupClickListener回调封装该行为)与onGroupClick连接起来。
  3. 我们通过不首先将它们设置为视图持有者来禁用适配器的ImageView(就像在父类中完成的那样)。因此,我们需要覆盖适配器的OnGroupClickListener方法,并简单地跳过设置侦听器的命令。但是,在同一方法中,我们可以获得一个视图持有者,包括其所有成员。因此,我们可以为每个观看者提供一个听众。 onCreateViewHolder()

    ImageView

    您还必须避开@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { switch (viewType) { case ExpandableListPosition.GROUP: SongDetailsViewHolder sdvh = onCreateGroupViewHolder(parent, viewType); sdvh.iv_artwork.setOnGroupClickListener(this); return sdvh; case ExpandableListPosition.CHILD: OptionViewHolder ovh = onCreateChildViewHolder(parent, viewType); return ovh; default: throw new IllegalArgumentException("viewType is not valid"); } } 类设置的OnClickListener,可能只是直接从GroupViewHolder扩展SongDetailsViewHolder

    这是未经测试的代码,但我很确定至少这个想法是正确的:)