1. 程式人生 > >RecyclerView.Adapter結合ButterKnife封裝

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 ? 0 : 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 = 0; i < 100; i++) {
            strings.add("封裝RecyclerView" + i);
        }
        rcycview.setLayoutManager(new LinearLayoutManager(this));
        rcycview.setAdapter(new RecyclerAdapter(strings,this));

    }
}