Android (1) teaches you how to use pull-down refresh and Android pull-down

Source: Internet
Author: User

Android (1) teaches you how to use pull-down refresh and Android pull-down

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.

    <string name="pull_to_refresh_pull_label">Pull to refresh...</string>    <string name="pull_to_refresh_release_label">Release to refresh...</string>    <string name="pull_to_refresh_refreshing_label">Loading...</string>    <string name="pull_to_refresh_tap_label">Tap to refresh...</string>

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.


、<pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <pulltorefresh.PullToRefreshListView        android:id="@+id/ptrlv"        android:layout_width="fill_parent"        android:layout_height="fill_parent">    </pulltorefresh.PullToRefreshListView></RelativeLayout>

 
<Pre name = "code" class = "html"> 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 and select copy qualifiled Name <span style = "font-size: 18px; "> start to write java code. </span> since it is defined in xml, it must be instantiated in java code, <pre name = "code" class = "java"> 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. vie WGroup; 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. lay Out. activity_main); pullToRefreshListView = (PullToRefreshListView) findViewById (R. id. ptrlv); // obtain a ListView lv = pullToRefreshListView from LinearLayout. getRefreshableView (); // fill ArrayAdapter with data for ListView <String> adapter = new ArrayAdapter <String> (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, and display // fake: 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, * It is also the onPostExecute () parameter type. Generally: String byte [] Bitmap */class MyAsyncT extends AsyncTask <String, Integer, String >{@ 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 );}}}
In the next chapter, refresh the drop-down list, pull, load, and write more together.
Click to download source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.