gpt4 book ai didi

java - Holder 中发生某些情况时更新 RecyclerView

转载 作者:行者123 更新时间:2023-12-02 09:42:01 25 4
gpt4 key购买 nike

所以,这是我的项目结构。这只是一个两个选项卡的应用程序。

左侧选项卡上有所有可用项目的列表。在右侧选项卡上,有用户最喜欢的项目的列表。每个项目都有一个按钮,单击该按钮,该项目就会成为 Collection 夹。好吧,至少这是我想要实现的目标。显然,这些项目已添加到 Collection 夹列表中,但目前查看新 Collection 夹列表的唯一方法是关闭并重新打开应用程序。

我需要的是在将项目添加为 Collection 夹时调用adapter.notifyDataSetChanged()的某种方法。

现在,这个最喜欢的功能是作为我正在使用的 Holder 的一种方法完成的。因此,基本上,用户单击该项目上的 Collection 夹按钮,然后 Holder 有一个 clickListener ,它更新我在 SharedPreferences 中存储为 JSON 的数组,其中包含所有 Collection 夹项目。

这两个选项卡是 Fragments,每个 Fragment 都有一个 RecyclerView。他们都使用相同的类:

public class PlaceholderFragment extends Fragment {

private static final String ARG_SECTION_NUMBER = "section_number";

private PageViewModel pageViewModel;

private int section;

private RecyclerView recyclerView;

private SharedPreferences prefs = null;

public static PlaceholderFragment newInstance(int index) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle bundle = new Bundle();
bundle.putInt(ARG_SECTION_NUMBER, index);
fragment.setArguments(bundle);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class);
int index = 1;
if (getArguments() != null) {
index = getArguments().getInt(ARG_SECTION_NUMBER);
}

section = index;
pageViewModel.setIndex(index);
prefs = getContext().getSharedPreferences("app_preferences", MODE_PRIVATE);
}

@Override
public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView = rootView.findViewById(R.id.items_list);

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);

recyclerView.setLayoutManager(linearLayoutManager);

List<ItemModel> itemList= new ArrayList<>();

// All Items Tab
if (section == 1) {
// Here I get all the Items and set them in the itemList Array

else { // Favorite Items Tab (it retrieves an array of items stored in the SharedPreferences)
Gson gson = new Gson();
String json = prefs.getString("favoriteList", "");

if (json.isEmpty()) {

} else {
Type type = new TypeToken<List<ItemModel>>() {
}.getType();

itemList= gson.fromJson(json, type);
}
}

MyItemAdapter itemAdapter = new MyItemAdapter (getContext(), getActivity(), itemList, section);

recyclerView.setAdapter(itemAdapter);

return rootView;
}

最佳答案

您可以使用MutableLiveData来观察您的列表,然后您可以在观察 block 内调用adapter.notifyDataSetChanged()。因此,例如,

public class PlaceholderFragment extends Fragment {

private MutableLiveData<List<ItemModel>> mMutableLiveData;

@Override
public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mMutableLiveData = new MutableLiveData<>();
mMutableLiveData.observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
rootView.findViewById(R.id.items_list).getAdapter().notifyDataSetChanged();
}
});

List<ItemModel> itemList= new ArrayList<>();

// All Items Tab
if (section == 1) {
// Here I get all the Items and set them in the itemList Array

else { // Favorite Items Tab (it retrieves an array of items stored in the SharedPreferences)
Gson gson = new Gson();
String json = prefs.getString("favoriteList", "");

if (json.isEmpty()) {

} else {
Type type = new TypeToken<List<ItemModel>>() {
}.getType();

itemList= gson.fromJson(json, type);
mMutableLiveData.setValue(itemList);
}
}

MyItemAdapter itemAdapter = new MyItemAdapter (getContext(), getActivity(), itemList, section);

recyclerView.setAdapter(itemAdapter);

return rootView;
}
}

关于java - Holder 中发生某些情况时更新 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56995781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com