如何点击Activity中的recyclerview项目? [复制]
Posted
技术标签:
【中文标题】如何点击Activity中的recyclerview项目? [复制]【英文标题】:How to click recyclerview items in Activity? [duplicate] 【发布时间】:2018-07-16 12:03:42 【问题描述】:我使用 recyclerview 创建了图像的水平列表视图,当我单击特定图像时,我想用我的活动图像更改背景图像,但我遇到了问题,我如何设置 onItem click对于我的 mainactivity 中的 recyclerview 项目?
【问题讨论】:
【参考方案1】:也许它会对你有所帮助,但我确实在我的代码中这样做了:
RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(context, recyclerView ,new RecyclerItemClickListener.OnItemClickListener()
@Override public void onItemClick(View view, int position)
// do whatever
@Override public void onLongItemClick(View view, int position)
// do whatever
)
);
RecyclerItemClickListener
实现:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener
private OnItemClickListener mListener;
public interface OnItemClickListener
public void onItemClick(View view, int position);
public void onLongItemClick(View view, int position);
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener)
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener()
@Override
public boolean onSingleTapUp(MotionEvent e)
return true;
@Override
public void onLongPress(MotionEvent e)
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && mListener != null)
mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
);
@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e)
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e))
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
return false;
@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent)
@Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept)
【讨论】:
【参考方案2】: public class MyRecycleViewClickListener implements RecyclerView.OnItemTouchListener
private OnItemClickListener mListener;
public interface OnItemClickListener
public void onItemClick(View view, int position);
GestureDetector mGestureDetector;
public MyRecycleViewClickListener(Context context, OnItemClickListener listener)
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener()
@Override
public boolean onSingleTapUp(MotionEvent e)
return true;
);
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e)
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e))
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return false;
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent)
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept)
如下使用
mRecyclerView.addOnItemTouchListener(new MyRecycleViewClickListener(this, new MyRecycleViewClickListener.OnItemClickListener()
@Override
public void onItemClick(View view, int position)
));
【讨论】:
谢谢朋友,解决了我的问题【参考方案3】:您可以让您的适配器从此适配器扩展:
/**
* Enables subclasses to add itself as an OnClickListener when binding. This enables the registered
* listener of type @link OnItemClickListener to receive an onItemClick callback.
*/
public abstract class ItemClickRecyclerViewAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH>
implements View.OnClickListener
/**
* A similar interface to that of ListView lore.
*/
public interface OnItemClickListener
void onItemClick(RecyclerView recyclerView, View view, int position);
protected WeakReference<RecyclerView> mAttachedRecyclerViewReference;
protected OnItemClickListener mOnItemClickListener;
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView)
super.onAttachedToRecyclerView(recyclerView);
mAttachedRecyclerViewReference = new WeakReference<>(recyclerView);
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView)
super.onDetachedFromRecyclerView(recyclerView);
mAttachedRecyclerViewReference = null;
@CallSuper
@Override
public void onBindViewHolder(VH holder, int position)
holder.itemView.setOnClickListener(this);
@CallSuper
@Override
public void onViewRecycled(VH holder)
holder.itemView.setOnClickListener(null);
/**
* Provides access to the currently attached recycler view.
*/
public RecyclerView getAttachedRecyclerView()
return mAttachedRecyclerViewReference.get();
/**
* Get the registered item click listener.
*/
public OnItemClickListener getOnItemClickListener()
return mOnItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener)
mOnItemClickListener = onItemClickListener;
@Override
public void onClick(View v)
if (mAttachedRecyclerViewReference != null && mAttachedRecyclerViewReference.get() != null && mOnItemClickListener != null)
mOnItemClickListener.onItemClick(mAttachedRecyclerViewReference.get(), v, mAttachedRecyclerViewReference.get().getChildAdapterPosition(v));
并像这样使用它:
MyAdapter adapter = new MyAdapter();
adapter.setOnItemClickListener(this::itemClicked);
private void itemClicked(RecyclerView recyclerView, View view, int position)
//Handle item click here
【讨论】:
以上是关于如何点击Activity中的recyclerview项目? [复制]的主要内容,如果未能解决你的问题,请参考以下文章