Recyclerview的使用

recyclerview是Google Android 5.0推出的新控件,所以导入support-v7包要是5.0以上的

implementation 'com.android.support:appcompat-v7:26.1.0'

或者直接导入

compile 'com.android.support:recyclerview-v7:21.0.0'

还有一种方式就是导入recyclerview-v7-21.0.0.jar
进入Android SDK的路径
D:\Users\Users\AppData\Local\Android\sdk\extras\android\m2repository\com\android\support\recyclerview-v7\21.0.0
找到recyclerview-v7-21.0.0.aar, 复制一份,然后修改后缀为recyclerview-v7-21.0.0 - 副本.aar.rar ,然后解压该.rar压缩包,找到其中的classes.jar 文件名为:recyclerview-v7-21.0.0.jar

然后就可以开始使用Recyclerview了:
activity_main.xml




    


新建Adapter
RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter{

    private Context context;
    private ArrayList datas;

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivIcon;
        TextView tvTitle;

        public ViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.id_num);
        }
    }

    public RecyclerViewAdapter(Context context, ArrayList datas) {
        this.context = context;
        this.datas = datas;
    }

    /**
     * 相当于getView方法中View和ViewHolder
     * @param viewGroup
     * @param i
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = View.inflate(context, R.layout.item_home, null);
        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        itemView.setLayoutParams(lp);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
    //根据position得到对应的数据
        String data = datas.get(i);
        viewHolder.tvTitle.setText(data);
    }

    /**
     * 总条数
     * @return
     */
    @Override
    public int getItemCount() {
        return datas.size();
    }
}

item_home布局
····





    
    




MainActivity使用代码

mRecyclerView = (RecyclerView) findViewById(R.id.my_recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
initData();
mRecyclerView.setAdapter(new RecyclerViewAdapter(this,titles));

这里需要提一下怎么添加分割线,方法addItemDecoration();

mRecyclerView.addItemDecoration(new SimplePaddingDecoration(this));

SimplePaddingDecoration代码:

public class SimplePaddingDecoration extends RecyclerView.ItemDecoration{
private int dividerHeight;
    private Paint dividerPaint;

    public SimplePaddingDecoration(Context context) {
        dividerPaint = new Paint();
        dividerPaint.setColor(context.getResources().getColor(R.color.colorPrimaryDark));
        dividerHeight = context.getResources().getDimensionPixelSize(R.dimen.divider_height);
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.bottom = dividerHeight;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int childCount = parent.getChildCount();
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        for (int i = 0; i < childCount - 1; i++) {
            View view = parent.getChildAt(i);
            float top = view.getBottom();
            float bottom = view.getBottom() + dividerHeight;
            c.drawRect(left, top, right, bottom, dividerPaint);
        }
    }
}

最终效果图
myrecyclerview.png

代码下载路径:https://github.com/lllllliudahong/MyRecyclerView

最近发现了BRVAH框架,推荐一下
https://github.com/CymChad/BaseRecyclerViewAdapterHelper

你可能感兴趣的:(Recyclerview的使用)