Android Open Source project Pulltorefresh Drop-down Refresh features detailed 2_android

Source: Internet
Author: User
Tags addall arrays stub

First look at the effect chart:

Here is the use of Pulltorefreshgridview, and the previous Pulltorefreshlistview method is the same, because the open source project is very good modularity, so it is easy to implement. It's equivalent to saying that we can do this by using the controls before, regardless of other issues.

Ideas:

1. Write the layout file, put the control that can be pulled down to refresh
2. Locate the Drop-down refresh control, set the listener, and open an asynchronous task in the Refresh method to manipulate
3. This drop-down refreshes the control's Getrefreshableview () method to get the GridView object and set the adapter in normal operation
4. Add new data to the head or tail in an asynchronous task by LinkedList

implementation:

1. layout file

As we can see, we can still define the properties of the GridView as we would use the GridView. Of course, you can set exclusive properties through the PTR: namespaces

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:orientation=" vertical "
 android:layout_width=" fill_parent "
 android:layout_" height= "Fill_parent" >

<!--the Pulltorefreshgridview replaces a standard GridView widget.-->
 < Com.handmark.pulltorefresh.library.PullToRefreshGridView
  xmlns:ptr= "http://schemas.android.com/apk/ Res-auto "
  android:id=" @+id/pull_refresh_grid "
  android:layout_height=" fill_parent "
  android:layout_" Width= "Fill_parent"
  android:numcolumns= "Auto_fit"
  android:verticalspacing= "1DP"
  android: horizontalspacing= "1DP"
  android:columnwidth= "100DP"
  android:stretchmode= "ColumnWidth"
  android: gravity= "Fill"
  ptr:ptrmode= "both"
  ptr:ptrdrawable= "@drawable/ic_launcher"/>

</ Linearlayout>

2. Find this control that can be pulled down and set the listener

