Android SwipeRefreshLayout with Recyclerview example

Android SwipeRefreshLayout should be used whenever the user can refresh the contents of a view via a vertical swipe gesture. The activity that instantiates this view should add an OnRefreshListener to be notified whenever the swipe to refresh gesture is completed.

The SwipeRefreshLayout will notify the listener each and every time the gesture is completed again; the listener is responsible for correctly determining when to actually initiate a refresh of its content. If the listener determines there should not be a refresh, it must call setRefreshing(false) to cancel any visual indication of a refresh.

If an activity wishes to show just the progress animation, it should call setRefreshing(true). To disable the gesture and progress animation, call setEnabled(false) on the view.

Swipe To RefreshLayout

 

swipe refresh layout download

Let’s getting started to coding part.

1. Add Recyclerview and Cardview dependencies to your app build.gradle file.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    implementation 'com.android.support:cardview-v7:26.1.0'
}

Related: Android recyclerview material design with Example [Download]
Related: Cardview Android Material Design with Example [Download]


2. Adding SwipeRefreshLayout and Recyclerview to activity_main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/SwipeRefreshLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout> 
</RelativeLayout>

3. Initialize the SwipeRefreshLayout in your MainActivity.java

SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.SwipeRefreshLayout);
swipeRefreshLayout.setColorScheme(android.R.color.holo_blue_light);

4. Initialize the Recyclerview,

recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerviewAdapter = new RecyclerviewAdapter();
recyclerView.setAdapter(recyclerviewAdapter);

RecyclerviewAdapter.java

public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.MyViewHolder> {

    private List<User> userList;

    public void setUserList(List<User> userList) {
        this.userList = userList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerviewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_adapter,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerviewAdapter.MyViewHolder holder, int position) {
        holder.tvTitle.setText(userList.get(position).getName());
    }

    @Override
    public int getItemCount() {
        if(userList != null){
            return userList.size();
        }
        return 0;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.title);
        }
    }
}

5. Add Default data to Recyclerview.

userList = new ArrayList<>();
User user = new User("User 1",R.drawable.velmm_icon);
userList.add(user);
user = new User("User 2",R.drawable.velmm_icon);
userList.add(user);
recyclerviewAdapter.setUserList(userList);

6. Setup SwipeRefreshLayout

Add setOnRefreshListener() to add new Items or Call the REST API’s when your screen got swiped.

swipeRefreshLayout.setOnRefreshListener(refreshListener);
private OnRefreshListener refreshListener = new OnRefreshListener() {
   
   @Override
   public void onRefresh() {
      swipeRefreshLayout.setRefreshing(true);
      i = i + 1;
      User user = new User("User "+i,R.drawable.ic_launcher);
      userList.add(user);
      recyclerviewAdapter.setUserList(userList);
      swipeRefreshLayout.setRefreshing(false);
   }
};

Use setRefreshing(true) to show the Refreshing view at the Top of the screen.

swipeRefreshLayout.setRefreshing(true);

Use setRefreshing(false) to show the Refreshing view at the Top of the screen.

swipeRefreshLayout.setRefreshing(false);

Add Time Delay to Hide the Refreshing view.

SwipeRefreshLayout

 

new Handler().postDelayed(new Runnable() {
    @Override public void run() {
    // TODO Auto-generated method stub
        swipeRefreshLayout.setRefreshing(false);
    }
    },3000);

7. You can use Handler And Runnable to Automatically call the SwipeRefreshLayout after the particular Time interval.

Handler handler = new Handler();
handler.post(refreshers);
private OnRefreshListener refreshListener = new OnRefreshListener() {
   
   @Override
   public void onRefresh() {
      swipeRefreshLayout.setRefreshing(true);
      i = i + 1;
      User user = new User("User "+i,R.drawable.ic_launcher);
      userList.add(user);
      recyclerviewAdapter.setUserList(userList);
      swipeRefreshLayout.setRefreshing(false);
   }
};

Read Also: Android Snackbar Material design with Example [Step By Step]


 

swipe refresh layout download

Leave a Reply

Your email address will not be published. Required fields are marked *