Swiperefreshlayout, Refresh Inwards Background Thread

Posted on

Last postal service present a basic illustration of SwipeRefreshLayout. But, commonly refreshing involve to a greater extent than or less longtime task; such every bit loading information from Internet. This illustration present how to refresh amongst longtime trace of piece of job inwards 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;  populace cast 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             populace void onRefresh() {                 refresh();             }         });          swipeList = (ListView)findViewById(R.id.swipelist);          myList = novel ArrayList<>();         adapter = novel ArrayAdapter<String>(                 this, android.R.layout.simple_list_item_1, myList);         swipeList.setAdapter(adapter);          swipeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {             @Override             populace void onItemClick(AdapterView<?> parent, View view, int position, long id) {                 String detail = (String) parent.getItemAtPosition(position);                 Toast.makeText(MainActivity.this, item, Toast.LENGTH_LONG).show();             }         });     }      mortal void refresh(){          concluding int pos = myList.size();         myList.add(pos, "Refreshing...");         swipeList.invalidateViews();         swipeRefreshLayout.setRefreshing(true);          //refresh long-time trace of piece of job inwards background thread         novel Thread(new Runnable() {             @Override             populace void run() {                 assay {                     //dummy delay for two minute                     Thread.sleep(2000);                 } grab (InterruptedException e) {                     e.printStackTrace();                 }                  //update ui on UI thread                 runOnUiThread(new Runnable() {                     @Override                     populace void run() {                         String currentDateTime =                                 DateFormat.getDateTimeInstance().format(new Date());                         myList.set(pos, pos + " - " + currentDateTime);                         swipeList.invalidateViews();                         swipeRefreshLayout.setRefreshing(false);                     }                 });              }         }).start();     } }  

Keep using the layout inwards last post.

more:
– SwipeRefreshLayout, travel amongst RecyclerView