[英]RecyclerView with ViewModel in Fragment in Java
I have a project with RecyclerView MVVM and I am new to Android architecture components, Now I want to keep the state of RecyclerView using ViewModel, but in the fragment my code does not work(in Activity worked)我有一个 RecyclerView MVVM 项目,我是 Android 架构组件的新手,现在我想使用 ViewModel 保留 RecyclerView 的 state,但在片段中我的代码不起作用(在活动中)
And Now I show only MyFragment and MyViewModel and the rest I attach a link to Tutorial现在我只显示 MyFragment 和 MyViewModel 以及 rest 我附上教程的链接
Tutorial https://medium.com/@atifmukhtar/recycler-view-with-mvvm-livedata-a1fd062d2280教程https://medium.com/@atifmukhtar/recycler-view-with-mvvm-livedata-a1fd062d2280
My Fargment我的农场
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);
}
};
}
My ViewModel我的视图模型
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);
}
}
On your HomeFragment you are accesing to the viewModel
when is currently null
.在您的 HomeFragment 上,您正在访问
viewModel
当前是null
。 You should change this:你应该改变这个:
viewModel.getUserMutableLiveData().observe(getViewLifecycleOwner(), userListUpdateObserver);
viewModel = new ViewModelProvider(requireActivity()).get(HomeViewModel.class);
to this:对此:
viewModel = new ViewModelProvider(requireActivity()).get(HomeViewModel.class);
viewModel.getUserMutableLiveData().observe(getViewLifecycleOwner(), userListUpdateObserver);
I hope this helps you.我希望这可以帮助你。
The problem was in Fragment and RecyclerView itself.问题出在 Fragment 和 RecyclerView 本身。 I managed to solve this problem by calling setLayoutManager in XML.
我通过在 XML 中调用 setLayoutManager 设法解决了这个问题。
In MyFragment i deleted在 MyFragment 我删除了
recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
I call setLayoutManager in XML我在 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.