RecyclerView 通用 Adapter

先看使用方法 👀

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:java

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
    implementation 'com.github.stefanji:CommRecyclerViewAdapter:2.1'
}

Use Simple

Kotlin:android

listView.adapter = object : CommRecyclerViewAdapter<String>(strings, object : AdapterItem {

            lateinit var textView: TextView
        
            override fun bindViews(p: View) {
                textView = p.findViewById(android.R.id.text1)
            }

            override fun handleData(p: Int) {
                val s = strings[p]
                textView.text = s
            }

            override fun getResId(): Int {
                return android.R.layout.simple_list_item_1
            }
        }) {}

Java:git

CommRecyclerViewAdapter<String> mAdapter = new CommRecyclerViewAdapter<>(strings, new AdapterItem() {
    TextView title;

    @Override
    public void handleData(int position) {
        String s = strings.get(position);
        title.setText(s);
    }

    @Override
    public int getResId() {
        return R.layout.list_item;
    }

    @Override
    public void bindViews(View itemView) {
        title = itemView.findViewById(R.id.strTitle);
    }
});
rcv.setAdapter(mAdapter);
rcv.setLayoutManager(ew LinearLayoutManager(context));

再看原理

// 繼承 RecyclerView.Adapter
// 提取一層 CommItemHolder,其持有 AdapterItem
//   在 onCreateViewHolder 時,將 adapterItem 注入
//   在 onBindViewHolder 時,經過 CommItemHolder 獲取 adapterItem,實現 handleData
public class CommRecyclerViewAdapter<T> extends RecyclerView.Adapter<CommItemHolder> {

    private AdapterItem mAdapterItem;

     public CommRecyclerViewAdapter(List<T> data, AdapterItem adapterItem) {
        this.mData = mData;
        this.mAdapterItem = adapterItem;
    }

    public CommRecyclerViewAdapter(T[] arrayData, AdapterItem adapterItem) {
        this.mArrayData = arrayData;
        this.mAdapterItem = adapterItem;
    }

    public CommItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CommItemHolder(parent.getContext(), parent, mAdapterItem);
    }

    public void onBindViewHolder(CommItemHolder holder, int position) {
        // 經過 tag 存儲 position,方便 clickListener
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);
        holder.itemView.setOnLongClickListener(this);
        holder.mAdapterItem.handleData(position);
    }

    public void onClick(View view) {
        int position = (int) view.getTag();
        if (clickListener != null) {
            clickListener.onClickItem(position);
        }
    }

    public boolean onLongClick(View view) {
        int position = (int) view.getTag();
        if (longClickListener != null) {
            longClickListener.onLongClickItem(position);
            return true;
        }
        return false;
    }
}

AdapterItem:定義3個需實現的方法github

public interface AdapterItem {
    // 數據渲染到視圖
    // position 當前 item 位置
    void handleData(int position);
    
    // 需返回佈局 id
    int getResId();

    // 這裏面作視圖的 itemView.findViewById
    void bindViews(View itemView);
}

CommItemHolder:
經過在構造方法中 super 中加載佈局,而後調 adapterItem.bindViews 將加載的佈局返回。利用了 RecyclerView.ViewHolder 內部存有的 itemView 對象maven

public class CommItemHolder extends RecyclerView.ViewHolder {
    AdapterItem mAdapterItem;

    CommItemHolder(Context context, ViewGroup parent, AdapterItem adapterItem) {
        super(LayoutInflater.from(context).inflate(adapterItem.getResId(), parent, false));
        mAdapterItem = adapterItem;
        mAdapterItem.bindViews(this.itemView);
    }
}
相關文章
相關標籤/搜索