为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕尼黑的夜晚无繁华 2022-07-20 21:00:02
我正在尝试从RecyclerViewMain 类中执行单击事件,但它没有捕捉到单击。界面public interface OnItemClickListener {    void onItemClick(int posicao);    void onItemLongClick(int posicao);}接口属性和设置器private OnItemClickListener mlistener;public void setOnItemClickListener(OnItemClickListener listener){    mlistener = listener;}类 ViewHolderpublic 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的点击事件,但是它不起作用,它没有捕捉到点击项的位置。
查看完整描述

3 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

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



查看完整回答
反对 回复 2022-07-20
?
慕森王

TA贡献1777条经验 获得超3个赞

不要在 ViewHOLDER 类中使用点击监听器

它总是返回 null 因为它不会初始化。

在 recyclerview Adapter 的 onBindViewHolder() 方法中使用点击监听器。


查看完整回答
反对 回复 2022-07-20
?
浮云间

TA贡献1829条经验 获得超4个赞

您可以按照其他人所说的将侦听器设置为onBindViewHolder,或者我有一个用于recyclerView操作的类onClick和onLongClick。


import android.support.v7.widget.RecyclerView;

import android.view.View;


public class ItemClickSupport {

    private final RecyclerView mRecyclerView;

    private OnItemClickListener mOnItemClickListener;

    private OnItemLongClickListener mOnItemLongClickListener;

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            if (mOnItemClickListener != null) {

                // ask the RecyclerView for the viewHolder of this view.

                // then use it to get the position for the adapter

                RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);

                mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);

            }

        }

    };

    private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {

        @Override

        public boolean onLongClick(View v) {

            if (mOnItemLongClickListener != null) {

                RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);

                return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);

            }

            return false;

        }

    };

    private RecyclerView.OnChildAttachStateChangeListener mAttachListener

            = new RecyclerView.OnChildAttachStateChangeListener() {

        @Override

        public void onChildViewAttachedToWindow(View view) {

            // every time a new child view is attached add click listeners to it

            if (mOnItemClickListener != null) {

                view.setOnClickListener(mOnClickListener);

            }

            if (mOnItemLongClickListener != null) {

                view.setOnLongClickListener(mOnLongClickListener);

            }

        }


        @Override

        public void onChildViewDetachedFromWindow(View view) {


        }

    };


    private ItemClickSupport(RecyclerView recyclerView) {

        mRecyclerView = recyclerView;

        // the ID must be declared in XML, used to avoid

        // replacing the ItemClickSupport without removing

        // the old one from the RecyclerView

        mRecyclerView.setTag(R.id.item_click_support, this);

        mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);

    }


    public static ItemClickSupport addTo(RecyclerView view) {

        // if there's already an ItemClickSupport attached

        // to this RecyclerView do not replace it, use it

        ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);

        if (support == null) {

            support = new ItemClickSupport(view);

        }

        return support;

    }


    public static ItemClickSupport removeFrom(RecyclerView view) {

        ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);

        if (support != null) {

            support.detach(view);

        }

        return support;

    }


    public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {

        mOnItemClickListener = listener;

        return this;

    }


    public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {

        mOnItemLongClickListener = listener;

        return this;

    }


    private void detach(RecyclerView view) {

        view.removeOnChildAttachStateChangeListener(mAttachListener);

        view.setTag(R.id.item_click_support, null);

    }


    public interface OnItemClickListener {


        void onItemClicked(RecyclerView recyclerView, int position, View v);

    }


    public interface OnItemLongClickListener {


        boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);

    }

}  

不要忘记导入<package>.R


查看完整回答
反对 回复 2022-07-20
  • 3 回答
  • 0 关注
  • 272 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信