[英]RecyclerView with ViewModel in Fragment in Java
我有一個 RecyclerView MVVM 項目,我是 Android 架構組件的新手,現在我想使用 ViewModel 保留 RecyclerView 的 state,但在片段中我的代碼不起作用(在活動中)
現在我只顯示 MyFragment 和 MyViewModel 以及 rest 我附上教程的鏈接
教程https://medium.com/@atifmukhtar/recycler-view-with-mvvm-livedata-a1fd062d2280
我的農場
public class HomeFragment extends Fragment implements LifecycleOwner {
private RecyclerView recyclerView;
private HomeAdapter homeAdapter;
private HomeViewModel viewModel;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
this.setHasOptionsMenu(true);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
//I use code from
//https://developer.android.com/topic/libraries/architecture/viewmodel
viewModel.getUserMutableLiveData().observe(getViewLifecycleOwner(), userListUpdateObserver);
viewModel = new ViewModelProvider(requireActivity()).get(HomeViewModel.class);
return view;
}
private Observer<ArrayList<ProductData>> userListUpdateObserver = new Observer<ArrayList<ProductData>>() {
@Override
public void onChanged(ArrayList<ProductData> userArrayList) {
homeAdapter = new HomeAdapter(requireActivity(),userArrayList);
recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
recyclerView.setAdapter(homeAdapter);
}
};
}
我的視圖模型
public class HomeViewModel extends ViewModel {
private MutableLiveData<ArrayList<ProductData>> userLiveData;
private ArrayList<ProductData> userArrayList;
public HomeViewModel() {
userLiveData = new MutableLiveData<>();
init();
}
MutableLiveData<ArrayList<ProductData>> getUserMutableLiveData() {
return userLiveData;
}
private void init(){
populateList();
userLiveData.setValue(userArrayList);
}
private void populateList(){
ProductData user = new ProductData();
user.setName("Daylight");
user.setPrice("Best rating movie");
userArrayList = new ArrayList<>();
userArrayList.add(user);
userArrayList.add(user);
userArrayList.add(user);
userArrayList.add(user);
userArrayList.add(user);
userArrayList.add(user);
userArrayList.add(user);
userArrayList.add(user);
userArrayList.add(user);
userArrayList.add(user);
userArrayList.add(user);
userArrayList.add(user);
}
}
在您的 HomeFragment 上,您正在訪問viewModel
當前是null
。 你應該改變這個:
viewModel.getUserMutableLiveData().observe(getViewLifecycleOwner(), userListUpdateObserver);
viewModel = new ViewModelProvider(requireActivity()).get(HomeViewModel.class);
對此:
viewModel = new ViewModelProvider(requireActivity()).get(HomeViewModel.class);
viewModel.getUserMutableLiveData().observe(getViewLifecycleOwner(), userListUpdateObserver);
我希望這可以幫助你。
問題出在 Fragment 和 RecyclerView 本身。 我通過在 XML 中調用 setLayoutManager 設法解決了這個問題。
在 MyFragment 我刪除了
recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
我在 XML 中調用 setLayoutManager
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.