android view如何更新,android - 如何在RecyclerView中更新/刷新特定项目

android - 如何在RecyclerView中更新/刷新特定项目

我正在尝试刷新AlertDialog中的特定项目。

故事:每当用户点击项目时,它会显示AlertDialog.用户可以通过单击确定按钮键入一些文本。 我想在此项目中显示此文本并显示隐形ImageView - 以XML和适配器ViewHolder声明 -

我在AlertDialog正面按钮中使用此功能来更新项目:

private void updateListItem(int position) {

View view = layoutManager.findViewByPosition(position);

ImageView medicineSelected = (ImageView) view.findViewById(R.id.medicine_selected);

medicineSelected.setVisibility(View.VISIBLE);

TextView orderQuantity = (TextView) view.findViewById(R.id.order_quantity);

orderQuantity.setVisibility(View.VISIBLE);

orderQuantity.setText(quantity + " packet added!");

medicinesArrayAdapter.notifyItemChanged(position);

}

但是这段代码不仅改变了传递位置的itemView,还改变了其他一些itemView(s)!

如何通过单击更改特定的itemView?

10个解决方案

74 votes

您可以使用RecyclerView.Adapter类中的notifyItemChanged(int position)方法。 从文档:

通知任何注册观察员该位置的项目已更改。   相当于调用notifyItemChanged(position,null);.

这是一个项目更改事件,而不是结构更改事件。 它   表示位置数据的任何反映都已过时   并应该更新。 位置处的项目保留相同的标识。

由于你已经拥有这个职位,它应该适合你。

Edson Menegatti answered 2019-05-31T20:36:39Z

18 votes

更新单个项目

更新数据项

使用notifyItemChanged(updateIndex)通知适配器更改

更改“羊”项目,使其显示“我喜欢羊”。

UUzHr.gif

String newValue = "I like sheep.";

int updateIndex = 3;

data.set(updateIndex, newValue);

adapter.notifyItemChanged(updateIndex);

我的完整答案有更多的例子。

Suragch answered 2019-05-31T20:37:22Z

6 votes

我必须通过捕获需要修改的项目的位置来解决此问题然后在适配器调用中

public void refreshBlockOverlay(int position) {

notifyItemChanged(position);

}

,这将在此特定位置为此特定项调用onBindViewHolder(ViewHolder holder,int position)。

haneenCo answered 2019-05-31T20:37:53Z

4 votes

我想我对如何处理这个有一个想法。 更新与在确切位置删除和替换相同。 所以我首先使用下面的代码从该位置删除该项目:

public void removeItem(int position){

mData.remove(position);

notifyItemRemoved(position);

notifyItemRangeChanged(position, mData.size());

}

然后我会在该特定位置添加项目,如下所示:

public void addItem(int position, Landscape landscape){

mData.add(position, landscape);

notifyItemInserted(position);

notifyItemRangeChanged(position, mData.size());

}

我现在正试图实现这一点。 当我通过时,我会给你一个反馈!

Black Panther answered 2019-05-31T20:38:31Z

1 votes

在适配器类中,在onBindViewHolder方法中,将ViewHolder设置为setIsRecyclable(false),如下面的代码所示。

@Override

public void onBindViewHolder(RecyclerViewAdapter.ViewHolder p1, int p2)

{

// TODO: Implement this method

p1.setIsRecyclable(false);

// Then your other codes

}

shankar_vl answered 2019-05-31T20:38:56Z

1 votes

一种对我个人有用的方法是使用recyclerview的适配器方法来处理其项目的变化。

它会以类似于此的方式,在您的自定义回收站视图中创建一个方法,如下所示:

public void modifyItem(final int position, final Model model) {

mainModel.set(position, model);

notifyItemChanged(position);

}

user3709410 answered 2019-05-31T20:39:27Z

1 votes

以下解决方案适合我:

在RecyclerView项目上,用户将单击一个按钮,但像TextView这样的另一个视图将更新而不直接通知适配器:

我没有使用notifyDataSetChanged()方法找到了一个很好的解决方案,这个方法重新加载了recyclerView的所有数据,所以如果你有项目内的图像或视频,那么他们将重新加载,用户体验不会很好:

下面是一个单击ImageView图标的示例,只更新单个TextView(可以以相同项目的相同方式更新更多视图)以在添加+1后显示类似计数更新:

// View holder class inside adapter class

public class MyViewHolder extends RecyclerView.ViewHolder{

ImageView imageViewLike;

public MyViewHolder(View itemView) {

super(itemView);

imageViewLike = itemView.findViewById(R.id.imageViewLike);

imageViewLike.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

int pos = getAdapterPosition(); // Get clicked item position

TextView tv = v.getRootView().findViewById(R.id.textViewLikeCount); // Find textView of recyclerView item

resultList.get(pos).setLike(resultList.get(pos).getLike() + 1); // Need to change data list to show updated data again after scrolling

tv.setText(String.valueOf(resultList.get(pos).getLike())); // Set data to TextView from updated list

}

});

}

Touhid answered 2019-05-31T20:40:12Z

0 votes

您只需在保存点击时在警告对话框中添加以下代码

recyclerData.add(position, updateText.getText().toString());

recyclerAdapter.notifyItemChanged(position);

Dhruvisha answered 2019-05-31T20:40:37Z

-1 votes

这也是我的最后一个问题。 我的解决方案我为RecyclerView使用数据模型和适配器

/*Firstly, register your new data to your model*/

DataModel detail = new DataModel(id, name, sat, image);

/*after that, use set to replace old value with the new one*/

int index = 4;

mData.set(index, detail);

/*finally, refresh your adapter*/

if(adapter!=null)

adapter.notifyItemChanged(index);

deya tri answered 2019-05-31T20:41:02Z

-5 votes

在您的RecyclerView适配器中,您应该有一个ArrayList以及一个方法notifyDataSetChanged来将列表项添加到ArrayList。 然后,您可以动态调用adapter.addItemsToList(items)添加列表项。 将所有列表项添加到ArrayList后,您可以调用adapter.notifyDataSetChanged()来显示列表。

您可以在适配器中使用notifyDataSetChanged作为RecyclerView

Nooruddin Lakhani answered 2019-05-31T20:41:34Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值