封装RecyclerView.ViewHolder

最近开始研究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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值