【问题标题】:refresh the whole RecyclerView from within the Adapter从适配器内刷新整个 RecyclerView
【发布时间】:2019-07-19 22:08:23
【问题描述】:

我尝试从 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)));

}

找到解决方案

不幸的是,cmets 中的解决方案都没有帮助,方法 notifyDataSetChanged()notifyItemChanged(position) 没有刷新数据。

也许它会在未来帮助某人: 为了刷新整个列表,我访问了Activity(Store)中的init方法(initImageBitmapsForAllPlayers),并从Adapter中的onBindViewHolder调用:

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

【问题讨论】:

    标签: java android android-recyclerview adapter


    【解决方案1】:

    调用下面的方法来刷新适配器内部:

    notifyDataSetChanged()
    

    内部活动

    if (recyclerView.getAdapter() != null) {
         recyclerView.getAdapter().notifyDataSetChanged();
    }
    

    【讨论】:

      【解决方案2】:

      如果您想要更改整个项目集,只需在适配器中调用 notifyDataSetChanged()。如果知道哪些项目发生了变化,notifyItemChanged()、notifyItemInserted() 和 notifyItemDeleted() 效率更高。

      【讨论】:

      • 两者都不起作用。没有错误消息,并且列表或项目未刷新。
      【解决方案3】:

      单击时只更改列表中的一项,更改其参数后,调用:

      notifyItemChanged(position) 
      

      【讨论】:

        【解决方案4】:

        如果您使用了 recyclerview 并且想要更改整个回收器视图项目以便您可以使用 notifyDataSetChanged(),如果您从 recyclerview 中删除一些项目以便您可以使用 notifyItemDeleted(),并且如果您添加了更多数据,那么在添加数据之后只需拨打notifyItemChanged(), notifyItemInserted()

        如果您想从活动中添加删除和更新数据,以便您可以使用界面

        【讨论】:

        • 两者都不起作用。没有错误消息,并且列表或项目未刷新。
        【解决方案5】:

        试试这个:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-29
          • 1970-01-01
          • 2018-10-11
          • 2017-11-16
          • 1970-01-01
          相关资源
          最近更新 更多