简体   繁体   中英

Swipe to Refresh Layout android

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. ie elements are placed as they are inserted.

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