封装RecyclerView.ViewHolder

来源:互联网 发布:java 网校系统 编辑:程序博客网 时间:2024/05/22 15:49

最近开始研究RecyclerView发现还有很多地方可以优化,方便开发。根据以前对ListView的ViewHolder优化过程准备对RecyclerView的ViewHolder进行优化。

目标

  • 消灭findViewById
  • 自动数据绑定,将JavaBean的数据自动绑定到相应字段

思路

通过反射遍历ViewHolder的字段,根据字段查找对应的Id。利用findViewById找到相应的View设置给ViewHolder的字段。

要求

ViewHoler的字段名必须和布局文件中的id名一致,否则查找失败。其实可以通过注解实现字段名与id名不一致,但是会破坏程序的可读性。

/** * Created by zhuguohui on 2016/3/25. */public class TRSViewHolder extends RecyclerView.ViewHolder {    public TRSViewHolder(View itemView) {        super(itemView);        setViewToFiled();    }    private void setViewToFiled() {        Field[] fields = getClass().getFields();        for(Field f:fields){            setFiled(f);        }    }    private void setFiled(Field f) {        try{        //通过Resources的getIdentifier方法,可以根据名字查找对应的id,如果id为0则表示没有找到。            int id = itemView.getContext().getResources().getIdentifier(f.getName(), "id", itemView.getContext().getPackageName());            if(id!=0) {                View view = itemView.findViewById(id);                f.set(this, view);            }        }catch (Exception e){          Log.e(getClass().getSimpleName(),"初始化view失败:"+e.getMessage());        }    }}

使用

自定义的holer继承自TRSViewHolder

     public static class NewsViewHolder extends TRSViewHolder{        public TextView title;        public TextView time;        public ImageView image;        public NewsViewHolder(View itemView) {            super(itemView);        }    }

布局文件

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/card_view"    android:layout_width="match_parent"    android:layout_height="130dp"    android:layout_gravity="center"    app:cardCornerRadius="4dp"    app:cardElevation="5dp"    app:cardPreventCornerOverlap="true"    app:cardUseCompatPadding="true">    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@+id/item_news_bg"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@android:color/white"        android:orientation="horizontal"        android:padding="10dp">        <ImageView            android:id="@+id/image"            android:layout_width="0dp"            android:layout_height="fill_parent"            android:layout_gravity="top"            android:layout_marginRight="10dp"            android:layout_weight="5"            android:scaleType="centerCrop"            android:src="@drawable/default_pic" />        <RelativeLayout            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="10"            android:orientation="vertical"            android:paddingRight="10dp">            <TextView                android:id="@+id/title"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:maxLines="2"                android:tag="trs_text_color_black"                android:text="这是标题这是标题这是标题这是标题这是标题这是标题这是标题这是标题这是标题"                android:textColor="@color/primary_text" />            <TextView                android:id="@+id/txt_news_summary"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="3dp"                android:tag="trs_text_color_grey"                android:visibility="gone" />            <TextView                android:id="@+id/time"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_alignParentBottom="true"                android:layout_marginTop="3dp"                android:tag="trs_text_color_grey"                android:text="2016-02-01"                android:textColor="@color/primary" />        </RelativeLayout>    </LinearLayout></android.support.v7.widget.CardView>

效果

部分没有图片的是因为服务没有图片数据,关于数据绑定将在下一篇博客中介绍

0 0