Android drop-down refresh pull-up load effect function

Source: Internet
Author: User

Application Scenarios:

In app development, information acquisition and presentation. It is not possible to get all of them with a demo. In order to provide users with a friendly and convenient user experience, the requirement to dynamically load data with sliding and pull-down effects will occur. To do this, the effect function needs to be applied to the desired display page.

Knowledge Point Introduction:This article mainly according to open source project Android-pulltorefresh launches the introduction.
Android-pulltorefresh
"A powerful pull refresh open source project that supports various controls drop-down refreshes the ListView, Viewpager, Wevview, Expandablelistview, GridView, (horizontal) ScrollView, Fragment up and down around to pull the refresh, than the following Johannilsson that only support the ListView of the powerful more. And his implementation of the dropdown refresh ListView will not show a refresh prompt in the case of item less than one screen, the experience is better.

】
Project Address:
Https://github.com/chrisbanes/Android-PullToRefresh
Demo Address:
Https://github.com/Trinea/TrineaDownload/blob/master/pull-to-refreshview-demo.apk?raw=true

How to use:First step: Create a new Androidprojectsampledemo
Step Two: Create a new attrs.xml under Res/values
<? 

XML version= "1.0" encoding= "Utf-8"?

><resources> <declare-styleable name= "Pulltorefresh" > <attr name= "mode" format= "Reference" > <flag name= "pulldownfromtop" value= "0x1"/> <flag name= "Pullupfrombottom" value= "0x2" /> <flag name= "Both" value= "0x3"/> </attr> </DECLARE-STYLEABLE></RESOURCES&G T;srings.xml<?xml version= "1.0" encoding= "Utf-8"?

><resources> <string name= "app_name" >SampleDemo</string> <string name= "Action_settings" >Settings</string> <string name= "Pull_to_refresh_pull_down_label" > Swipe refresh </string> <string Name= "Pull_to_refresh_release_label" > Release refresh </string> <string name= "Pull_to_refresh_refreshing_label" > Load </string> <string name= "Pull_to_refresh_tap_label" > click to refresh </string></resources>

The third step: Put the required picture files in the corresponding directory below, the image files used are:
Fourth Step:
1. Import or add the required class files (. java) from the Open source project Android-pulltorefresh to the specified package in your project.


The class file involved in the demo use case is:
"Library\src\com\handmark\pulltorefresh\library"
Pulltorefreshadapterviewbase.java
Pulltorefreshbase.java
Pulltorefreshlistview.java
"Library\src\com\handmark\pulltorefresh\library\internal"
Emptyviewmethodaccessor.java
Loadinglayout.java
2. Build the class file (. java) that you need.

