So I've implemented swipeRefresh Layout in my app but when I refresh it duplicates my items... so this is my xlm code
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/ad_banner_container"
android:layout_below="@id/tabs">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager_fragments"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.viewpager.widget.ViewPager>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
and above my java code
SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swiperefresh);
swipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadFragments();
// This line is important as it explicitly
// refreshes only once
// If "true" it implicitly refreshes forever
swipeRefreshLayout.setRefreshing(false);
}
}
);
and so the fragments load here
private void loadFragments(){
fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
vp_fragments.setAdapter(fragmentAdapter);
tabLayout.setupWithViewPager(vp_fragments);
Inside on refresh clear the list or use set to keep unique elements, set will take less time complexity however for non primitive datatype you need to use comparable or comparator interface, LinkedHashSet maintains insertion order of elements. i.e elements are placed as they are inserted.