パッケージRecyclerView.Adapter

22657 ワード

RecyclerView.Adapterを基本パッケージ化し、自動データバインドを実現します.汎用型を用いてデータ型を指定し、インポートレイアウトidを構築する
構想と実現
クラス宣言は次のとおりです.
public class TRSRecyclerAdapter<T, H extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<H> 

コンストラクション関数:
     List<T> data;
     Context context;
    private final LayoutInflater mInflater;
    private int layoutId;

    public TRSRecyclerAdapter(List<T> data, Context context, int layoutId) {
        this.data = data;
        this.layoutId = layoutId;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

onCreateViewHolderメソッドを書き換え、汎用性を反射することで指定したViewHolderを生成します.反射したViewHolderは指定したコンストラクション関数を使用する必要があることに注意してください.
 @Override
    public H onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = mInflater.inflate(layoutId, parent, false);
        return getInstanceOfH(view);
    }

    H getInstanceOfH(View view) {
        //    
        ParameterizedType superClass = (ParameterizedType) getClass().getGenericSuperclass();
        //  ,   1             。
        Class<H> type = (Class<H>) superClass.getActualTypeArguments()[1];
        try {
            //  ViewHolder     
            Constructor<H> constructor = type.getConstructor(View.class);
           //  ViewHolder
            return constructor.newInstance(view);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

onBindViewHolderメソッドを書き換え、このメソッドでデータバインドを行います.
  @Override
    public final void onBindViewHolder(H h, int position) {
        T t = data.get(position);
        bindData(t, h);
    }

bindDataメソッドでは、ViewHolderのフィールドタイプと名前に基づいて、t内の対応するデータが検索され、フィールドタイプがTextViewの場合はsetTextが呼び出され、ImageViewの場合はglideを使用してピクチャがロードされます.この方法を使用する前提は、ViewHolderのフィールド名がJavaBeanのフィールド名と一致することです.
    public void bindData(T t, H h) {
        setDataToHolder(t, h);
    }

     private void setDataToHolder(T t, H h) {
         //  ViewHolder    
        Field[] fields = h.getClass().getDeclaredFields();
        Object o ;
        //    
        for (Field f : fields) {
            f.setAccessible(true);
            o = null;
            try {
                o = f.get(h);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            //      
            if (o instanceof TextView) {
                TextView tv = (TextView) o;
                //     ,  javaBean    ,   javabean     ViewHolder      
                Object value = getValueFromFiled(t, f.getName());
                if (value instanceof CharSequence) {
                    tv.setText((CharSequence) value);
                }
            } else if (o instanceof ImageView) {
                ImageView iv = (ImageView) o;
                Object value = getValueFromFiled(t, f.getName());
                if (value instanceof String) {
                    String url = (String) value;
                    Glide.with(context).load(url).placeholder(R.drawable.default_pic).into(iv);
                }
            }


        }
    }

    public Object getValueFromFiled(T t, String name) {
        try {
            Field hf = t.getClass().getDeclaredField(name);
            hf.setAccessible(true);
            return hf.get(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

完全なコード
:
package trs.com.myapp.adapter;

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

import com.bumptech.glide.Glide;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import trs.com.myapp.R;

/** * Created by zhuguohui on 2016/3/23. */
public class TRSRecyclerAdapter<T, H extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<H> {
    List<T> data;
    Context context;
    private final LayoutInflater mInflater;
    private int layoutId;

    public TRSRecyclerAdapter(List<T> data, Context context, int layoutId) {
        this.data = data;
        this.layoutId = layoutId;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public H onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = mInflater.inflate(layoutId, parent, false);
        return getInstanceOfH(view);
    }

    H getInstanceOfH(View view) {
        ParameterizedType superClass = (ParameterizedType) getClass().getGenericSuperclass();
        Class<H> type = (Class<H>) superClass.getActualTypeArguments()[1];
        try {
            Constructor<H> constructor = type.getConstructor(View.class);
            return constructor.newInstance(view);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public final void onBindViewHolder(H h, int position) {
        T t = data.get(position);
        bindData(t, h);
    }

    private void setDataToHolder(T t, H h) {
        Field[] fields = h.getClass().getDeclaredFields();
        Object o ;
        for (Field f : fields) {
            f.setAccessible(true);
            o = null;
            try {
                o = f.get(h);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            if (o instanceof TextView) {
                TextView tv = (TextView) o;
                Object value = getValueFromFiled(t, f.getName());
                if (value instanceof CharSequence) {
                    tv.setText((CharSequence) value);
                }
            } else if (o instanceof ImageView) {
                ImageView iv = (ImageView) o;
                Object value = getValueFromFiled(t, f.getName());
                if (value instanceof String) {
                    String url = (String) value;
                    Glide.with(context).load(url).placeholder(R.drawable.default_pic).into(iv);
                }
            }


        }
    }

    public Object getValueFromFiled(T t, String name) {
        try {
            Field hf = t.getClass().getDeclaredField(name);
            hf.setAccessible(true);
            return hf.get(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void bindData(T t, H h) {
        setDataToHolder(t, h);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

}

使用
1.javaBean
package trs.com.myapp.bean;

/** * Created by zhuguohui on 2016/3/23. */
public class NewsItem {


    private String title;
    private String image;
    private String time;
    private String url;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

2.Adapter
package trs.com.myapp.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import trs.com.myapp.R;
import trs.com.myapp.bean.NewsItem;
import trs.com.myapp.holder.TRSViewHolder;

/** * Created by yuelin on 2016/3/23. */
public class NewsAdapter extends TRSRecyclerAdapter<NewsItem,NewsAdapter.NewsViewHolder> {


    public NewsAdapter(List<NewsItem> data, Context context) {
        super(data, context, R.layout.item_news_layout);
    }

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

3.レイアウトファイル
<?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>

効果
まとめ
RecyclerView.Adapterのパッケージを通じて、汎用、反射などをより深く理解しました.使用時の注意:自動的にデータを埋め込み、ビュー機能を自動的にバインドするには、ViewHolderのフィールド名、javabeanのフィールド名、レイアウトファイルのidと同じようにする必要があります.今のところ柔軟性に欠けているようですが、後で良い案が解決されるのを待っていましょう.