Android pulltorefresh, androidpulltorefresh
The pulltorefresh plug-in allows you to easily pull up and pull down and refresh, and search and download github.com directly.
Layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ptr="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pull_to_refresh_listview" android:layout_height="fill_parent" android:layout_width="fill_parent" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrAnimationStyle="flip" ptr:ptrHeaderBackground="@android:color/transparent" ptr:ptrHeaderTextColor="#919191" /></RelativeLayout>
The core code is as follows. Many knowledge points in Android are used here, mainly for asynchronous task processing and custom adapters.
Package com. example. pulltorefresh; import java. util. arrayList; import com. handmark. pulltorefresh. library. ILoadingLayout; import com. handmark. pulltorefresh. library. pullToRefreshBase; import com. handmark. pulltorefresh. library. pullToRefreshListView; import android. OS. asyncTask; import android. OS. bundle; import android. app. activity; import android. content. context; import android. view. layoutInflater; import andro Id. view. menu; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. textView; public class MainActivity extends Activity {private PullToRefreshListView pullToRefreshView; private ArrayList <Music> musics = new ArrayList <Music> (); private DataAdapter dataAdapter; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceSta Te); setContentView (R. layout. activity_main); pullToRefreshView = (PullToRefreshListView) findViewById (R. id. pull_to_refresh_listview); pullToRefreshView. setOnRefreshListener (new PullToRefreshBase. onRefreshListener2 () {@ Override public void onPullDownToRefresh (PullToRefreshBase refreshView) {// method stub automatically generated by TODO new effeccute () ;}@ Override public void pulonluptorefresh (PullToRefreshBase refreshView) {// method stub automatically generated by TODO new dataasynctask(mainactivity.this0000.exe cute () ;}}); pullToRefreshView. setMode (PullToRefreshBase. mode. BOTH); ILoadingLayout startLabels = pullToRefreshView. getLoadingLayoutProxy (true, false); startLabels. setPullLabel ("pull-down refresh... "); // The startLabels. setRefreshingLabel ("loading... "); // startLabels when refreshing. setReleaseLabel ("Release refresh... "); // displayed when the distance is reached Message: ILoadingLayout endLabels = pullToRefreshView. getLoadingLayoutProxy (false, true); endLabels. setPullLabel ("pull up to refresh... "); // The prompt" endLabels "is displayed when you pull down the page. setRefreshingLabel ("loading... "); // endLabels. setReleaseLabel ("Release refresh... "); // when it reaches a certain distance, the following prompt is displayed: loadData (); dataAdapter = new DataAdapter (this, musics); pullToRefreshView. setAdapter (dataAdapter) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inf Late the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;} private int count; private void loadData () {for (int I = 0; I <10; I ++) {musics. add (new Music ("Song-" + count, "singer-" + count); count ++ ;}} static class DataAsyncTask extends AsyncTask <Void, Void, string> {private MainActivity mainActivity; public DataAsyncTask (MainActivity mainActiv Ity) {this. mainActivity = mainActivity;} @ Override protected String doInBackground (Void... arg0) {try {Thread. sleep (2000);} catch (InterruptedException e) {// catch Block e automatically generated by TODO. printStackTrace ();} mainActivity. loadData (); return "success" ;}@ Override protected void onPostExecute (String result) {// method stubs automatically generated by TODO super. onPostExecute (result); if ("success ". equals (result) {mainActivity. dataA Dapter. notifyDataSetChanged (); mainActivity. pullToRefreshView. onRefreshComplete () ;}} static class DataAdapter extends BaseAdapter {private Context ctx; private ArrayList <Music> musics; public DataAdapter (Context ctx, ArrayList <Music> musics) {this. ctx = ctx; this. musics = musics;} @ Override public int getCount () {// return musics, the method stub automatically generated by TODO. size () ;}@ Override public Object getItem (int arg0) {// Return musics. get (arg0) ;}@ Override public long getItemId (int arg0) {// return arg0;} // ViewHolder is the class of the owner, there is usually no method in it. Only the attribute is used to store a temporary storage. // you can save the View returned every time in the getView method. The advantage of this is that you do not have to get your View in the layout file every time, improving the efficiency @ Override public View getView (int arg0, View arg1, ViewGroup arg2) {ViewHolder h; if (arg1 = null) {arg1 = LayoutInflater. from (ctx ). inflate (R. layout. listitem, null); vh = new ViewHolder (); vh. TV _title = (TextView) arg1.findViewById (R. id. gequ); FLAC. TV _singer = (TextView) arg1.findViewById (R. id. geshou); arg1.setTag (vh);} FLAC = (ViewHolder) arg1.getTag (); Music m = musics. get (arg0); Vl. TV _title.setText (m. getTitle (); Vl. TV _singer.setText (m. getSinger (); return arg1;} static class ViewHolder {TextView TV _title; TextView TV _singer ;}}}
With the Music class used in the core code, the custom layout used by the custom adapter is very simple. There are two textviews, which are omitted here.
package com.example.pulltorefresh;public class Music { private String title; private String singer; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSinger() { return singer; } public void setSinger(String singer) { this.singer = singer; } public Music(String title, String singer) { this.title = title; this.singer = singer; } public Music() { } }