Android RecyclerView 监听Item短按和长按

Posted 王睿丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android RecyclerView 监听Item短按和长按相关的知识,希望对你有一定的参考价值。

HouseHomeAdapter.java

private OnItemOnClickListener mOnItemOnClickListener;
    public void setOnItemClickListener(OnItemOnClickListener listener)
        this.mOnItemOnClickListener = listener;
    

    public interface OnItemOnClickListener
        void onItemOnClick(View view,int pos);
        void onItemLongOnClick(View view ,int pos);
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, @SuppressLint("RecyclerView") int i) 
        if(mOnItemOnClickListener!=null) 
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View view) 
                    mOnItemOnClickListener.onItemOnClick(viewHolder.itemView, i);
                
            );

            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() 
                @Override
                public boolean onLongClick(View view) 
                    mOnItemOnClickListener.onItemLongOnClick(viewHolder.itemView, i);
                    return false;
                
            );
        
    

MainActivity.java

adapter.setOnItemClickListener(new HouseHomeAdapter.OnItemOnClickListener() 
            @Override
            public void onItemOnClick(View view, int pos) 
                Toast.makeText(getActivity(), "短按第"+pos+"项", Toast.LENGTH_SHORT).show();
            

            @Override
            public void onItemLongOnClick(View view, int pos) 
                Toast.makeText(getActivity(), "长按第"+pos+"项", Toast.LENGTH_SHORT).show();
            
        );

以上是关于Android RecyclerView 监听Item短按和长按的主要内容,如果未能解决你的问题,请参考以下文章

Android Recyclerview 监听滑动状态和滑动距离

Android Recyclerview 监听滑动状态和滑动距离

Android RecyclerView 监听Item短按和长按

Android基础—— 自定义RecyclerView内部控件的监听器

Android基础—— 自定义RecyclerView内部控件的监听器

Android 通过OnScrollListener来监听RecyclerView的位置