Does anyone know how to define an onClickListener in RecyclerView? I've thought about how to define onClick for each item in the Recycler View, and this seems too much of a hassle Is there any other way?
java android android-5.0-lollipop recyclerview
RecyclerView provides touchListener instead of clickListener. If you don't mind that, please refer to https://developer.android.com/intl/ko/reference/android/support/v7/widget/RecyclerView.OnItemTouchListener.html)%EC%97%AC%EA%B8%B0%EC%97%90%EC%84%9C here.
All items as you think if you want onclicklistener onclicklistener to the settings you. In
, recyclerview adapterprivate final OnClickListener mOnClickListener = new MyOnClickListener();
@Override
public MyViewHolder onCreateViewHolder(final ViewGroup parent, final int position) {
View view = LayoutInflater.from(mContext).inflate(R.layout.myview, parent, false);
view.setOnClickListener(mOnClickListener);
return new MyViewHolder(view);
}
And the onClick method
@Override
public void onClick(final View view) {
int itemPosition = mRecyclerView.getChildPosition(view);
String item = mList.get(itemPosition);
Toast.makeText(mContext, item, Toast.LENGTH_LONG).show();
}
You can define it like this.
© 2024 OneMinuteCode. All rights reserved.