Android - 观察无法从片段中工作的实时数据
Android - Observing on live data not working from fragment
我在 Activity 中有 2 个片段,一个列表片段和详细信息片段,详细信息片段显示列表片段中所选项目的详细信息,并且有一个按钮可以更改列表项"status" 将订单设置为就绪。
我想在点击订单就绪按钮时将所选项目移动到就绪部分。
我尝试使用共享视图模型进行观察,但是当我在其中设置值时 onchange 方法没有调用。
这是一个视图模型:
package com.example.ordermanager.fragments;
import android.database.ContentObserver;
import android.os.Handler;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.example.ordermanager.fragments.orderlist.dummy.DummyContent;
import java.util.List;
public class SharedViewModel extends ViewModel {
private MutableLiveData<DummyContent.DummyItem> item = new MutableLiveData<DummyContent.DummyItem>();
public void setItem(DummyContent.DummyItem value){
item.setValue(value);
}
public MutableLiveData<DummyContent.DummyItem> getItem(){
return item;
};
}
ListFragment:
public class OrderItemFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
private SharedViewModel vm;
private RecyclerView recyclerView;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public OrderItemFragment() {
}
// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static OrderItemFragment newInstance(int columnCount) {
OrderItemFragment fragment = new OrderItemFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
vm = ViewModelProviders.of(this).get(SharedViewModel.class);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_order_item_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyOrderItemRecyclerViewAdapter(DummyContent.ITEMS, mListener));
}
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Observer<DummyItem> itemObserver = new Observer<DummyItem>() {
@Override
public void onChanged(@Nullable DummyItem selectedItem) {
//this never happening
Log.e("hereeeee","dfgdfg");
recyclerView.getAdapter().notifyDataSetChanged();
}
};
vm.getItem().observe(this, itemObserver);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(DummyItem item);
}
}
详细片段:
public class OrderDetailFragment extends Fragment {
private SharedViewModel mViewModel;
private DummyContent.DummyItem selectedItem;
private Button ReadyBtn;
public static OrderDetailFragment newInstance() {
return new OrderDetailFragment();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.order_detail_fragment, container, false);
Bundle bundle = getArguments();
if(bundle != null){
selectedItem = (DummyContent.DummyItem)getArguments().getSerializable("item");
TextView tv = (TextView) view.findViewById(R.id.detailid);
tv.setText(selectedItem.content);
}
ReadyBtn = view.findViewById(R.id.readyBtn);
ReadyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(selectedItem != null){
selectedItem.isReady = true;
mViewModel.getItem().setValue(selectedItem);
}
}
});
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(SharedViewModel.class);
}
}
观察者在ListFragment的OnViewCreated函数中
有什么想法吗?
当您在函数中声明局部变量时,它们会在函数调用结束时被销毁。因此,您需要将 itemObserver
存储在一个字段中。
旁注...
片段中不需要默认的空构造函数,除非您创建自定义构造函数,不推荐这样做。
关于 recyclerview
我建议详细阅读 this(尤其是 ListAdapter
部分)。
您应该在调用 notifyDataSetChanged()
方法之前更改适配器中的数据。现在您在 itemObserver
中获得了新值,但您没有更改适配器。
更新。我已经解决了这个问题! SharedViewModel
初始化代码中的密钥。在这两种情况下,您都应该将 activity 附加到 ViewModelProviders
class,但是您使用了这个,实际上您有两个不同的实例,而不是应该附加到父实例的实例 activity.因此,将初始化代码更改为
mViewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
它会成功的!
我在 Activity 中有 2 个片段,一个列表片段和详细信息片段,详细信息片段显示列表片段中所选项目的详细信息,并且有一个按钮可以更改列表项"status" 将订单设置为就绪。
我想在点击订单就绪按钮时将所选项目移动到就绪部分。
我尝试使用共享视图模型进行观察,但是当我在其中设置值时 onchange 方法没有调用。
这是一个视图模型:
package com.example.ordermanager.fragments;
import android.database.ContentObserver;
import android.os.Handler;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.example.ordermanager.fragments.orderlist.dummy.DummyContent;
import java.util.List;
public class SharedViewModel extends ViewModel {
private MutableLiveData<DummyContent.DummyItem> item = new MutableLiveData<DummyContent.DummyItem>();
public void setItem(DummyContent.DummyItem value){
item.setValue(value);
}
public MutableLiveData<DummyContent.DummyItem> getItem(){
return item;
};
}
ListFragment:
public class OrderItemFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
private SharedViewModel vm;
private RecyclerView recyclerView;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public OrderItemFragment() {
}
// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static OrderItemFragment newInstance(int columnCount) {
OrderItemFragment fragment = new OrderItemFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
vm = ViewModelProviders.of(this).get(SharedViewModel.class);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_order_item_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyOrderItemRecyclerViewAdapter(DummyContent.ITEMS, mListener));
}
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Observer<DummyItem> itemObserver = new Observer<DummyItem>() {
@Override
public void onChanged(@Nullable DummyItem selectedItem) {
//this never happening
Log.e("hereeeee","dfgdfg");
recyclerView.getAdapter().notifyDataSetChanged();
}
};
vm.getItem().observe(this, itemObserver);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(DummyItem item);
}
}
详细片段:
public class OrderDetailFragment extends Fragment {
private SharedViewModel mViewModel;
private DummyContent.DummyItem selectedItem;
private Button ReadyBtn;
public static OrderDetailFragment newInstance() {
return new OrderDetailFragment();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.order_detail_fragment, container, false);
Bundle bundle = getArguments();
if(bundle != null){
selectedItem = (DummyContent.DummyItem)getArguments().getSerializable("item");
TextView tv = (TextView) view.findViewById(R.id.detailid);
tv.setText(selectedItem.content);
}
ReadyBtn = view.findViewById(R.id.readyBtn);
ReadyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(selectedItem != null){
selectedItem.isReady = true;
mViewModel.getItem().setValue(selectedItem);
}
}
});
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(SharedViewModel.class);
}
}
观察者在ListFragment的OnViewCreated函数中
有什么想法吗?
当您在函数中声明局部变量时,它们会在函数调用结束时被销毁。因此,您需要将 itemObserver
存储在一个字段中。
旁注...
片段中不需要默认的空构造函数,除非您创建自定义构造函数,不推荐这样做。
关于 recyclerview
我建议详细阅读 this(尤其是 ListAdapter
部分)。
您应该在调用 notifyDataSetChanged()
方法之前更改适配器中的数据。现在您在 itemObserver
中获得了新值,但您没有更改适配器。
更新。我已经解决了这个问题! SharedViewModel
初始化代码中的密钥。在这两种情况下,您都应该将 activity 附加到 ViewModelProviders
class,但是您使用了这个,实际上您有两个不同的实例,而不是应该附加到父实例的实例 activity.因此,将初始化代码更改为
mViewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
它会成功的!