Swiperefreshlayout, Live On Amongst Recyclerview

Posted on

Last 2 postal service demonstrate “Simple instance of using SwipeRefreshLayout” too “SwipeRefreshLayout, refresh inwards background thread“, target to ListView.

This instance demonstrate how SwipeRefreshLayout operate alongside RecyclerView (reference: step-by-step of using RecyclerView).


To purpose RecyclerView inwards your Android Studio project, you lot bring to Add Support Libraries of RecyclerView every bit dependencies.

Create layout/layout_item.xml, to define the layout of RecyclerView item.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="vertical">      <TextView         android:id="@+id/item_text"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textSize="28dp"/>  </LinearLayout> 

Create a novel class, RecyclerViewAdapter.java.

package com.blogspot.android_er.androidswiperefresh;  import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;  import java.util.ArrayList; import java.util.List;  world shape RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ItemHolder>{      someone List<String> itemsList;     someone OnItemClickListener onItemClickListener;     someone LayoutInflater layoutInflater;      world RecyclerViewAdapter(Context context){         layoutInflater = LayoutInflater.from(context);         itemsList = novel ArrayList<String>();     }      @Override     world ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {         View itemView = layoutInflater.inflate(R.layout.layout_item, parent, false);         provide novel ItemHolder(itemView, this);     }      @Override     world void onBindViewHolder(ItemHolder holder, int position) {         holder.setItemText(itemsList.get(position));     }      @Override     world int getItemCount() {         provide itemsList.size();     }      world void setOnItemClickListener(OnItemClickListener listener){         onItemClickListener = listener;     }      world OnItemClickListener getOnItemClickListener(){         provide onItemClickListener;     }      world interface OnItemClickListener{         world void onItemClick(ItemHolder item, int position);     }      world void add(int location, String iString){         itemsList.add(location, iString);         notifyItemInserted(location);     }      world void set(int location, String iString){         itemsList.set(location, iString);         notifyItemChanged(location);     }      world void clear(){         itemsList.clear();         notifyDataSetChanged();     }      world static shape ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{          someone RecyclerViewAdapter parent;         TextView textItemText;          world ItemHolder(View itemView, RecyclerViewAdapter parent) {             super(itemView);             itemView.setOnClickListener(this);             this.parent = parent;             textItemText = (TextView) itemView.findViewById(R.id.item_text);         }          world void setItemText(CharSequence itemString){             textItemText.setText(itemString);         }          world CharSequence getItemText(){             provide textItemText.getText();         }          @Override         world void onClick(View v) {             concluding OnItemClickListener listener = parent.getOnItemClickListener();             if(listener != null){                 listener.onItemClick(this, getAdapterPosition());             }         }     } }  

Modify layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidswiperefresh.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <Button         android:id="@+id/clearall"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Clear All"/>      <android.support.v4.widget.SwipeRefreshLayout         android:id="@+id/swiperefreshlayout"         android:layout_height="match_parent"         android:layout_width="match_parent">          <android.support.v7.widget.RecyclerView             android:id="@+id/myrecyclerview"             android:layout_width="match_parent"             android:layout_height="match_parent"/>      </android.support.v4.widget.SwipeRefreshLayout>  </LinearLayout>  

MainActivity.java

package com.blogspot.android_er.androidswiperefresh;  import android.os.Bundle; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Button; import android.widget.Toast;  import java.text.DateFormat; import java.util.Date;  world shape MainActivity extends AppCompatActivity implements RecyclerViewAdapter.OnItemClickListener{      SwipeRefreshLayout swipeRefreshLayout;     Button btnClearAll;      someone RecyclerView myRecyclerView;     someone LinearLayoutManager linearLayoutManager;     someone RecyclerViewAdapter myRecyclerViewAdapter;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swiperefreshlayout);         swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {             @Override             world void onRefresh() {                 refresh();             }         });         btnClearAll = (Button)findViewById(R.id.clearall);         btnClearAll.setOnClickListener(new View.OnClickListener() {             @Override             world void onClick(View v) {                 //In social club to forbid the racing status of updating removed detail inwards refreshing,                 //disable "Clear All" if refreshing                 if(!swipeRefreshLayout.isRefreshing()){                     myRecyclerViewAdapter.clear();                 }             }         });          myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);         linearLayoutManager =                 novel LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);         myRecyclerViewAdapter = novel RecyclerViewAdapter(this);         myRecyclerViewAdapter.setOnItemClickListener(this);         myRecyclerView.setAdapter(myRecyclerViewAdapter);         myRecyclerView.setLayoutManager(linearLayoutManager);      }      someone void refresh(){          concluding int pos = myRecyclerViewAdapter.getItemCount();         myRecyclerViewAdapter.add(pos, "Refreshing...");         swipeRefreshLayout.setRefreshing(true);          //refresh long-time chore inwards background thread         novel Thread(new Runnable() {             @Override             world void run() {                 endeavor {                     //dummy delay for 2 minute                     Thread.sleep(2000);                 } grab (InterruptedException e) {                     e.printStackTrace();                 }                  //update ui on UI thread                 runOnUiThread(new Runnable() {                     @Override                     world void run() {                         String currentDateTime =                                 DateFormat.getDateTimeInstance().format(new Date());                          myRecyclerViewAdapter.set(pos, pos + " - " + currentDateTime);                         swipeRefreshLayout.setRefreshing(false);                     }                 });              }         }).start();     }      @Override     world void onItemClick(RecyclerViewAdapter.ItemHolder item, int position) {         Toast.makeText(this,                 seat + " : " + item.getItemText(),                 Toast.LENGTH_SHORT).show();     } }  

Simple instance of using SwipeRefreshLayout SwipeRefreshLayout, operate alongside RecyclerViewDownload the files .