为RecyclerView打造万能适配器,点击事件,5.0水波纹点击效果
in Android with 0 comment

为RecyclerView打造万能适配器,点击事件,5.0水波纹点击效果

in Android with 0 comment

一、前言

最近使用到 RecyclerView,RecyclerView 使用详解戳这里,由于使用过张鸿洋大神的ListView 万能 Adapter,感觉 RecyclerView 的 Adapter 编写还是太麻烦了,而且没有点击事件,ok,参考 ListView 的万能 Adapter 的思路,写一个 RecyclerView 通用的 Adapter,在加上点击效果( ̄︶ ̄)↗ 涨

二、代码编写

RecyclerAdapter 的编写

package com.example.admin.recyclerviewdemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by 橘子桑 on 2016/1/2.
 */
public abstract class RecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerViewHolder> {

    private Context mContext;
    private List<T> mDatas;
    private int mLayoutId;
    private LayoutInflater mInflater;

    private OnItemClickListener onItemClickListener;

    public RecyclerAdapter(Context mContext, List<T> mDatas, int mLayoutId) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        this.mLayoutId = mLayoutId;
        mInflater = LayoutInflater.from(mContext);
    }
    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //这里是创建ViewHolder的地方,RecyclerAdapter内部已经实现了ViewHolder的重用
        //这里我们直接new就好了
        return new RecyclerViewHolder(mInflater.inflate(mLayoutId, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {

    }

    public abstract void convert(RecyclerViewHolder holder, T data, int position);
    @Override
    public int getItemCount() {
        return mDatas.size();
    }
    /**自定义RecyclerView item的点击事件的点击事件*/
    interface OnItemClickListener {
        void OnItemClickListener(View view, int position);
    }
}

上面参考 ListView 的万能适配器,写的一个抽象类,可以看到 onBindViewHolder 方法里面是空的,我们需要在这里来,加入点击事件和效果。

RecyclerHolder

package com.example.admin.recyclerviewdemo;

import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by 橘子桑 on 2016/1/2.
 */
public class RecycleHolder extends RecyclerView.ViewHolder {

    /** 用于存储当前item当中的View */
    private SparseArray<View> mViews;

    public RecycleHolder(View itemView) {
        super(itemView);
        mViews = new SparseArray<View>();
    }
    public <T extends View> T findView(int ViewId) {
        View view = mViews.get(ViewId);
        //集合中没有,则从item当中获取,并存入集合当中
        if (view == null) {
            view = itemView.findViewById(ViewId);
            mViews.put(ViewId, view);
        }
        return (T) view;
    }
    public RecycleHolder setText(int viewId, String text) {
        TextView tv = findView(viewId);
        tv.setText(text);
        return this;
    }
    public RecycleHolder setText(int viewId, int text) {
        TextView tv = findView(viewId);
        tv.setText(text);
        return this;
    }
    public RecycleHolder setImageResource(int viewId, int ImageId) {
        ImageView image = findView(viewId);
        image.setImageResource(ImageId);
        return this;
    }
    public RecycleHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView image = findView(viewId);
        image.setImageBitmap(bitmap);
        return this;
    }
    public RecycleHolder setImageNet(int viewId, String url) {
        ImageView image = findView(viewId);
        //使用你所用的网络框架等
        return this;
    }
}

可以看的上面代码非常的简单,就是储存了当前 item 中的 View 而已,因为 RecyclerView 内部已经实现了 ViewHolder 的重用<( ̄︶ ̄)>

为 RecyclerView 添加 item 的点击事件

   @Override
    public void onBindViewHolder(final RecycleHolder holder, int position) {
        if (onItemClickListener != null) {
            //设置背景
            holder.itemView.setBackgroundResource(R.drawable.recycler_bg);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //注意,这里的position不要用上面参数中的position,会出现位置错乱
                    onItemClickListener.OnItemClickListener(holder.itemView, holder.getLayoutPosition());
                }
            });
        }
        convert(holder, mDatas.get(position), position);
    }
    public abstract void convert(RecycleHolder holder, T data, int position);

上面就完成了,万能适配器的编写,还有点击事件的添加了

drawable xml 代码

为了让 5.0 以上的系统产生水波纹效果,所以我们新建一个 drawable-v21 目录来存放 drawable 文件
<br/>
20160102193704005.png
<br/>

5.0 以下 drawable 文件

recycler_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape android:shape="rectangle">
            <solid android:color="#cfd8dc"></solid>
        </shape>
    </item>
</selector>

<br/>

5.0 以上 drawable-v21

recycler_rectangle.xml
recycler_bg.xml

<!--点击出现的水波纹效果是矩形的 -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#FFFFFF" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<!-- ripple 是5.0才出现的新标签-->
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#cfd8dc" ><!-- 点击出现的水波纹的颜色 -->
    <item android:drawable="@drawable/recycler_rectangle"/>
</ripple>

三、使用

写个匿名类就 OK 了 ╭(′▽`)╯ 是不是比以前简单好多

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView rv_list = findView(R.id.rv_list);
        rv_list.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rv_list.setAdapter(TextAdapter = new RecyclerAdapter<String>(this, getData(), R.layout.recycler_item) {
            @Override
            public void convert(RecycleHolder holder, String data, int position) {
                holder.setText(R.id.tv, data);
                holder.setImageResource(R.id.image, R.mipmap.ic_launcher);
            }
        });
        TextAdapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
            @Override
            public void OnItemClickListener(View view, int position) {
                ToastShow("点击" + position);

            }
        });
    }

四、效果

5.0 的效果
20160102192402887.gif

代码下载地址 http://download.csdn.net/detail/qq_29262849/9387198

Responses