"Pulltask.java"
Import Java.util.linkedlist;import Com.example.sampledemo.view.pulltorefreshlistview;import android.os.AsyncTask; Import Android.widget.baseadapter;public class Pulltask extends Asynctask<void, Void, string>{private  Pulltorefreshlistview Pulltorefreshlistview;               Implement pull-down refresh with pull-loaded listviewprivate int pullstate; Record inference.     Pull and pull action private baseadapter baseadapter; The ListView adapter is used to alert the ListView that the data has been updated private linkedlist<string> linkedlist;public Pulltask (pulltorefreshlistview Pulltorefreshlistview, int pullstate,baseadapter baseadapter, linkedlist<string> LinkedList) { This.pulltorefreshlistview = Pulltorefreshlistview;this.pullstate = Pullstate;this.baseadapter = BaseAdapter; This.linkedlist = LinkedList;} @Overrideprotected String doinbackground (Void ... params) {try {thread.sleep;} catch (Interruptedexception e) {} return "Stringtest";} @Overrideprotected void OnPostExecute (String result) {if (pullstate = = 1) {//name= "pulldownfromtop" value= "0x1" Drop down Linkedlist.addfirst("Top Data");} if (pullstate = = 2) {//name= "Pullupfrombottom" value= "0x2" pull-up linkedlist.addlast ("bottom Data");} Baseadapter.notifydatasetchanged ();p ulltorefreshlistview.onrefreshcomplete (); Super.onpostexecute (result);}}

"Pulladapter.java"
Import Java.util.linkedlist;import Com.example.sampledemo.r;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.textview;public class Pulladapter extends Baseadapter {private linkedlist<string> linkedlist;private layoutinflater minflater;public pulladapter (LinkedList<String> LinkedList, Context context) {Minflater = Layoutinflater.from (context); this.linkedlist = LinkedList;} @Overridepublic int GetCount () {return linkedlist.size ();} @Overridepublic Object getItem (int position) {return linkedlist.get (position);} @Overridepublic long Getitemid (int position) {return position;}  @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder holder=null;if (Convertview = = NULL) {holder = new Viewholder (); Convertview = Minflater.inflate (R.layout.layout_main_listitem, null); Holder.textview = (TextView) Convertview.findviewbyid (R.id.texTView); Convertview.settag (holder);} else {holder = (Viewholder) Convertview.gettag ();} if (Linkedlist.size () >0) {final String datastr = linkedlist.get (position); Holder.textView.setText (DATASTR);} return Convertview;}        private static class Viewholder {TextView TextView; Data display Area}}

3. Design layout file for Pulladapter.java layout_main_listitem.xml
<?xml version= "1.0" encoding= "Utf-8"?

><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match _parent " android:layout_height=" match_parent " android:background=" #FFFFFF " android:orientation=" Vertical "> <textview android:id=" @+id/textview " android:textcolor=" #99CC66 " android: Textsize= "18DP" android:layout_margintop= "4DP" android:layout_width= "Match_parent" android: layout_height= "Wrap_content" android:gravity= "left"/></linearlayout>


A reminder layout file appears when sliding pull_to_refresh_header.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:paddingtop=" 10DP "Android oid:paddingbottom= "10dip" > <textview android:id= "@+id/pull_to_refresh_text" android:text= "@string/ Pull_to_refresh_pull_down_label "android:textappearance="? Android:attr/textappearancemedium "Android:textSty Le= "Bold" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_cen Terinparent= "true"/> <progressbar android:id= "@+id/pull_to_refresh_progress" android:indeterminate = "true" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_margi nleft= "30dip" android:layout_marginright= "20dip" android:visibility= "Gone" Android:layout_centerverti Cal= "true" style= "?

Android:attr/progressbarstylesmall "/> <imageview android:id=" @+id/pull_to_refresh_image "Android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "30dip" an droid:layout_marginright= "20dip" android:layout_centervertical= "true"/></relativelayout>


Mainactivity.java Main layout file Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "xmlns:cp=" Http://schemas.android.com/apk/res/com.example.sampledemo "    android: Layout_width= "Match_parent"    android:background= "#FFFFFF"    android:layout_height= "Match_parent" >< Com.example.sampledemo.view.PullToRefreshListView    android:id= "@+id/pullrefresh"    android:background= "# FFFFFF "         android:layout_width=" fill_parent "         android:layout_height=" fill_parent "         android:divider=" @ Android:color/black "    android:dividerheight=" 0.1dip "    android:cachecolorhint=" #00000000 "cp:mode=" both " ></com.example.sampledemo.view.PullToRefreshListView></RelativeLayout>

4, write Mainactivity.java
Import Java.util.arrays;import Java.util.linkedlist;import Com.example.sampledemo.view.pulltorefreshbase.onrefreshlistener;import Com.example.sampledemo.view.pulltorefreshlistview;import Com.example.sampledemo.view.adapter.pulladapter;import Com.example.sampledemo.view.task.pulltask;import Android.os.bundle;import Android.widget.arrayadapter;import Android.widget.listview;import android.app.activity;/** * @ClassName Mainactivity.java * @Author mahaochen * @Date 2014-4-30 15:56:47 */public class Mainactivity extends Activity {private linkedlist<string> mlistitems;private Pulltorefreshlistview mpullrefreshlistview;private arrayadapter<string> mAdapter;private ListView MListView; Private Pulladapter pulladapter;private string[] mstrings = {"Initial data 01", "Initial data 02", "Initial data 03", "Initial data 04", "Initial data 05"};@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Initviews ();} private void Initviews () {Mpullrefreshlistview = (Pulltorefreshlistview) Findviewbyid (R.id.pullrefresh); Mpullrefreshlistview.setonrefreshlistener ( Monrefreshlistener); Mlistview = Mpullrefreshlistview.getrefreshableview (); mlistitems = new LinkedList<String> (); Mlistitems.addall (Arrays.aslist (mstrings));p ulladapter = new Pulladapter (Mlistitems, mainactivity.this); Mlistview.setadapter (Pulladapter);} Onrefreshlistener Monrefreshlistener = new Onrefreshlistener () {public void Onrefresh () {pulltask pulltask =new PullTask ( Mpullrefreshlistview, Mpullrefreshlistview.getrefreshtype (), Pulladapter, Mlistitems);p ulltask.execute ();}};}

:http://download.csdn.net/detail/ma_hoking/7276365

Android drop-down refresh pull-up load effect function

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.