1

I am using this library to implement an expandable RecyclerView. I have implemented it, and it works fine. But what I want is the group should only expand if I click a particular view. Right now what is happening is if I click anywhere on the RecyclerView item, the group expands. I have an ImageView, and when I click that ImageView, only then the group should expand. How can I implement this?

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);
    }
}
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Rektirino
  • 582
  • 5
  • 24
  • feel free to modify ExpandableRecyclerViewAdapter (whatever it is) source to fulfill your needs ... eventually ask library's author to do this – Selvin Jan 15 '18 at 13:25

1 Answers1

0

I am doing a bit of guesswork here since I can't afford to make a test project with your setup at the moment.

You don't need to "modify" ExpandableRecyclerViewAdapter as @Selvin is suggesting; it suffice to overwrite the class as you already did with GenresSongAdapter. Simply add to this class. My approach would entail two steps (but it turns out that these steps can be taken in a single method):

  1. Disable the built-in expand-on-click behaviour and
  2. Connect an OnGroupClickListener (whose onGroupClick callback encapsulates that behaviour) with the ImageViews.

We disable the OnGroupClickListeners of the adapter by not setting them to the view holders in the first place (as it is done in the parent class). Therefore we need to overwrite the onCreateViewHolder() method of the adapter and simply skip the command by which the listener is set. However, in the same method we can get a view holder, including all its members. Therefore we can attach a listener to each view holders' 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");
    }
}

You will also have to evade the OnClickListeners set by the GroupViewHolder class, possibly by simply extending SongDetailsViewHolder directly from RecyclerView.ViewHolder.

This is untested code, but I am pretty sure that at least the idea is correct :)

kalabalik
  • 3,792
  • 2
  • 21
  • 50
  • Hi, thank you for sparing your time for this, actually I solved this by myself. I did pretty much same as you. I disabled the `OnGroupClickListeners ` and add this `toggle(layout)` to my viewHolder. – Rektirino Jan 18 '18 at 10:19