Recyclerview的使用

2018-09-19  本文已影响0人  lllllliudahong

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

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    tools:context="android.my.com.myrecyclerview.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

新建Adapter
RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{

    private Context context;
    private ArrayList<String> 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<String> 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布局
····

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="android.my.com.myrecyclerview.MainActivity"
android:background="#ffffff"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/id_image"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:background="@drawable/ic_launcher_background"/>
    <TextView
        android:id="@+id/id_num"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical"
        android:layout_toRightOf="@id/id_image"
        android:text="1" />
</RelativeLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/colorPrimaryDark"
    android:visibility="gone"
    />
</LinearLayout>

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

上一篇下一篇

猜你喜欢

热点阅读