1

我正在使用这个库来实现一个可扩展的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);
    }
}
4

1 回答 1

0

我在这里做了一些猜测,因为我目前无法使用您的设置进行测试项目。

您不需要ExpandableRecyclerViewAdapter像@Selvin 建议的那样“修改”;就像您已经对GenresSongAdapter. 只需添加到这个类。我的方法需要两个步骤(但事实证明,这些步骤可以在一个方法中完成):

  1. 禁用内置的单击时展开行为并
  2. 将一个OnGroupClickListener(其onGroupClick回调封装了该行为)与ImageViews.

我们通过不首先将它们设置为视图持有者来禁用OnGroupClickListener适配器的 s(就像在父类中所做的那样)。因此,我们需要覆盖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设置的 s GroupViewHolder,可能只需SongDetailsViewHolder直接从RecyclerView.ViewHolder.

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

于 2018-01-16T10:26:37.053 回答