我尝试从Adapter类(onBindViewHolder
方法)中刷新整个RecyclerView。
如果单击列表中的某个项目,则希望该项目的参数会发生变化。
最好的方法是什么?
onBindViewHolder
类中的 Adapter
方法:
@Override
public void onBindViewHolder(@NonNull final StoreRecyclerViewAdapter.ViewHolder holder, final int position) {
// Get the image of the Player item
Glide.with(context)
.asBitmap()
.load(allPlayers.get(position))
.into(holder.playerInStore);
// Get the price of the Player of the item
holder.playerPrice.setText(allPrices.get(position).toString());
// Get the status of the Player of the item
holder.status = allStatus.get(position);
// If Player status is "CLOSED", get the closed lock image of the item
if (allStatus.get(position).equals("CLOSE")) {
Glide.with(context)
.asBitmap()
.load(close.getStatusPic())
.into(holder.playerLock);
// The user can purchase the item
holder.layoutStore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int price = Integer.valueOf(holder.playerPrice.getText().toString());
boolean canPurchase = StoreLogic.getStoreLogic().canPurchase(price);
if (canPurchase) {
String purchaseSuccessfully = "purchase succcessfully :)";
Toast.makeText(context, purchaseSuccessfully, Toast.LENGTH_SHORT).show();
// update list details
//****************** REFRESH LIST *******************
}
else {
String purchaseFailed = "not enough coins :(";
Toast.makeText(context, purchaseFailed, Toast.LENGTH_SHORT).show();
}
}
});
}
// If Player status is "OPEN", get the open lock image of the item
else {
Glide.with(context)
.asBitmap()
.load(open.getStatusPic())
.into(holder.playerLock);
// The user cannot purchase the item
holder.layoutStore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "already available", Toast.LENGTH_SHORT).show();
}
});
}
}
initRecyclerView
类中的 RecyclerView
方法:
protected void initRecyclerViewForAllPlayers() {
RecyclerView recyclerView = findViewById(R.id.rv_store);
StoreRecyclerViewAdapter adapter = new StoreRecyclerViewAdapter(this, allPlayers, allPrices, allStatus, open, close);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager((this)));
}
找到解决方案
不幸的是,注释中的解决方案都没有帮助,方法notifyDataSetChanged()
和notifyItemChanged(position)
并没有整理数据。
也许将来会帮助某人:
为了刷新整个列表,我访问了活动(initImageBitmapsForAllPlayers
)中的init方法(Store
),并在适配器的onBindViewHolder
中对其进行了调用:
if (context instanceof Store) {
notifyItemChanged(position);
((Store) context).initImageBitmapsForAllPlayers();
}
答案 0 :(得分:1)
如果要更改整个项目集,只需在适配器内调用notifyDataSetChanged()。如果您知道哪些项目已更改,则notifyItemChanged(),notifyItemInserted()和notifyItemDeleted()会更有效。
答案 1 :(得分:1)
要在单击列表时仅更改列表中的一项,请在更改其参数后调用:
notifyItemChanged(position)
答案 2 :(得分:1)
如果您使用过recyclerview并想更改整个recyclerview项目,以便可以使用notifyDataSetChanged()
,如果您从recyclerview中删除了一些项目,因此可以使用notifyItemDeleted()
,并且添加了更多数据因此,在添加数据后,只需调用notifyItemChanged(), notifyItemInserted()
如果要添加活动中的删除和更新数据,以便可以使用界面
答案 3 :(得分:1)
在适配器内部调用以下方法以刷新:
val minString = minValue.filter(_._1 == "http://subdom0003.example.com").first()._2
内部活动
notifyDataSetChanged()
答案 4 :(得分:0)
尝试一下:
RecyclerView recyclerView = findViewById(R.id.rv_store);
recyclerView.setLayoutManager(new LinearLayoutManager(LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
StoreRecyclerViewAdapter adapter = new StoreRecyclerViewAdapter(this, allPlayers, allPrices, allStatus, open, close);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();