RecyclerView添加点击和长按事件的简单实现

这里写的方式其实是通过给RecylerView的item添加onClickListene和onLongClickListener的方法来达到我们的要求。

不过貌似还有一种比这更高效的实现方法。末尾附上其地址。与大家共同学习

代码比较简单,相信对于需要这个小需求的人来说一看就懂。
不过要使用recylerview不要忘了在module的build中添加关联

compile ‘com.android.support:design:24.2.0’

照旧贴上代码。
MianActivity的代码:

public class MainActivity extends AppCompatActivity {

    private MyRecyclerViewAdapter mMyRecyclerViewAdapter;
    private RecyclerView mRecyclerView;
    private ArrayList<String> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        initData();
        mMyRecyclerViewAdapter = new MyRecyclerViewAdapter(this, mList);
        LinearLayoutManager linear = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linear);
        mRecyclerView.setAdapter(mMyRecyclerViewAdapter);

        //给item设置点击和长按事件
        mMyRecyclerViewAdapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MainActivity.this, position+"被点了", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(MainActivity.this, +position+"被长按了", Toast.LENGTH_SHORT).show();
            }
        });

    }
    private void initData() {
        mList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            mList.add(i+"");
        }
    }
}

那个。。。以上代码说哪一句看不懂 你可以站出来,我直播吃翔

接下来就是MyRecyclerViewAdapter了:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyViewHolder> {

    private Context context;
    private List<String> datas;
    private LayoutInflater mLayoutInflater;

    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public MyRecyclerViewAdapter(Context context, List<String> datas) {
        this.context = context;
        this.datas = datas;
        this.mLayoutInflater = LayoutInflater.from(context);

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    //创建一个viewholder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item, parent, false);

        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    //绑定viewholder中的数据
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.tv_result.setText(datas.get(position));
        click(holder);
    }

   //让每个item去实现点击事件
    protected void click(final MyViewHolder holder) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, layoutPosition);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(holder.itemView, layoutPosition);

                    return false;
                }
            });
        }
    }
  
}
//自定义的ViewHolder,含有每一个条目的元素
class MyViewHolder extends RecyclerView.ViewHolder {

    TextView tv_result;

    public MyViewHolder(View itemView) {
        super(itemView);
        tv_result = (TextView) itemView.findViewById(R.id.tv_result);
    }

}

以上就可以给我们recyclerview的item添加上了点击和长按事件。
关键是看懂一下adapter中的代码就可以理解了

附上module的下载地址:
地址

附上一个更优的实现方法:
地址
链接没用可以搜一下:
android开发游记:RecyclerView无法添加onItemClickListener最佳的高效解决方案

你可能感兴趣的:(Android笔记)