android studio:帮助 RecyclerView 点击不起作用

我正在尝试从RecyclerViewMain 类中执行单击事件,但它没有捕捉到单击。


界面


public interface OnItemClickListener {

    void onItemClick(int posicao);

    void onItemLongClick(int posicao);

}

接口属性和设置器


private OnItemClickListener mlistener;


public void setOnItemClickListener(OnItemClickListener listener){

    mlistener = listener;

}

类 ViewHolder


public class ViewHolderArtista extends RecyclerView.ViewHolder{


    public ViewHolderArtista(@NonNull View itemView,  final OnItemClickListener listener) {

        super(itemView);


        itemView.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                if(listener != null){

                    int position = getAdapterPosition();

                    if(position != RecyclerView.NO_POSITION){

                        listener.onItemClick(position);

                    }

                }

            }

        });


        itemView.setOnLongClickListener(new View.OnLongClickListener() {

            @Override

            public boolean onLongClick(View v) {

                if (listener != null){

                    int position = getAdapterPosition();

                    if (position != RecyclerView.NO_POSITION){

                        listener.onItemLongClick(position);

                    }

                }

                return true;

            }

        });

    }


}

在主类中实现:


artistaAdapter.setOnItemClickListener(new OnItemClickListener() {

    @Override

    public void onItemClick(int posicao) {

        Log.d("TESTE", "CLIQUE CURTO" +posicao);

    }


    @Override

    public void onItemLongClick(int posicao) {

        Log.d("TESTE", "CLIQUE LONGO" +posicao);

    }

});

但是,单击时没有任何反应:

https://i.stack.imgur.com/oWppR.png

也就是我在主类中实现了RecyclerView的点击事件,但是它不起作用,它没有捕捉到点击项的位置。



慕尼黑的夜晚无繁华
浏览 273回答 3
3回答

喵喔喔

不要在构造中 setOnClickListener,而是覆盖 onBindViewHolder 函数,在其中 setOnClickListener

慕森王

不要在 ViewHOLDER 类中使用点击监听器它总是返回 null 因为它不会初始化。在 recyclerview Adapter 的 onBindViewHolder() 方法中使用点击监听器。

浮云间

您可以按照其他人所说的将侦听器设置为onBindViewHolder,或者我有一个用于recyclerView操作的类onClick和onLongClick。import android.support.v7.widget.RecyclerView;import android.view.View;public class ItemClickSupport {&nbsp; &nbsp; private final RecyclerView mRecyclerView;&nbsp; &nbsp; private OnItemClickListener mOnItemClickListener;&nbsp; &nbsp; private OnItemLongClickListener mOnItemLongClickListener;&nbsp; &nbsp; private View.OnClickListener mOnClickListener = new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mOnItemClickListener != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ask the RecyclerView for the viewHolder of this view.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // then use it to get the position for the adapter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean onLongClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mOnItemLongClickListener != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; private RecyclerView.OnChildAttachStateChangeListener mAttachListener&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = new RecyclerView.OnChildAttachStateChangeListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onChildViewAttachedToWindow(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // every time a new child view is attached add click listeners to it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mOnItemClickListener != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.setOnClickListener(mOnClickListener);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mOnItemLongClickListener != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.setOnLongClickListener(mOnLongClickListener);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onChildViewDetachedFromWindow(View view) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; private ItemClickSupport(RecyclerView recyclerView) {&nbsp; &nbsp; &nbsp; &nbsp; mRecyclerView = recyclerView;&nbsp; &nbsp; &nbsp; &nbsp; // the ID must be declared in XML, used to avoid&nbsp; &nbsp; &nbsp; &nbsp; // replacing the ItemClickSupport without removing&nbsp; &nbsp; &nbsp; &nbsp; // the old one from the RecyclerView&nbsp; &nbsp; &nbsp; &nbsp; mRecyclerView.setTag(R.id.item_click_support, this);&nbsp; &nbsp; &nbsp; &nbsp; mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);&nbsp; &nbsp; }&nbsp; &nbsp; public static ItemClickSupport addTo(RecyclerView view) {&nbsp; &nbsp; &nbsp; &nbsp; // if there's already an ItemClickSupport attached&nbsp; &nbsp; &nbsp; &nbsp; // to this RecyclerView do not replace it, use it&nbsp; &nbsp; &nbsp; &nbsp; ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);&nbsp; &nbsp; &nbsp; &nbsp; if (support == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; support = new ItemClickSupport(view);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return support;&nbsp; &nbsp; }&nbsp; &nbsp; public static ItemClickSupport removeFrom(RecyclerView view) {&nbsp; &nbsp; &nbsp; &nbsp; ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);&nbsp; &nbsp; &nbsp; &nbsp; if (support != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; support.detach(view);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return support;&nbsp; &nbsp; }&nbsp; &nbsp; public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {&nbsp; &nbsp; &nbsp; &nbsp; mOnItemClickListener = listener;&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp; public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {&nbsp; &nbsp; &nbsp; &nbsp; mOnItemLongClickListener = listener;&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp; private void detach(RecyclerView view) {&nbsp; &nbsp; &nbsp; &nbsp; view.removeOnChildAttachStateChangeListener(mAttachListener);&nbsp; &nbsp; &nbsp; &nbsp; view.setTag(R.id.item_click_support, null);&nbsp; &nbsp; }&nbsp; &nbsp; public interface OnItemClickListener {&nbsp; &nbsp; &nbsp; &nbsp; void onItemClicked(RecyclerView recyclerView, int position, View v);&nbsp; &nbsp; }&nbsp; &nbsp; public interface OnItemLongClickListener {&nbsp; &nbsp; &nbsp; &nbsp; boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);&nbsp; &nbsp; }}&nbsp;&nbsp;不要忘记导入<package>.R
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java