簡體   English   中英

從適配器內部刷新整個RecyclerView

[英]refresh the whole RecyclerView from within the Adapter

我嘗試從Adapter類( onBindViewHolder方法)中刷新整個RecyclerView。 如果單擊列表中的某個項目,則希望該項目的參數會發生變化。 最好的方法是什么?

Adapter類中的onBindViewHolder方法:

@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();

            }
        });
    }
}

RecyclerView類中的initRecyclerView方法:

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中的onBindViewHolder調用它:

if (context instanceof Store) {
    notifyItemChanged(position);
    ((Store) context).initImageBitmapsForAllPlayers();
}

如果要更改整個項目集,只需在適配器內調用notifyDataSetChanged()。 如果您知道哪些項目已更改,則notifyItemChanged(),notifyItemInserted()和notifyItemDeleted()會更高效。

要在單擊時僅更改列表中的一項,請在更改其參數后調用:

notifyItemChanged(position) 

如果您使用過recyclerview並想更改整個recyclerview項目,那么可以使用notifyDataSetChanged() ,如果您從recyclerview中刪除了某些項目,那么您可以使用notifyItemDeleted()並且如果您添加了更多數據,那么在添加數據之后,只需調用notifyItemChanged(), notifyItemInserted()

如果您想從活動中添加刪除和更新數據,以便可以使用界面

調用以下方法刷新適配器內部:

notifyDataSetChanged()

內部活動

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM