Search code examples
androidfragmentswiperefreshlayout

How to control ViewPager with a fragment with SwipeRefreshLayout


In my app I use FragmentStatePagerAdapter with one fragment , I use it 3 times in the adapter. In my Fragment I have a SwipeRefreshLayout and a Retrofit . My problem is , When I swipe between pages and use the SwipRefresh, my content duplicated . How can I stop this duplication .

Thanks

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            if (!isNetworkAvailable()) {
                Toast.makeText(getActivity(), "No Internet", Toast.LENGTH_SHORT).show();
            } else if (isNetworkAvailable()) {
                if (flag) {
                    listDataHeader.clear();
                    adapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
                    expListView.setAdapter(adapter);
                    getData();
                    adapter.notifyDataSetChanged();
                } else {
                    listDataHeader.clear();
                    adapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
                    expListView.setAdapter(adapter);
                    getData();
                    adapter.notifyDataSetChanged();
                }
            }
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    swipeRefreshLayout.setRefreshing(false);
                }

            }, 4000);
        }
    });

Solution

  • when you get data using this getData(); that time arraylist or list will clear then fresh response bind into list or arraylist.