bignerdranch / expandable-recycler-view

[DEPRECATED]
MIT License
1.21k stars 290 forks source link

How to handle onClick on Child item #357

Open alexleonald opened 7 years ago

alexleonald commented 7 years ago

the method getChildAdapterPosition in ChildViewHolder seems useless since i cannot get the child item inside a fragment or activity. Can you help me please?

ronniemx commented 7 years ago

We need to create an Interface to listen child clicks:

In your Adapter:

public void setChildClickListener(OnChildClickListener clickListener) {
    this.childClickListener = clickListener;
}

public interface OnChildClickListener {
    /**
     * Callback method to be invoked when a child in this expandable list has
     * been clicked.
     *
     * @param v The view within the expandable list/ListView that was clicked
     * @param groupPosition The group position that contains the child that
     *        was clicked
     * @param childPosition The child position within the group
     * @param id The row id of the child that was clicked
     * @return True if the click was handled
     */
    boolean onChildClick(View v, int groupPosition, int childPosition, long id);
}

In your ChildViewHolder Constructor:

itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(childClickListener != null) {
                childClickListener.onChildClick(itemView, getParentAdapterPosition(), getChildAdapterPosition(), 0);
            }
        }
    });

And finally, in your Activity or Fragment:

adapter.setChildClickListener(new ExpRecyclerAdapter.OnChildClickListener() {
        @Override
        public boolean onChildClick(View v, int groupPosition, int childPosition, long id) {
            Log.d("AdapterChildClick", "position " + groupPosition + "," + childPosition );

            return true;
        }
});

Hope this can help you!

alexleonald commented 7 years ago

I put MyChildViewHolder and MyParentViewHolder classes inside MyExpandableRecyclerAdapter class. That solve as my problem. They were initially separated. Thank you!

kckunal2612 commented 7 years ago

How do I call these 2 methods inside my adapter class ?

getParentAdapterPosition() 
getChildAdapterPosition()
aaryan1 commented 6 years ago

child item click could not be implemented as explained by ronnie .

orions014 commented 4 years ago

`public class TrainBwStnAdapater extends RecyclerView.Adapter {

//region variables
private Context mContext;
private List<TrainDetail> mTrainDetails;
public DetailsAdapterListener onClickListener;
//endregion

//region viewholder class
public class TrainStnViewHolder extends RecyclerView.ViewHolder  {
//using ButterKnife for Binding
//other fields removed for simplicity
    @BindView(R.id.bw_trnNo)
    TextView mTrainNo;
    @BindView(R.id.class_btn)
    AppCompatButton mClassBtn;
    @BindView(R.id.days_btn)
    AppCompatButton mDaysBtn;

    public TrainStnViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);

        mClassBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.classOnClick(v, getAdapterPosition());
            }
        });
        mDaysBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.daysOnClick(v, getAdapterPosition());
            }
        });
    }
}
//endregion

//region constructor
public TrainBwStnAdapater(Context context, List<TrainDetail> mTrainDetails, 
                          DetailsAdapterListener listener) {
    this.mContext = context;
    this.mTrainDetails = mTrainDetails;
    this.onClickListener = listener;
}
//endregion

//region overrides
@Override
public TrainBwStnAdapater.TrainStnViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.train_bw_stn_card, parent, false);

    return new TrainStnViewHolder(itemView);
}

@Override
public void onBindViewHolder(TrainBwStnAdapater.TrainStnViewHolder holder, final int position) {
    TrainDetail trainDetail = mTrainDetails.get(position);
    holder.mTrainNo.setText(String.valueOf(trainDetail.getNumber()));
    //other fields removed for simplicity
}

@Override
public int getItemCount() {
    return mTrainDetails.size();
}
//endregion

//region Interface Details listener
public interface DetailsAdapterListener {

    void classOnClick(View v, int position);

    void daysOnClick(View v, int position);
}
//endregion

}`

`mTrainDetails = getTrain(); mAdapter = new TrainBwStnAdapater(getBaseContext(), mTrainDetails, new TrainBwStnAdapater.DetailsAdapterListener() { @Override public void classOnClick(View v, int position) { showClass(mTrainDetails, position);// do something or navigate to detailed classes }

@Override
public void daysOnClick(View v, int position) {
    showDays(mTrainDetails, position);// do something or navigate to running days
}

}); recyclerView.setAdapter(mAdapter); mAdapter.notifyDataSetChanged();`

https://www.codeproject.com/Tips/1229751/Handle-Click-Events-of-Multiple-Buttons-Inside-a