安卓 - 从碎片中观察实时数据不起作用。

问题描述 投票:0回答:1

我在activityty中有2个片段,一个是列表片段,另一个是详情片段,详情片段显示的是列表片段中选择的项目详情,有一个按钮可以改变列表项目的 "状态",设置订单为就绪。

我想在点击Order is ready的按钮时,将选中的项目移动到ready seaction。

我试着用共享视图模型观察,但当我在其中设置值时,onchange方法没有被调用。

enter image description here

这里是一个viewModel。

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);
    }
}

DetailsFragment。

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函数中。

有什么好办法吗?

android android-fragments android-livedata mutablelivedata
1个回答
0
投票

你应该在调用你的适配器之前改变数据。notifyDataSetChanged() 方法。现在你在 itemObserver 但你没有更换适配器。

UPD. 我已经解决了这个问题! 你的初始化代码中的 SharedViewModel. 您应该将活动附加到 ViewModelProviders 类,但您使用了这个方法,实际上您有两个不同的实例,而不是一个应该连接到父活动的实例。所以,把初始化的代码改为 mViewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class); 就可以了!


0
投票

当你在函数中声明局部变量时,它们会在函数调用结束时被销毁。因此你需要将你的 itemObserver 在田里。

顺便说一句... 你不需要在fragments中使用默认的空构造函数,除非你创建了一个自定义的构造函数,这并不推荐。

关于 recyclerview 我建议阅读 这个 详解(特别是 ListAdapter 部分)。)

© www.soinside.com 2019 - 2024. All rights reserved.