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> ...

Google Map v2 signed map isssue fixed

Hi Step 1: Develop Map Application  using android studio Step 2: For Map viewing we need to have google api key .in order to get api key we need to register application in google api console  https://console.developers.google.com/apis/ Step 3; For getting SHA1 Key please follow the steps            1) Go the java installed folder location , open cmd                 Eg: C:\Program Files\Java\jdk1.7.0_04\bin            2)  Type                   Windows keytool -list -v -keystore "CHANGE WITH OUR KEYSTORE LOC" -alias ALIASNAME -storepass PASSWORD -keypass PASSWORD                   Mac & Linux  keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android Ref:  http://www.truiton.com/2015/04/obtaining-sha1...

Store Image to Google Cloud Store using Google App engine java

Here is sample code for uploading image files to cloud storage bucket in google cloud platform Step 1 Create a google cloud project in cloud (https://console.cloud.google.com/)  enable cloud storage Step 2: Create a new web application project I am using Eclipse with Java for App engine Project Step 3: create a POST web service method the below code for upload image to the existing bucket in google cloud and the uploaded image url will be returned Code   @Path("/addres")  @POST       @Consumes(MediaType.MULTIPART_FORM_DATA)    public String addRestaurant( @FormDataParam("imgname")  String imagename,            @FormDataParam("file") InputStream uploadedInputStream,         @FormDataParam("file") FormDataContentDisposition restImages     ) throws IOException  {   try { // Create your service object Storage storage = St...