The listener here is different from the previous article, which is bidirectional. So it is very convenient to monitor the sliding operation!

 /**
  * Set dropdown refresh view, set bi-directional listener 
  /
 private void Initptrgrideview () {
  //Get Drop-down refresh of the GridView
  Mpullrefreshgridview = (Pulltorefreshgridview) Findviewbyid (R.id.pull_refresh_grid);
  Set up the listener, which can monitor bidirectional sliding, which can trigger different event
  Mpullrefreshgridview.setonrefreshlistener (New onrefreshlistener2< Gridview> () {

   @Override public
   void Onpulldowntorefresh (pulltorefreshbase<gridview> refreshview) {
    Toast.maketext (Getapplicationcontext (), "drop", Toast.length_short). Show ();
    New Getdatatask (Mpullrefreshgridview, Madapter, Mlistitems). Execute ();

   @Override public
   void Onpulluptorefresh (pulltorefreshbase<gridview> refreshview) {
    Toast.maketext ( Getapplicationcontext (), "pull", Toast.length_short). Show ();
    New Getdatatask (Mpullrefreshgridview, Madapter, Mlistitems). Execute ();}}
 

3. Find the GridView to set up the adapter

 A list array object that is used to easily add String objects
 private linkedlist<string> mlistitems;
 Controls used to pull down the refresh
 private Pulltorefreshgridview Mpullrefreshgridview;
 The real use of the control, it is implied into the pulltorefreshgridview, so need to find out to use the
 private GridView Mgridview;
 The adapter that defines the GridView
 private arrayadapter<string> madapter;

Here you can also set what is displayed when there is no data in the adapter, and the method is called: Setemptyview ()

 /**
  * Sets the GridView, finds it first, and then sets the adapter *
 /private void Initgrideview () {
  Mgridview = Mpullrefreshgridview.getrefreshableview ();
  Define a string array, and then put it in the LinkedList, and then just use LinkedList in the asynchronous task to add the beginning and end data.
  String []data = new string[] {"Android", "iOS "," WP "," Java "," C + + "," C # "};
  Mlistitems = new linkedlist<string> ();
  Mlistitems.addall (arrays.aslist (data));

  What is displayed when there is no data in the adapter, because I have populated a string array in the adapter, so it does not show "Here is empty, drop-Pull refresh try"
  TextView TV = new TextView (this);
  Tv.setgravity (gravity.center);
  Tv.settext ("Here is empty, pull the refresh try");
  The View
  Mpullrefreshgridview.setemptyview (TV) displayed when the interface is empty;

  Set adapter
  madapter = new Arrayadapter<string> (this, 
    Android. R.layout.simple_list_item_1, mlistitems);
  Mgridview.setadapter (Madapter);
 }

4. Perform asynchronous tasks, simulate loading data, this is the same as before

Package Com.kale.ptrgridview;

Import java.util.LinkedList;
Import Android.os.AsyncTask;

Import Android.widget.ArrayAdapter;
Import Com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;

Import Com.handmark.pulltorefresh.library.PullToRefreshGridView; /** * @author: Jack Tony * @tips: Load the data from the network through an asynchronous task, update * @date: 2014-10-14/public class Getdatatask extends Asynctas
 K<void, Void, void>{private Pulltorefreshgridview Mpullrefreshgridview;
 Private arrayadapter<string> Madapter;
 
 
 Private linkedlist<string> Mlistitems; Public Getdatatask (Pulltorefreshgridview GridView, arrayadapter<string> adapter,linkedlist<string>
  ListItems) {//TODO automatically generated constructor stub mpullrefreshgridview = GridView;
  Madapter = adapter;
 Mlistitems = ListItems;
  @Override protected void doinbackground (void ... params) {//Impersonation request, Shu Yi 2 second try {thread.sleep (2000);
 catch (Interruptedexception e) {} return null; } @Override protected void OnpostexeCute (Void result) {//TODO automatically generated method stub super.onpostexecute (results);
  Get the current pattern to determine where the data should be loaded mode mode = Mpullrefreshgridview.getcurrentmode ();
  if (mode = = Mode.pull_from_start) {Mlistitems.addfirst ("This is the refreshed data");
  else {Mlistitems.addlast ("This is the refreshed data");
  }//Notification data changed madapter.notifydatasetchanged ();
  
 Stop refreshing mpullrefreshgridview.onrefreshcomplete () after loading completes;

 }
 


}

Mainactivity.java all code in the  

Package Com.kale.ptrgridview;
Import Java.util.Arrays;

Import java.util.LinkedList;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.Gravity;
Import Android.widget.ArrayAdapter;
Import Android.widget.GridView;
Import Android.widget.TextView;

Import Android.widget.Toast;
Import Com.handmark.pulltorefresh.library.PullToRefreshBase;
Import Com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;

Import Com.handmark.pulltorefresh.library.PullToRefreshGridView;
 public class Mainactivity extends the activity {//List array object to facilitate adding String objects private linkedlist<string> mlistitems;
 Controls used to pull down the refresh private Pulltorefreshgridview Mpullrefreshgridview;
 The real use of the control, it is implied into the pulltorefreshgridview, so need to find out to use the private GridView mgridview;
 
 The adapter that defines the GridView private arrayadapter<string> madapter;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  
  Setcontentview (R.layout.activity_main);
Initview (); private void Initview () {//TODO automatically generated method stub Initptrgrideview ();
 Initgrideview (); /** * Set Drop-down refresh view, set bidirectional listener/private void Initptrgrideview () {//Get Drop-down refresh of GridView Mpullrefreshgridview = (pul
  Ltorefreshgridview) Findviewbyid (R.id.pull_refresh_grid); Set up the listener, which can monitor bidirectional sliding, which can trigger different event Mpullrefreshgridview.setonrefreshlistener (new Onrefreshlistener2<gridview > () {@Override public void Onpulldowntorefresh (pulltorefreshbase<gridview> refreshview) {Toast.maket
    Ext (Getapplicationcontext (), "drop Down", Toast.length_short). Show ();
   New Getdatatask (Mpullrefreshgridview, Madapter, Mlistitems). Execute (); @Override public void Onpulluptorefresh (pulltorefreshbase<gridview> refreshview) {Toast.maketext (getAp
    Plicationcontext (), "pull", Toast.length_short). Show ();
   New Getdatatask (Mpullrefreshgridview, Madapter, Mlistitems). Execute ();
 }

  }); /** * Sets the GridView, finds it first, and then sets the adapter */private void Initgrideview () {MgridvIew = Mpullrefreshgridview.getrefreshableview (); Define a string array, and then put it in the LinkedList, and then just use LinkedList in the asynchronous task to add the beginning and end of the string []data = new string[] {"Android", "ios", "wp
  "," Java "," C + + "," C # "};
  Mlistitems = new linkedlist<string> ();

  Mlistitems.addall (arrays.aslist (data));
  What is displayed when there is no data in the adapter, because I have populated a string array in the adapter, so it does not show "Here is empty, drop-Pull refresh try" TextView TV = new TextView (this);
  Tv.setgravity (Gravity.center);
  Tv.settext ("Here is empty, pull the refresh try");

  The View Mpullrefreshgridview.setemptyview (TV) displayed when the interface is empty; Set adapter Madapter = new Arrayadapter<string> (this, Android.
  R.layout.simple_list_item_1, Mlistitems);
 Mgridview.setadapter (Madapter);

 }
 
 
}

SOURCE Download: Http://xiazai.jb51.net/201609/yuanma/AndroidGridView (jb51.net). rar

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.