fragment在attach里面获取了传过来的集合如何刷新ui
时间: 2024-04-29 20:25:24 浏览: 73
在attach方法中获取到集合后,可以通过调用适配器的notifyDataSetChanged()方法来刷新UI。具体实现方式如下:
1.在attach方法中获取到集合:
```
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof MyListener) {
myListener = (MyListener) context;
list = myListener.getList();
}
}
```
2.在适配器中调用notifyDataSetChanged()方法:
```
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> list;
public MyAdapter(List<String> list) {
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//...
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//...
}
@Override
public int getItemCount() {
return list.size();
}
public void refresh(List<String> list) {
this.list = list;
notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
//...
}
}
```
3.在Activity或Fragment中调用适配器的refresh方法:
```
myAdapter.refresh(list);
```
阅读全文