Showing posts with label swiperefreshlayout. Show all posts
Showing posts with label swiperefreshlayout. Show all posts
Thursday, September 28, 2017
SwipeRefreshLayout work with RecyclerView
SwipeRefreshLayout work with RecyclerView

Last two post show "Simple example of using SwipeRefreshLayout" and "SwipeRefreshLayout, refresh in background thread", target to ListView.
This example show how SwipeRefreshLayout work with RecyclerView (reference: step-by-step of using RecyclerView).
To use RecyclerView in your Android Studio project, you have to Add Support Libraries of RecyclerView as dependencies.

Create layout/layout_item.xml, to define the layout of RecyclerView item.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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 new 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;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ItemHolder>{
private List<String> itemsList;
private OnItemClickListener onItemClickListener;
private LayoutInflater layoutInflater;
public RecyclerViewAdapter(Context context){
layoutInflater = LayoutInflater.from(context);
itemsList = new ArrayList<String>();
}
@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.layout_item, parent, false);
return new ItemHolder(itemView, this);
}
@Override
public void onBindViewHolder(ItemHolder holder, int position) {
holder.setItemText(itemsList.get(position));
}
@Override
public int getItemCount() {
return itemsList.size();
}
public void setOnItemClickListener(OnItemClickListener listener){
onItemClickListener = listener;
}
public OnItemClickListener getOnItemClickListener(){
return onItemClickListener;
}
public interface OnItemClickListener{
public void onItemClick(ItemHolder item, int position);
}
public void add(int location, String iString){
itemsList.add(location, iString);
notifyItemInserted(location);
}
public void set(int location, String iString){
itemsList.set(location, iString);
notifyItemChanged(location);
}
public void clear(){
itemsList.clear();
notifyDataSetChanged();
}
public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private RecyclerViewAdapter parent;
TextView textItemText;
public ItemHolder(View itemView, RecyclerViewAdapter parent) {
super(itemView);
itemView.setOnClickListener(this);
this.parent = parent;
textItemText = (TextView) itemView.findViewById(R.id.item_text);
}
public void setItemText(CharSequence itemString){
textItemText.setText(itemString);
}
public CharSequence getItemText(){
return textItemText.getText();
}
@Override
public void onClick(View v) {
final OnItemClickListener listener = parent.getOnItemClickListener();
if(listener != null){
listener.onItemClick(this, getAdapterPosition());
}
}
}
}
Modify layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="http://android-er.blogspot.com/"
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;
public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter.OnItemClickListener{
SwipeRefreshLayout swipeRefreshLayout;
Button btnClearAll;
private RecyclerView myRecyclerView;
private LinearLayoutManager linearLayoutManager;
private 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
public void onRefresh() {
refresh();
}
});
btnClearAll = (Button)findViewById(R.id.clearall);
btnClearAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//In order to prevent the racing condition of updating removed item in refreshing,
//disable "Clear All" if refreshing
if(!swipeRefreshLayout.isRefreshing()){
myRecyclerViewAdapter.clear();
}
}
});
myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);
linearLayoutManager =
new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
myRecyclerViewAdapter = new RecyclerViewAdapter(this);
myRecyclerViewAdapter.setOnItemClickListener(this);
myRecyclerView.setAdapter(myRecyclerViewAdapter);
myRecyclerView.setLayoutManager(linearLayoutManager);
}
private void refresh(){
final int pos = myRecyclerViewAdapter.getItemCount();
myRecyclerViewAdapter.add(pos, "Refreshing...");
swipeRefreshLayout.setRefreshing(true);
//refresh long-time task in background thread
new Thread(new Runnable() {
@Override
public void run() {
try {
//dummy delay for 2 second
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//update ui on UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
String currentDateTime =
DateFormat.getDateTimeInstance().format(new Date());
myRecyclerViewAdapter.set(pos, pos + " - " + currentDateTime);
swipeRefreshLayout.setRefreshing(false);
}
});
}
}).start();
}
@Override
public void onItemClick(RecyclerViewAdapter.ItemHolder item, int position) {
Toast.makeText(this,
position + " : " + item.getItemText(),
Toast.LENGTH_SHORT).show();
}
}
download file now
Labels:
recyclerview,
swiperefreshlayout,
with,
work
Sunday, September 3, 2017
Swipe to Refresh ListView using SwipeRefreshLayout
Swipe to Refresh ListView using SwipeRefreshLayout

With android.support.v4.widget.SwipeRefreshLayout, user can refresh the contents of a view via a vertical swipe gesture. This example show how to implement Swipe-to-Refresh ListView using SwipeRefreshLayout.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="http://android-er.blogspot.com/"
android_textStyle="bold" />
<android.support.v4.widget.SwipeRefreshLayout
android_id="@+id/swiperefreshlayout"
android_layout_height="match_parent"
android_layout_width="match_parent">
<ListView
android_id="@+id/swipelist"
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.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
SwipeRefreshLayout swipeRefreshLayout;
ListView swipeList;
List<String> myList;
ListAdapter adapter;
@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
public void onRefresh() {
refresh();
}
});
swipeList = (ListView)findViewById(R.id.swipelist);
myList = new ArrayList<>();
adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, myList);
swipeList.setAdapter(adapter);
}
private void refresh(){
//insert dummy string of current date/time
String currentDateTime = DateFormat.getDateTimeInstance().format(new Date());
myList.add(currentDateTime);
swipeList.invalidateViews();
swipeRefreshLayout.setRefreshing(false);
}
}
In this example, the ListView items is updated in SwipeRefreshLayout.OnRefreshListener(). But normally refreshing involve longtime task, such as loading from Internet. The next example show how to refresh with longtime task run in background thread.
download file now
Thursday, August 24, 2017
SwipeRefreshLayout refresh in background thread
SwipeRefreshLayout refresh in background thread

Last post show a basic example of SwipeRefreshLayout. But, normally refreshing involve some longtime task; such as loading data from Internet. This example show how to refresh with longtime task in background thread.
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.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
SwipeRefreshLayout swipeRefreshLayout;
ListView swipeList;
List<String> myList;
ListAdapter adapter;
@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
public void onRefresh() {
refresh();
}
});
swipeList = (ListView)findViewById(R.id.swipelist);
myList = new ArrayList<>();
adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, myList);
swipeList.setAdapter(adapter);
swipeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = (String) parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this, item, Toast.LENGTH_LONG).show();
}
});
}
private void refresh(){
final int pos = myList.size();
myList.add(pos, "Refreshing...");
swipeList.invalidateViews();
swipeRefreshLayout.setRefreshing(true);
//refresh long-time task in background thread
new Thread(new Runnable() {
@Override
public void run() {
try {
//dummy delay for 2 second
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//update ui on UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
String currentDateTime =
DateFormat.getDateTimeInstance().format(new Date());
myList.set(pos, pos + " - " + currentDateTime);
swipeList.invalidateViews();
swipeRefreshLayout.setRefreshing(false);
}
});
}
}).start();
}
}
Keep using the layout in last post.
more:
- SwipeRefreshLayout, work with RecyclerView
download file now
Labels:
background,
in,
refresh,
swiperefreshlayout,
thread
Subscribe to:
Posts (Atom)