Android MVP架构 - 存储库和视图之间的通信

时间:2016-12-28 13:47:17

标签: android design-patterns mvp

我在here学习Android MVP架构。基于该示例,我创建了自己的简单应用程序,该应用程序仅显示带有recyclerview的列表以及提升刷新功能。

当拉出列表时,我想首先清除recyclerview,然后从存储库再次重新加载数据(具有伪延迟延迟)。但是,当我清除RecyclerViewAdpater中的数据时,存储库中的所有数据也被清除,并且没有任何显示。我无法理解原因。

这是我创建Presenter和View(即片段)的活动:

    protected void onCreate(Bundle savedInstanceState) {

            mainFragment = MainFragment.newInstance();

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.contentFrame, mainFragment);
            transaction.commit();

            new MainPresenter(new MainDataRepository(), mainFragment);
    }

这里是我的片段(MainFragment) - 注意: updateList(null) - 这是清除所有数据的地方,包括内部存储库:

    public MainFragment() {
    }

    public static MainFragment newInstance() {
        return new MainFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //....

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                recyclerAdapter.updateList(null); // **This is where it clears all data, including inside Repository**

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPresenter.loadList(); // to reload the list
                    }
                }, 1500);
            }
        });

        //...
    }

    @Override
    public void onResume() {
        super.onResume();

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPresenter.loadList();
            }
        }, 3000);
    }

    @Override
    public void setPresenter(MainContractor.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showList(List<String> mainDataList) {
        if(recyclerAdapter == null) {
            recyclerAdapter = new RecyclerAdapter(mainDataList);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.setAdapter(recyclerAdapter);
        }else{
            recyclerAdapter.updateList(mainDataList);
        }
    }

这是我的主持人(MainPresenter):

    public MainPresenter(MainDataRepository mainDataRepository, MainContractor.View view){
        mMainDataRepository = mainDataRepository;
        mMainContractorView = view;

        view.setPresenter(this);
    }

    @Override
    public void loadList() {
        ArrayList<String> strings = mMainDataRepository.getList();

        mMainContractorView.showList(strings);
    }

这是我的存储库(MainDataRepository):

private ArrayList<String> repositoryList;

    public MainDataRepository() {
        createList();
    }

    private void createList() {
        if(repositoryList == null){
            repositoryList = new ArrayList<>();
        }

        for (int i = 1; i <= 10; i++) {
            repositoryList.add("Item " + i);
        }
    }

    public ArrayList<String> getList(){
        return repositoryList;
    }

最后一个,这就是我在RecyclerAdapter中更新recyclelerview的方式:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

    private List<String> stringsList;

    public RecyclerAdapter(List<String> stringsList) {
        this.stringsList = stringsList;
    }

    public void updateList(List<String> newStrings){
            if(newStrings != null){
                stringsList = newStrings;
            }else{
                stringsList.clear();
            }
            notifyDataSetChanged();
        }

       //....
}

为什么在RecyclerAdapter中更新list mehod还会清除ArrayList<String> repositoryList存储库中的数据?

1 个答案:

答案 0 :(得分:1)

这是因为你引用了同一个对象。 在这里从Repository返回列表,更好的选择是返回它的副本:

public ArrayList<String> getList(){
    return new ArrayList<>(repositoryList);
}

然后它将是复制项目的另一个对象。