文章转自 冥萧阳大神 原作地址http://blog.csdn.net/guxiao1201/article/details/40423361
RecyclerView不再负责Item视图的布局及显示,所以RecyclerView也没有为Item开放OnItemClick等点击事件,这就需要开发者自己实现。博客最下面有Demo程序运行动画。
奉上Demo的Github链接。
在调研过程中,发现有同学修改RecyclerView源码来实现Item的点击监听,但认为这不是一个优雅的解决方案,最终决定在RecyclerView.ViewHolder上做文章。
思路是:因为ViewHolder我们可以拿到每个Item的根布局,所以如果我们为根布局设置单独的OnClick监听并将其开放给Adapter,那不就可以在组装RecyclerView时就能够设置ItemClickListener,只不过这个Listener不是设置到RecyclerView上而是设置到Adapter。
我们首先看ViewHolder的代码:
- public class MyViewHolder extends ViewHolder implements OnClickListener,OnLongClickListener{
-
- public ImageView iv;
- public TextView tv;
- private MyItemClickListener mListener;
- private MyItemLongClickListener mLongClickListener;
-
- public MyViewHolder(View rootView,MyItemClickListener listener,MyItemLongClickListener longClickListener) {
- super(rootView);
- iv = (ImageView)rootView.findViewById(R.id.item_iv);
- tv = (TextView)rootView.findViewById(R.id.item_tv);
- this.mListener = listener;
- this.mLongClickListener = longClickListener;
- rootView.setOnClickListener(this);
- rootView.setOnLongClickListener(this);
- }
-
-
-
-
- @Override
- public void onClick(View v) {
- if(mListener != null){
- mListener.onItemClick(v,getPosition());
- }
- }
-
-
-
-
- @Override
- public boolean onLongClick(View arg0) {
- if(mLongClickListener != null){
- mLongClickListener.onItemLongClick(arg0, getPosition());
- }
- return true;
- }
-
- }</span>
因为在构造ViewHolder时,rootView将作为一个必传参数传递进来,所以我们只需要拿到rootView并给其绑定点击监听事件即可。
下面要考虑的就是怎样把listener传递进来。Demo中设定了监听点击事件的Interface:MyItemClickListener:
- public interface MyItemClickListener {
- public void onItemClick(View view,int postion);
- }
MyItemClickListener模仿ListView的OnItemClickListener,开放了view和position两个参数,这对习惯使用ListView的开发者们使用起来更得心应手。从ViewHolder的代码中可以看到,执行onClick方法时会调用getPosition()将当前Item的位置回调给listener。getPosition()是ViewHolder的内置方法,可直接使用。
上面提到过,listener是设定到Adapter上的,所以Adapter就需要对外开放相关方法:
- @Override
- public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent,false);
- MyViewHolder vh = new MyViewHolder(itemView,mItemClickListener,mItemLongClickListener);
- return vh;
- }
-
-
-
-
-
- public void setOnItemClickListener(MyItemClickListener listener){
- this.mItemClickListener = listener;
- }
-
- public void setOnItemLongClickListener(MyItemLongClickListener listener){
- this.mItemLongClickListener = listener;
- }
上篇博客(Android-RecylerView初识)提到过,Adapter的onCreateViewHolder是负责实例化每个Item的视图,所以我在实例化视图时就将listener传递给ViewHolder。
最后就是组装RecyclerView时根据需求设定点击监听了:
-
-
-
- private void initView(){
- mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
- MyLayoutManager manager = new MyLayoutManager(this);
- manager.setOrientation(LinearLayout.HORIZONTAL);
- mRecyclerView.setLayoutManager(manager);
- mRecyclerView.setItemAnimator(new DefaultItemAnimator());
- }
-
- private void initData(){
- this.mData = new ArrayList<MyItemBean>();
- for(int i=0;i<20;i++){
- MyItemBean bean = new MyItemBean();
- bean.tv = "Xmy"+i;
- mData.add(bean);
- }
- this.mAdapter = new MyAdapter(mData);
- this.mRecyclerView.setAdapter(mAdapter);
- RecyclerView.ItemDecoration decoration = new MyDecoration(this);
- this.mRecyclerView.addItemDecoration(decoration);
- this.mAdapter.setOnItemClickListener(this);
- this.mAdapter.setOnItemLongClickListener(this);
- }
Demo为ViewHolder设置了OnClick和OnLongClickListener,在Activity中我们实现了接口方法并在里面打印Toast提示:
- @Override
- public void onItemClick(View view, int postion) {
- MyItemBean bean = mData.get(postion);
- if(bean != null){
- Toast.makeText(this, bean.tv, Toast.LENGTH_SHORT).show();
- }
- }
-
- @Override
- public void onItemLongClick(View view, int postion) {
- MyItemBean bean = mData.get(postion);
- if(bean != null){
- Toast.makeText(this, "LongClick "+bean.tv, Toast.LENGTH_SHORT).show();
- }
- }