从 Adapter 中刷新整个 RecyclerView
refresh the whole RecyclerView from within the Adapter
我尝试从 Adapter class(onBindViewHolder
方法)中刷新整个 RecyclerView。
如果我点击列表中的一个项目,我希望这个项目的参数会改变。
最好的方法是什么?
onBindViewHolder
Adapter
中的方法 class:
@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
中的方法 class:
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)
没有刷新数据。
也许它会在将来帮助某人:
为了刷新整个列表,我访问了 Activity (Store
) 中的 init 方法 (initImageBitmapsForAllPlayers
),并从适配器中的 onBindViewHolder
调用它:
if (context instanceof Store) {
notifyItemChanged(position);
((Store) context).initImageBitmapsForAllPlayers();
}
如果您想要更改整个项目集,只需在适配器中调用 notifyDataSetChanged() 即可。如果您知道更改了哪些项目,notifyItemChanged()、notifyItemInserted() 和 notifyItemDeleted() 会更有效。
要在单击时仅更改列表中的一项,请在更改其参数后调用:
notifyItemChanged(position)
如果您已经使用过 recyclerview 并且想要更改整个回收器视图项目以便您可以使用 notifyDataSetChanged()
,如果您从 recyclerview 中删除了一些项目以便您可以使用 notifyItemDeleted()
如果您添加了一些更多数据,所以在添加数据后只需调用 notifyItemChanged(), notifyItemInserted()
如果你想从 activity 添加删除和更新数据,这样你就可以使用接口
调用以下方法 刷新 内部适配器:
notifyDataSetChanged()
里面activity
if (recyclerView.getAdapter() != null) {
recyclerView.getAdapter().notifyDataSetChanged();
}
试试这个:
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();
我尝试从 Adapter class(onBindViewHolder
方法)中刷新整个 RecyclerView。
如果我点击列表中的一个项目,我希望这个项目的参数会改变。
最好的方法是什么?
onBindViewHolder
Adapter
中的方法 class:
@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
中的方法 class:
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)
没有刷新数据。
也许它会在将来帮助某人:
为了刷新整个列表,我访问了 Activity (Store
) 中的 init 方法 (initImageBitmapsForAllPlayers
),并从适配器中的 onBindViewHolder
调用它:
if (context instanceof Store) {
notifyItemChanged(position);
((Store) context).initImageBitmapsForAllPlayers();
}
如果您想要更改整个项目集,只需在适配器中调用 notifyDataSetChanged() 即可。如果您知道更改了哪些项目,notifyItemChanged()、notifyItemInserted() 和 notifyItemDeleted() 会更有效。
要在单击时仅更改列表中的一项,请在更改其参数后调用:
notifyItemChanged(position)
如果您已经使用过 recyclerview 并且想要更改整个回收器视图项目以便您可以使用 notifyDataSetChanged()
,如果您从 recyclerview 中删除了一些项目以便您可以使用 notifyItemDeleted()
如果您添加了一些更多数据,所以在添加数据后只需调用 notifyItemChanged(), notifyItemInserted()
如果你想从 activity 添加删除和更新数据,这样你就可以使用接口
调用以下方法 刷新 内部适配器:
notifyDataSetChanged()
里面activity
if (recyclerView.getAdapter() != null) {
recyclerView.getAdapter().notifyDataSetChanged();
}
试试这个:
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();