复选框不能保留屏幕旋转更改

时间:2019-10-13 07:56:34

标签: android android-recyclerview retrofit

我有一个正在练习MVVM的应用程序。该应用程序仅调用Retrofit,然后在RecyclerView上绘制JSON数据。在RecyclerView行中,我有一个复选框,其功能是:如果已标记,请按或向我展示另一个片段的另一个RecyclerView中那些标记的项目的按钮。问题是,当我标记支票时,在发送支票或其他任何东西时,如果我翻动屏幕,就好像我重新加载列表一样,或者我不太清楚自己在做什么,但是情况是再次取消选中此复选框并且我希望尽管我还没有将这些更改发送给Retrofit,但仍然单击该复选框:

这是我的视图类:

package com.example.menunavegacion.ui.fragments.tabs.fragmentpets.view;


....

public class FragmentPets extends Fragment {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;

    PetsViewModel mWordViewModel;
    PetsAdapter adapter;

    @Inject
    RequestInterface requestInterface;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_tab_one, container, false);
        ButterKnife.bind(this, v);
        initViews();
        return v;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mWordViewModel = ViewModelProviders.of(this).get(PetsViewModel.class);

        mWordViewModel.getAllPets().observe(this, new Observer<ArrayList<PetsDTO>>() {
            @Override
            public void onChanged(ArrayList<PetsDTO> petsDTOS) {
                adapter = new PetsAdapter(petsDTOS, getContext(), false);
                recyclerView.setAdapter(adapter);
            }
        });

        mWordViewModel.getLoadingLiveData().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean loading) {
                if (loading) {
                    progressBar.setVisibility(View.VISIBLE);
                } else {
                    progressBar.setVisibility(View.GONE);
                }
            }
        });

        mWordViewModel.loadJSON();

        mWordViewModel.showFinishMessage().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    Toast.makeText(getActivity(), getActivity().getString(R.string.update_toast_message), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(), getActivity().getString(R.string.message_send_error), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @OnClick(R.id.btnSend)
    public void onSend() {
        mWordViewModel.updateList(adapter.getPetList());
    }

    private void initViews() {
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
    }
}

这是我的视图模型:

package com.example.menunavegacion.ui.fragments.tabs.fragmentpets.viewmodel;

....

public class PetsViewModel extends AndroidViewModel implements ViewModelInterface.ViewModel {

    private PetsRepository mPetsRepository;
    private LiveData<ArrayList<PetsDTO>> mAllPets;

    public PetsViewModel (Application application){
        super(application);
        mPetsRepository = new PetsRepository(application);
        mAllPets = mPetsRepository.getAllPetsLiveData();
    }

    public LiveData<ArrayList<PetsDTO>> getAllPets() {
        return mPetsRepository.getAllPetsLiveData();
    }

    public LiveData<Boolean> getLoadingLiveData() {
        return mPetsRepository.getLoadingLiveData();
    }

    public LiveData<Boolean> showFinishMessage() {
        return mPetsRepository.getFinishMessage();
    }


    @Override
    public void loadJSON() {
        mPetsRepository.useCaseLoadJSON();
    }

    @Override
    public void updateList(ArrayList<PetsDTO> pets) {
        mPetsRepository.useCaseUpdateList(pets);
    }
}

这是存储库:

package com.example.menunavegacion.ui.fragments.tabs;

....

public class PetsRepository {

    LoadJSONUseCase loadJSONUseCase;
    UpdateListUseCase updateListUseCase;

    private MutableLiveData<ArrayList<PetsDTO>> allPetsLiveData = new MutableLiveData<>();
    private MutableLiveData<Boolean> loadingLiveData = new MutableLiveData<>();
    private MutableLiveData<Boolean> showFinishMessage = new MutableLiveData<>();

    public LiveData<ArrayList<PetsDTO>> getAllPetsLiveData() {
        return allPetsLiveData;
    }
    public LiveData<Boolean> getLoadingLiveData() {
        return loadingLiveData;
    }
    public LiveData<Boolean> getFinishMessage() {
        return showFinishMessage;
    }

    public PetsRepository(Application application) {
        loadJSONUseCase = new LoadJSONUseCase();
        updateListUseCase = new UpdateListUseCase();
    }

    public void useCaseLoadJSON() {
        loadJSONUseCase.loadJSON(new RepositoryInterface() {
            @Override
            public void showError(Boolean show) {
                showFinishMessage.postValue(show);
            }

            @Override
            public void onLoading(boolean loading) {
                loadingLiveData.postValue(loading);
            }

            @Override
            public void onSuccess(ArrayList<PetsDTO> data) {
                allPetsLiveData.postValue(data);
            }
        });
    }

    public void useCaseUpdateList(ArrayList<PetsDTO> pets) {
        updateListUseCase.updateList(pets, new RepositoryInterface() {
            @Override
            public void showError(Boolean show) {
                showFinishMessage.postValue(show);
            }

            @Override
            public void onLoading(boolean loading) {
                loadingLiveData.postValue(loading);
            }

            @Override
            public void onSuccess(ArrayList<PetsDTO> data) {
                allPetsLiveData.postValue(pets);
            }
        });
    }

}

这是我的用例。使用翻新功能读取JSON数据:

package com.example.menunavegacion.ui.fragments.usecase;

....

public class LoadJSONUseCase {

    @Inject
    RequestInterface requestInterface;

    public LoadJSONUseCase(){
        DaggerComponenTest.builder().build().inject(this);
    }

    public void loadJSON(RepositoryInterface repositoryInterface) {
        Call<JSONResponse> call = requestInterface.getJSON();
        repositoryInterface.onLoading(true);
        call.enqueue(new Callback<JSONResponse>() {
            @Override
            public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
                repositoryInterface.onLoading(false);
                JSONResponse jsonResponse = response.body();
                ArrayList<PetsDTO> data = new ArrayList<>();
                data.clear();
                data.addAll(jsonResponse.getPetsDTO());
                repositoryInterface.onSuccess(data);
            }

            @Override
            public void onFailure(Call<JSONResponse> call, Throwable t) {
               repositoryInterface.onLoading(false);
               repositoryInterface.showError(false);
            }
        });

    }
}

这就是我的全部代码。我正在学习的同时,我也接受有关MVVM的建议,不客气,所以我将与您在一起。非常感谢。

1 个答案:

答案 0 :(得分:0)

关于生命周期和维护复选框状态。因此,此链接将帮助您更多 Android CheckBox -- Restoring State After Screen Rotation