删除列表项后,从适配器类更新片段textView文本

时间:2020-04-22 22:14:28

标签: android android-recyclerview

我有一个带有recyclerView的片段。

该片段中有一个textview,显示了来自recyclerView的项目数量。

在每个recyclerView项目上都有一个按钮,用于从recyclerView中删除该项目:

在适配器中:

subjects.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
notifyItemRangeChanged(position,subjects.size());

其中subject是填充回收视图的列表。

这三行代码确实从recyclerView中删除了该项目并更新了列表大小。

我需要同时从片段中更新textview,该片段显示了recyclerview中的项目数,并将新值从subject.size()传递给片段,作为textview的文本。

编辑

public class RecyclerViewFollowingMiosBusquedaCardViewAdapter extends RecyclerView.Adapter<RecyclerViewFollowingMiosBusquedaCardViewAdapter.ViewHolder> {

    Context context;

    List<UsuariosBuscarFollowing> subjects;
public RecyclerViewFollowingMiosBusquedaCardViewAdapter(List<UsuariosBuscarFollowing> getDataAdapter, Context context){

    super();

    this.subjects = getDataAdapter;

    this.context = context;



}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_amigos_following_busqueda, parent, false);

    ViewHolder viewHolder = new ViewHolder(view);

    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

片段。适配器初始化:

txtNumFollowers.setText(numFollowers+" "+getResources().getString(R.string.followers));


        recyclerViewadapterFollowers = new RecyclerViewFollowersMiosBusquedaCardViewAdapter(subjectsListFollowers, getActivity());
        recyclerViewFollowers.setAdapter(recyclerViewadapterFollowers);

2 个答案:

答案 0 :(得分:1)

首先在项目中的新文件中创建界面

public interface onItemRemovedFromListListener{

void onItemRemoved(String text);
}

在您的片段中

class MyFragment extends Fragment implements onItemRemovedFromListListener{



//pass the listener to the adapter

........
recyclerViewadapterFollowers = new RecyclerViewFollowersMiosBusquedaCardViewAdapter(subjectsListFollowers, getActivity(),this);
recyclerViewFollowers.setAdapter(recyclerViewadapterFollowers);

}

@override
public void onItemRemoved(String text){

//update your text view
txtNumFollowers.setText(text);

}


}

在您的适配器中

//include a new field for listener
........
private onItemRemovedFromListListener listener;
.......

//constructor

public RecyclerViewFollowingMiosBusquedaCardViewAdapter(List<UsuariosBuscarFollowing> getDataAdapter, Context context , onItemRemovedFromListListener listener){

    super();

    this.subjects = getDataAdapter;

    this.context = context;

    this.listener = listener;



}

//now when you remove item from adapter notify the listener the fragment method will fire

subjects.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
notifyItemRangeChanged(position,subjects.size());

//send text to update
listener.onItemRemoved("the text that you want to update goes here");

答案 1 :(得分:0)

尝试将您的主题列表用作实时数据

LiveData<List<Subject>> subjects = new MutableLiveData<>();

然后观察您的列表并听取更改。而当您根据textView获取新的列表更新UI时。

   subjects.observe(this, subjects -> {
       // update your textView here

        });