简体   繁体   中英

Implementing swipe to refresh on RecyclerView

I am trying to add a SwipeRefreshLayout to a RecyclerView . How do I do that? This is the code I have but it is not working:

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

And my recycler code:

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

Please help me figure this out.

Try this changes, hopefully can help you

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM