天天看点

RecyclerView.Adapter结合ButterKnife封装

public class BaseViewHolder extends RecyclerView.ViewHolder {

    View mView ;
    public BaseViewHolder(View itemView) {
        super(itemView);
        this.mView=itemView;
    }

    public View getView(){
        return mView;
    }
}
           
package animation.sunset.com.animationdome.rcyc;

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


import java.util.List;

/**
 * Created by sunset on 17-5-2.
 */

public abstract class BaseRecycAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    Context mContext;
    List<T> mList;
    private OnItemClickListener mOnItemClickListener;

    public BaseRecycAdapter(List<T> list, Context context) {
        this.mContext = context;
        this.mList = list;
    }

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

               View itemView = LayoutInflater.from(mContext).inflate(getContentView(viewType), parent,false);

        return new BaseViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        holder.getView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //这个地方一定要判断 不然你没有注册点击事件的时候,点击Item的时候会报错
                if(mOnItemClickListener==null){
                    returen;
                }
                mOnItemClickListener.onItemClick(position);
            }
        });
        covert(holder, mList.get(position), position);
    }


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


    protected abstract int getContentView(int viewType);

    protected abstract void covert(BaseViewHolder holder, T data, int position);


    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(int position);
    }


}
           

使用示例

public class RecyclerAdapter extends BaseRecycAdapter<String> {
    @BindView(R.id.rcyc_item_text)
    TextView rcycItemText;

    public RecyclerAdapter(List<String> list, Context context) {
        super(list, context);
    }

    @Override
    protected int getContentView(int viewType) {
        return R.layout.activity_rcyc_item;
    }


    @Override
    protected void covert(BaseViewHolder holder, String data, int position) {
        ButterKnife.bind(this, holder.getView());
        rcycItemText.setText(data);
    }


}
           
public class RcycActivity extends AppCompatActivity {

    List<String> strings;
    @BindView(R.id.rcycview)
    RecyclerView rcycview;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rcyc);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        strings = new ArrayList<>();
        for (int i = ; i < ; i++) {
            strings.add("封装RecyclerView" + i);
        }
        rcycview.setLayoutManager(new LinearLayoutManager(this));
        rcycview.setAdapter(new RecyclerAdapter(strings,this));

    }
}