RecycleView 作为新出的控件.他规范了 Viewholder 的写法.
但是总感觉适配器的写法过于冗长.怎么办呢?
我们来简化他.
ViewHolder 的实现方式和ListView适配器的超省写法中的ViewHodler 是一样的
ViewHolder.class
- public class ViewHolder {
- private SparseArray<View> viewHolder;
- private View view;
- public static ViewHolder getViewHolder(View view){
- ViewHolder viewHolder = (ViewHolder) view.getTag();
- if (viewHolder == null) {
- viewHolder = new ViewHolder(view);
- view.setTag(viewHolder);
- }
- return viewHolder;
- }
- private ViewHolder(View view) {
- this.view = view;
- viewHolder = new SparseArray<View>();
- view.setTag(viewHolder);
- }
- public <T extends View> T get(int id) {
- View childView = viewHolder.get(id);
- if (childView == null) {
- childView = view.findViewById(id);
- viewHolder.put(id, childView);
- }
- return (T) childView;
- }
- public View getConvertView() {
- return view;
- }
- public TextView getTextView(int id) {
- return get(id);
- }
- public Button getButton(int id) {
- return get(id);
- }
- public ImageView getImageView(int id) {
- return get(id);
- }
- public void setTextView(int id,CharSequence charSequence){
- getTextView(id).setText(charSequence);
- }
- }
我们继承 RecyclerView.Adapter<RVHolder>
AutoRVAdapter.class
- public abstract class AutoRVAdapter extends RecyclerView.Adapter<RVHolder> {
- public List<?> list;
- private Context context;
- public AutoRVAdapter(Context context, List<?> list) {
- this.list = list;
- this.context = context;
- }
- @Override
- public RVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View view = LayoutInflater.from(context).inflate(onCreateViewLayoutID(viewType), null);
- return new RVHolder(view);
- }
- public abstract int onCreateViewLayoutID(int viewType);
- @Override
- public void onViewRecycled(final RVHolder holder) {
- super.onViewRecycled(holder);
- }
- @Override
- public void onBindViewHolder(final RVHolder holder, final int position) {
- onBindViewHolder(holder.getViewHolder(), position);
- if (onItemClickListener != null) {
- holder.itemView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- onItemClickListener.onItemClick(null, v, holder.getPosition(), holder.getItemId());
- }
- });
- }
- }
- public abstract void onBindViewHolder(ViewHolder holder, int position);
- @Override
- public int getItemCount() {
- return list.size();
- }
- private AdapterView.OnItemClickListener onItemClickListener;
- public AdapterView.OnItemClickListener getOnItemClickListener() {
- return onItemClickListener;
- }
- public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
- this.onItemClickListener = onItemClickListener;
- }
- }
RVHolder.class 继承 RecyclerView.ViewHolder
- public class RVHolder extends RecyclerView.ViewHolder {
- private ViewHolder viewHolder;
- public RVHolder(View itemView) {
- super(itemView);
- viewHolder=ViewHolder.getViewHolder(itemView);
- }
- public ViewHolder getViewHolder() {
- return viewHolder;
- }
- }
到此,结束了
我们新写的适配器继承AutoRVAdapter 实现onCreateViewLayoutID 和 onBindViewHolder 方法即可.
onCreateViewLayoutID->返回item 的布局.
onBindViewHolder->绑定数据源.
- public class DemoRVAdapter extends AutoRVAdapter {
- public RecyclerAdapter(Context context, List<?> list) {
- super(context, list);
- }
- @Override
- public int onCreateViewLayoutID(int viewType) {
- return R.layout.item;
- }
- @Override
- public void onBindViewHolder(ViewHolder holder, int position) {
- Entity item=(Entity) list.get(position);
- vh.getTextView(R.id.name).setText(item.getName());
- vh.getTextView(R.id.age).setText(item.getAge());
- vh.setText(R.id.height,item.getHeight());
- }
- }