封装RecyclerView.Adapter

一、定义抽象类

RecyclerView在项目中经常出现,与之一起的是Adapter的实现。在实现Adapter时,有一些步骤重复,繁琐。所以,在实现Adapter前,先进行一些封装,能提高不少的开发效率,用的时候简单,方便。

封装要求:

  1. 布局和数据由外部(构造函数)传入;
  2. Adapter的数据类型使用泛型;
  3. 绑定数据时,无需通过position获取,直接返回泛型对象;
  4. 需类型安全;
  5. 封装点击事件,处理多重点击;
  6. 点击事件中返回泛型,即无需通过position到集合中拿数据,直接返回泛型对象。

封装Adapter类后的完整代码:

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

import java.util.List;

public abstract class CustomBaseAdapter extends RecyclerView.Adapter {

    private List list;

    private int layoutId;

    private Context context;

    private OnItemClickListener onItemClickListener;

    private OnItemLongClickListener onItemLongClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, A a);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View view, A a);
    }

    public CustomBaseAdapter(int layoutId, List list) {
        this.list = list;
        this.layoutId = layoutId;
    }

    public CustomBaseAdapter(Context context, int layoutId, List list) {
        this.list = list;
        this.layoutId = layoutId;
        this.context = context;
    }

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

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public abstract VH onCreateCustomViewHolder(View view);

    public abstract void onBindCustomViewHolder(VH vh, T t);

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
        return onCreateCustomViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        onBindCustomViewHolder(holder, list.get(position));
    }

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

    public abstract class CustomBaseHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        private static final int CLICK_EVENT_TIME = 600;

        private long lastClickTime;

        public CustomBaseHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener != null) {
                long currentTime = System.currentTimeMillis();
                if (currentTime - lastClickTime >= CLICK_EVENT_TIME) {
                    onItemClickListener.onItemClick(v, list.get(getAdapterPosition()));
                }
                lastClickTime = currentTime;
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (onItemLongClickListener != null) {
                onItemLongClickListener.onItemLongClick(v, list.get(getAdapterPosition()));
            }
            return false;
        }
    }

}

二、使用自定义抽象类,定义需求Adapter

  1. xml布局,adapter_item_text



    

  1. 继承抽象类
import android.view.View;
import android.widget.TextView;

import java.util.List;

public class DefaultStringAdapter extends CustomBaseAdapter {

    public DefaultStringAdapter(int layoutResId, List list) {
        super(layoutResId, list);
    }

    @Override
    public DefaultStringHolder onCreateCustomViewHolder(View view) {
        return new DefaultStringHolder(view);
    }

    @Override
    public void onBindCustomViewHolder(DefaultStringHolder defaultStringHolder, String s) {
        defaultStringHolder.textView.setText(s);
    }

    public class DefaultStringHolder extends CustomBaseAdapter.CustomBaseHolder {
        private TextView textView;

        public DefaultStringHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_content);
        }
    }

}

三、使用Adapter

        List list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add("item" + i);
        }

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        DefaultStringAdapter adapter = new DefaultStringAdapter(R.layout.adapter_item_text, list);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new CustomBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, String s) {
                Log.d("111", "onItemClick: " + s);
            }
        });

你可能感兴趣的:(封装RecyclerView.Adapter)