Android (1) How to Use pull-down refresh
I haven't used open-source projects for a long time. I want to review them and refresh them. I also want to summarize the next project. I need those things to lay the foundation for myself. You can also go to the library by yourself.
One Class of pulltorefresh (pull-down refresh) is PullToRefreshListView. Because I only talk about pull-down refresh, only a part of the class is taken. Copy a package
Paste it into your project
Note that this class also requires some resource files:
Layout: layout displayed in the header of header. xml pull_to_refresh_header.xml when refreshing. If your boss doesn't like the layout, you can customize it by yourself.
Drawable-hdpi: the directory contains two pulltorefresh_down_arrow.png pulltorefresh_up_arrow.png images. A drop-down to a distance appears
You also need a res/values/attrs. xml custom tag
There are also res/values/Strings. xml
Here, you can edit the text displayed in the drop-down list.
Pull to refresh...
Release to refresh...
Loading...
Tap to refresh...
In fact, you can understand this open-source project as a linear layout, because it has a built-in Listview, just like ListFagment.
I believe everyone has already built the project. As I have already said above, it can be regarded as a linear layout. so you can use it as a linear layout and write the layout first.
、
Pulltorefresh. pullToRefreshListView is the full Name of the PullToRefreshListView class. To avoid errors, directly copy the package Name and open the PullToRefreshListView subnode. Right-click the subnode and select copy qualifiled Name to start writing java code. Since it is defined in, it must be instantiated in java code,
private PullToRefreshListView pullToRefreshListView; pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.ptrlv);
Then get a listview through getRefreshableView (); and then fill the data with the adapter
Then there is a method for pulling down and refresh the setOnRefreshListener drop-down listener. Generally, new network data is requested, parsed, and displayed.
All code
Package com. example. day24; import pulltorefresh. pullToRefreshBase. onRefreshListener; import pulltorefresh. pullToRefreshListView; import android. app. activity; import android. app. actionBar; import android. app. fragment; import android. OS. asyncTask; import android. OS. bundle; import android. view. layoutInflater; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. viewGroup; import android. widget. arrayAdapter; import android. widget. listView; import android. OS. build; public class MainActivity extends Activity {private PullToRefreshListView pullToRefreshListView;/** will be instantiated as the ListView */private ListView lv obtained from custom LinearLayout (PullToRefreshListView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); pullToRefreshListView = (PullToRefreshListView) findViewById (R. id. ptrlv); // obtain a ListView lv = pullToRefreshListView from LinearLayout. getRefreshableView (); // fill ArrayAdapter with data for ListView
Adapter = new ArrayAdapter
(MainActivity. this, android. r. layout. simple_list_item_1, initData (); lv. setAdapter (adapter); pullToRefreshListView. setOnRefreshListener (new OnRefreshListener () {@ Overridepublic void onRefresh () {// refresh operation. Generally, requests for new network data, parsing, display, and spoofing are asynchronous. if you want to pass a parameter to doInBackground (), you can pass the parameter new myasynct(cmd.exe cute () ;}}) ;}// false data private String [] initData () in listview () {String [] datas = new String [10]; for (int I = 0; I <datas. length; I ++) {datas [I] = item + I;} return datas;}/** generic 1: doInBackground () parameter ::: string url * generic 2: onProgressUpdate () parameter: Integer progress * generic 3: doInBackground () return value type, * Also onPostExecute () parameter type usually: string byte [] Bitmap */class MyAsyncT extends AsyncTask
{@ Overrideprotected String doInBackground (String... params) {try {Thread. sleep (2000);} catch (InterruptedException e) {e. printStackTrace ();} return null;} @ Overrideprotected void onPostExecute (String result) {// the asynchronous task is completed. Tell the PullToRefreshListView task to complete pullToRefreshListView. onRefreshComplete (); super. onPostExecute (result );}}}