Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to refresh your list when you swipe down at the top / use of SwipeRefreshLayout

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 19
    Comment on it

    Sometime we need to refresh our list when we swipe down at top of our list so this feature is attained with the help of Swipe Refresh Layout. Here I am showing the list with the help of recyclerView, so we need to add SwipeRefreshLayout tag in our xml file, below is the code.

    my_layout.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#DFEEED"
        tools:context="com.android.Fragment.Myfrag">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
         
    
        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
    
            android:layout_marginTop="@dimen/margin50"
            android:id="@+id/nudge_recyclerview"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_marginLeft="@dimen/margin10"
            android:layout_marginRight="@dimen/margin10"/>
    
        </android.support.v4.widget.SwipeRefreshLayout>
        </LinearLayout>
    </FrameLayout>

    Now below is the code for the activity where I have implemented recyclerView with SwipeRefreshLayout and its functionality, so here I have called the api from where I am getting the data that is to be fetched in recyclerView within the swiperefreshlayout.setOnRefreshListener.

    MyActivity.java

    public class YourNudges extends Fragment implements NudgeRecyclerViewAdapter.OnClickListener, MyPagerAdapter.Updateable {
        
        private RecyclerView mNudgesRView;
        private RecyclerView.Adapter mAdapter;
        private LinearLayoutManager mLayoutManager;
        private SwipeRefreshLayout swipeRefreshLayout;
        private Handler mHandler;
        
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_your_nudges, container, false);
            swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
    
    
    // BELOW LINE MAKES THE LIST REFRESH WHEN YOU SWIPE AT TOP
    
    
            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    
                        getTheMessageAPI();     
                }
            });
                getTheMessageAPI();
                return view;
        }
    
     
        public void getTheMessageAPI() {
           //Fetch your data in recyclerview over here
        }
    
        private void setRecyclerAdapter() {
    
    // set your recyclerView Adapter over here
            
        }
      
    }
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: