Search code examples
javaandroidswiperefreshlayout

SwipeRefreshLayout with ViewPager


I'm trying to implement swipe refresh on my app but as i am new to java I'm a bit lost about where i should put what

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewpager_fragments"
            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.support.v4.widget.SwipeRefreshLayout>

so you can see my xlm code for the swipe refresh and also here is my main activity code for the same, can anyone tell me what to put here

mySwipeRefreshLayout.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout");

                        // This method performs the actual data-refresh operation.
                        // The method calls setRefreshing(false) when it's finished.
                        myUpdateOperation();
                    }
                }
        );

Solution

  • Ref : Journal Dev Article on Swipe Refresh

    
        mySwipeRefreshLayout.setOnRefreshListener(
                        new SwipeRefreshLayout.OnRefreshListener() {
                            @Override
                            public void onRefresh() {
                        Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout");
                        shuffle(); // the function that populates your view 
                        mSwipeRefreshLayout.setRefreshing(false);
                                myUpdateOperation();
                            }
                        }
                );
    
    

    The shuffle can be a function like

    public void shuffle(){
            Collections.shuffle(arrayList, new Random(System.currentTimeMillis()));
            ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
            mListView.setAdapter(adapter);
        }