簡體   English   中英

實施滑動以在RecyclerView上刷新

[英]Implementing swipe to refresh on RecyclerView

我試圖將SwipeRefreshLayout添加到RecyclerView 我怎么做? 這是我的代碼,但無法正常工作:

public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    View consultantView = inflater.inflate(R.layout.fragment_consultant, container, false);

    swipeContainer = (SwipeRefreshLayout) consultantView.findViewById(R.id.consultant_recyclerview);
    retrieveConsultantList();
    swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeContainer.setRefreshing(false);
            retrieveConsultantList();
            practiceSpinner.setAdapter(consultantListAdapter);
            consultantListAdapter.notifyDataSetChanged();
        }
    });
}

而我的回收商代碼:

private void setUpConsultantRecyclerView(List<Consultant>  consultantList) {
    ConsultantRecylerViewAdapter consultantRecylerViewAdapter = new ConsultantRecylerViewAdapter(getContext(), consultantList);
    consultantRecyclerView.setAdapter(consultantRecylerViewAdapter);

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    consultantRecyclerView.setLayoutManager(linearLayoutManager);

    consultantRecyclerView.setItemAnimator(new DefaultItemAnimator());
}

請幫我解決這個問題。

試試這個變化,希望能對您有所幫助

//move adapter to global variable
ConsultantRecylerViewAdapter consultantRecylerViewAdapter ;

public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                     Bundle savedInstanceState) {
View consultantView = inflater.inflate(R.layout.fragment_consultant, container, false);
    swipeContainer = (SwipeRefreshLayout) consultantView.findViewById(R.id.consultant_recyclerview);

    //move to here
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    consultantRecyclerView.setLayoutManager(linearLayoutManager);

    //declare list first
    consultantList  = new ArrayList<>();
    consultantRecylerViewAdapter  = new ConsultantRecylerViewAdapter(getContext(), consultantList);
    consultantRecyclerView.setAdapter(consultantRecylerViewAdapter);

    retrieveConsultantList();

    swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeContainer.setRefreshing(false);
            retrieveConsultantList();
            practiceSpinner.setAdapter(consultantListAdapter);
            consultantListAdapter.notifyDataSetChanged();
        }
    });
}

private void retrieveConsultantList(){
    //put this code when finish load data from server
    setUpConsultantRecyclerView(consultantListFromServer)
}

//this to refresh your RecyclerView
private void setUpConsultantRecyclerView(List<Consultant>  consultantList) {
    //clear old list
    consultantList.clear();
    //add new collection to list
    consultantList.addAll(consultantList);
    //refresh adapter
    consultantRecyclerView.notifyDataSetChanged();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM