gpt4 book ai didi

java - 如何从回收者 View 适配器获取项目

转载 作者:行者123 更新时间:2023-12-02 01:07:24 24 4
gpt4 key购买 nike

我有一个以下类(我将另一个回收器 View 适配器传递给构造函数):

class BaseAdapter<T>(private var items: ArrayList<T?>, private val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>): RecyclerView.Adapter<RecyclerView.ViewHolder>()

是否可以不直接传递项目并从适配器获取它们?

我不想再次传递项目并希望从适配器获取它们。

private var adapter: RecyclerViewAdapter? = null
adapter = RecyclerViewAdapter(items!!, applicationContext) // another adapter, can be any


recyclerView.layoutManager = layoutManager
recyclerView.adapter = BaseAdapter(items, adapter!!)

如您所见,我传递了两次项目,因为我需要在新的适配器中使用它们,例如

 override fun showAd(firstVisibleItemPosition: Int) {
if(isInsertionNeeded){
items.add(firstVisibleItemPosition + 1, null)

setVisibleItemPos(firstVisibleItemPosition + 1)

notifyItemInserted(firstVisibleItemPosition+1)

isInsertionNeeded = false
}
}

最佳答案

您必须像这样创建BaseRecyclerViewAdapter:-

public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter implements AppConstants {

public SmartArrayList<T> mArrayList;
public Context mContext;
private int margin;

public BaseRecyclerViewAdapter(Context mContext) {
this.mArrayList = new SmartArrayList<>();
this.mContext = mContext;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(getView(), parent, false);
return getViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
if (mArrayList != null && mArrayList.size() > 0) {
final T obj = mArrayList.get(holder.getAdapterPosition());
setData(holder, obj, position);
}
}


@Override
public int getItemCount() {
return mArrayList.size();
}

public abstract RecyclerView.ViewHolder getViewHolder(View view);

public abstract int getView();

public abstract void setData(RecyclerView.ViewHolder holder, T data, int position);

public void addAll(ArrayList<T> mArrayList) {
if (null != mArrayList) {
this.mArrayList.addAll(mArrayList);
notifyItemRangeInserted(getItemCount(), mArrayList.size());
}
}

public void updateAll(ArrayList<T> mArrayList) {
this.mArrayList = new SmartArrayList<>();
this.mArrayList.addAll(mArrayList);
notifyDataSetChanged();
// notifyItemRangeInserted(getItemCount(), mArrayList.size());
}

public void clear() {
this.mArrayList.clear();
notifyDataSetChanged();
}

public void update(int position, T obj) {
this.mArrayList.set(position, obj);
notifyItemChanged(position);
}

public void remove(int position) {
this.mArrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,mArrayList.size());
}

public void add(T obj) {
int position = mArrayList.size();
this.mArrayList.add(obj);
notifyItemInserted(position);
}

public void add(int position, T obj) {
this.mArrayList.add(obj);
notifyItemInserted(position);
}

public SmartArrayList<T> getData() {
return mArrayList;
}
}

使用......

公共(public)类YourAdapterName扩展BaseRecyclerViewAdapter{

你的代码......

}

您可以轻松获取数据。

关于java - 如何从回收者 View 适配器获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59838144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com