Skip to main content

Simple Pull to refresh ListView

main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
     
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ListView android:id="@id/android:list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:drawSelectorOnTop="false"/>
    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>


Main activity 


private SwipeRefreshLayout swipeRefreshLayout;

private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getCountries());
    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);
    // find the layout
    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    // the refresh listner. this would be called when the layout is pulled down
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
         
        @Override
        public void onRefresh() {
            // get the new data from you data source
          
            handler.post(refreshing);
        }
    });
    // sets the colors used in the refresh animation
    swipeRefreshLayout.setColorScheme(R.color.blue_bright, R.color.green_light,
            R.color.orange_light, R.color.red_light);
}
private final Runnable refreshing = new Runnable(){
    public void run(){
        try {
            if(isRefreshing()){
                // re run the verification after 1 second
                handler.postDelayed(this, 1000);  
            }else{
                // stop the animation after the data is fully loaded
                swipeRefreshLayout.setRefreshing(false);
                // TODO : update your list with the new data
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }  
    }
};



Add latest support_v4 library library files


Comments

Post a Comment

Popular posts from this blog

Bluetooth Chat Example

Manifest File <manifest xmlns:android="http://schemas.android.com/apk/res/android"       package="com.example.android.BluetoothChat"       android:versionCode="1"       android:versionName="1.0">     <uses-sdk minSdkVersion="7" />     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />     <uses-permission android:name="android.permission.BLUETOOTH" />     <application android:label="@string/app_name"                  android:icon="@drawable/app_icon" >         <activity android:name=".BluetoothChat"                   android:label="@string/app_name"                   android:configChanges="orientation|keyboardHidden">             <intent-filter> ...

Custom TextView With Images in Android

Create a custom XML Tag with image and text 1) Class extends Textview import java . util . regex . Matcher ; import java . util . regex . Pattern ; import android . content . Context ; import android . text . Spannable ; import android . text . style . ImageSpan ; import android . util . AttributeSet ; import android . util . Log ; import android . widget . TextView ; public class TextViewWithImages extends TextView { public TextViewWithImages ( Context context , AttributeSet attrs , int defStyle ) { super ( context , attrs , defStyle ); } public TextViewWithImages ( Context context , AttributeSet attrs ) { super ( context , attrs ); } public TextViewWithImages ( Context context ) { super ( context ); } @Override public void setText ( CharSequence text , BufferType type ) { Spannable s = getTextWithImages ( getContext (), text ); super . setText...

AcceleroMeter Sensors with SurfaceMovements on Canvas

MainActivity.java import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class MainActivity extends Activity implements SensorEventListener { float x, y, sensorX, sensorY; Bitmap ball; SensorManager sm; Sensor s; MyBringBackSurface ourSurfaceView; /**  * Canvas Movement  */ public class MyBringBackSurface extends SurfaceView implements Runnable {     SurfaceHolder ourHolder;     Thread ourThread = null;     boolean isRunning = false;     public MyBringBackSurface(Context context) {         super(context)...