Android Sina Weibo down-pull refresh (latest message on top) _android

Source: Internet
Author: User
Tags gety
Check the latest news to use similar Sina Weibo dropdown refresh Function! Show the latest news on the top!
The code is as follows:
Pulltorefreshlistview Class Code
Copy Code code as follows:

Package com.markupartist.android.widget;

Import Java.util.Date;
Import COM.MARKUPARTIST.ANDROID.EXAMPLE.PULLTOREFRESH.R;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.animation.LinearInterpolator;
Import android.view.animation.RotateAnimation;
Import Android.widget.AbsListView;
Import Android.widget.AbsListView.OnScrollListener;
Import Android.widget.BaseAdapter;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.ListView;
Import Android.widget.ProgressBar;
Import Android.widget.TextView;

public class Pulltorefreshlistview extends ListView implements Onscrolllistener {
private static final String TAG = "ListView";
Private final static int release_to_refresh = 0;
Private final static int pull_to_refresh = 1;
Private final static int refreshing = 2;
Private final static int done = 3;
Private final static int LOADING = 4;
The ratio of the actual padding distance to the offset distance on the interface
Private final static int RATIO = 3;
Private Layoutinflater Inflater;
Private LinearLayout Headview;
Private TextView Tipstextview;
Private TextView Lastupdatedtextview;
Private ImageView Arrowimageview;
Private ProgressBar ProgressBar;

Private rotateanimation animation;
Private Rotateanimation reverseanimation;
Used to ensure that the value of the Starty is recorded only once in a full touch event
Private Boolean isrecored;
private int headcontentwidth;
private int headcontentheight;
private int starty;
private int firstitemindex;
private int state;
Private Boolean isback;
Private Onrefreshlistener Refreshlistener;
Private Boolean isrefreshable;
Public Pulltorefreshlistview {
Super (context);
Init (context);
}
Public Pulltorefreshlistview (context context, AttributeSet Attrs) {
Super (context, attrs);
Init (context);
}
private void init (context context) {
Setcachecolorhint (Context.getresources (). GetColor (Android). R.color.transparent));
Inflater = Layoutinflater.from (context);
Headview = (linearlayout) inflater.inflate (r.layout.pull_to_refresh_header, NULL);
Arrowimageview = (ImageView) headview
. Findviewbyid (R.id.head_arrowimageview);
Arrowimageview.setminimumwidth (70);
Arrowimageview.setminimumheight (50);
ProgressBar = (ProgressBar) headview
. Findviewbyid (R.id.head_progressbar);
Tipstextview = (TextView) Headview.findviewbyid (R.id.head_tipstextview);
Lastupdatedtextview = (TextView) headview
. Findviewbyid (R.id.head_lastupdatedtextview);
Measureview (Headview);
Headcontentheight = Headview.getmeasuredheight ();
Headcontentwidth = Headview.getmeasuredwidth ();
Headview.setpadding (0,-1 * headcontentheight, 0, 0);
Headview.invalidate ();
LOG.V ("Size", "width:" + headcontentwidth + "Height:"
+ headcontentheight);
Addheaderview (Headview, NULL, FALSE);
Setonscrolllistener (this);
Animation = new Rotateanimation (0,-180,
Rotateanimation.relative_to_self, 0.5f,
Rotateanimation.relative_to_self, 0.5f);
Animation.setinterpolator (New Linearinterpolator ());
Animation.setduration (250);
Animation.setfillafter (TRUE);
Reverseanimation = new Rotateanimation (-180, 0,
Rotateanimation.relative_to_self, 0.5f,
Rotateanimation.relative_to_self, 0.5f);
Reverseanimation.setinterpolator (New Linearinterpolator ());
Reverseanimation.setduration (200);
Reverseanimation.setfillafter (TRUE);
state = done;
Isrefreshable = false;
}
public void Onscroll (Abslistview arg0, int firstvisiableitem, int arg2,
int Arg3) {
Firstitemindex = Firstvisiableitem;
}
public void onscrollstatechanged (Abslistview arg0, int arg1) {
}
public boolean ontouchevent (Motionevent event) {

Firstitemindex=getfirstvisibleposition ();
if (isrefreshable) {
Switch (event.getaction ()) {
Case Motionevent.action_down:
if (Firstitemindex = = 0 &&!isrecored) {
Isrecored = true;
Starty = (int) event.gety ();
LOG.V (TAG, "record the current position at down time");
}
Break
Case MOTIONEVENT.ACTION_UP:
if (State!= refreshing && State!= LOADING) {
if (state = = done) {
Don't do anything.
}
if (state = = Pull_to_refresh) {
state = done;
Changeheaderviewbystate ();
LOG.V (TAG, "from the Drop-down refresh state, to the done State");
}
if (state = = Release_to_refresh) {
state = refreshing;
Changeheaderviewbystate ();
Onrefresh ();
LOG.V (TAG, "by loosening the refresh state, to the done State");
}
}
Isrecored = false;
Isback = false;
Break
Case Motionevent.action_move:
int tempy = (int) event.gety ();
if (!isrecored && firstitemindex = 0) {
LOG.V (TAG, "record position at move Time");
Isrecored = true;
Starty = Tempy;
}
if (State!= refreshing && isrecored && State!= LOADING) {
Ensure that the current position is always in the head during the setting of the padding, otherwise if the list goes out of the screen, the list scrolls at the same time when it is pushed up
You can let go and refresh.
if (state = = Release_to_refresh) {
SetSelection (0);
Push it up, push it to the screen enough to cover the head, but it hasn't been pushed to the full extent of the cover-up.
if ((tempy-starty)/RATIO < Headcontentheight)
&& (Tempy-starty) > 0) {
state = Pull_to_refresh;
Changeheaderviewbystate ();
LOG.V (TAG, "from the release of the refresh state to the Drop-down refresh State");
}
Pushed it to the top.
else if (tempy-starty <= 0) {
state = done;
Changeheaderviewbystate ();
LOG.V (TAG, "transition from release refresh state to done State");
}
Pulled down, or had not been pushed up to the top of the screen to cover up the head
else {
You don't have to do anything special, just update the paddingtop value.
}
}
Not yet reached show release refresh when done or Pull_to_refresh state
if (state = = Pull_to_refresh) {
SetSelection (0);
Down to the state of being able to enter the Release_to_refresh
if ((tempy-starty)/RATIO >= headcontentheight) {
state = Release_to_refresh;
Isback = true;
Changeheaderviewbystate ();
LOG.V (TAG, "from the Done or Drop-down Refresh state transition to release refresh");
}
It's pushing up the top.
else if (tempy-starty <= 0) {
state = done;
Changeheaderviewbystate ();
LOG.V (TAG, "transition from done or drop-down refresh state to done State");
}
}
Done in a state
if (state = = done) {
if (Tempy-starty > 0) {
state = Pull_to_refresh;
Changeheaderviewbystate ();
}
}
Update size of Headview
if (state = = Pull_to_refresh) {
Headview.setpadding (0,-1 * headcontentheight
+ (Tempy-starty)/RATIO, 0, 0);
}
Update Headview's Paddingtop
if (state = = Release_to_refresh) {
Headview.setpadding (0, (tempy-starty)/RATIO
-Headcontentheight, 0, 0);
}
}
Break
}
}
Return Super.ontouchevent (event);
}
When the state changes, call the method to update the interface
private void Changeheaderviewbystate () {
Switch (state) {
Case Release_to_refresh:
Arrowimageview.setvisibility (view.visible);
Progressbar.setvisibility (View.gone);
Tipstextview.setvisibility (view.visible);
Lastupdatedtextview.setvisibility (view.visible);
Arrowimageview.clearanimation ();
Arrowimageview.startanimation (animation);
Tipstextview.settext ("loosen refresh");
LOG.V (TAG, "Current state, release refresh");
Break
Case Pull_to_refresh:
Progressbar.setvisibility (View.gone);
Tipstextview.setvisibility (view.visible);
Lastupdatedtextview.setvisibility (view.visible);
Arrowimageview.clearanimation ();
Arrowimageview.setvisibility (view.visible);
was transformed by the Release_to_refresh state.
if (isback) {
Isback = false;
Arrowimageview.clearanimation ();
Arrowimageview.startanimation (reverseanimation);
Tipstextview.settext ("Drop-down refresh");
} else {
Tipstextview.settext ("Drop-down refresh");
}
LOG.V (TAG, "Current state, Drop-down refresh");
Break
Case refreshing:
Headview.setpadding (0, 0, 0, 0);
Progressbar.setvisibility (view.visible);
Arrowimageview.clearanimation ();
Arrowimageview.setvisibility (View.gone);
Tipstextview.settext ("refreshing ...");
Lastupdatedtextview.setvisibility (view.visible);
LOG.V (TAG, "current state, refreshing ...");
Break
Case done:
Headview.setpadding (0,-1 * headcontentheight, 0, 0);
Progressbar.setvisibility (View.gone);
Arrowimageview.clearanimation ();
Arrowimageview.setimageresource (R.drawable.ic_pulltorefresh_arrow);
Tipstextview.settext ("Drop-down refresh");
Lastupdatedtextview.setvisibility (view.visible);
LOG.V (TAG, "current state, done");
Break
}
}
public void Setonrefreshlistener (Onrefreshlistener refreshlistener) {
This.refreshlistener = Refreshlistener;
Isrefreshable = true;
}
Public interface Onrefreshlistener {
public void Onrefresh ();
}
public void Onrefreshcomplete () {
state = done;
Lastupdatedtextview.settext ("Last update:" + new Date (). toLocaleString ());
Changeheaderviewbystate ();
}
private void Onrefresh () {
if (Refreshlistener!= null) {
Refreshlistener.onrefresh ();
}
}
This method is directly copied from a Drop-down refresh on the network demo, here is "estimated" headview width and height
private void Measureview (View child) {
Viewgroup.layoutparams p = child.getlayoutparams ();
if (p = = null) {
p = new Viewgroup.layoutparams (ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
int childwidthspec = viewgroup.getchildmeasurespec (0, 0 + 0, p.width);
int lpheight = P.height;
int childheightspec;
if (Lpheight > 0) {
Childheightspec = Measurespec.makemeasurespec (Lpheight,
measurespec.exactly);
} else {
Childheightspec = Measurespec.makemeasurespec (0,
measurespec.unspecified);
}
Child.measure (Childwidthspec, Childheightspec);
}
public void Setadapter (Baseadapter adapter) {
Lastupdatedtextview.settext ("Last update:" + new Date (). toLocaleString ());
Super.setadapter (adapter);
}
}

Activity Calling code
Copy Code code as follows:

Package Com.markupartist.android.example.pulltorefresh;
Import Java.util.Arrays;
Import java.util.LinkedList;
Import android.app.ListActivity;
Import Android.os.AsyncTask;
Import Android.os.Bundle;
Import Android.widget.ArrayAdapter;
Import Com.markupartist.android.widget.PullToRefreshListView;
Import Com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;
public class Pulltorefreshactivity extends Listactivity {
Private linkedlist<string> Mlistitems;
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.pull_to_refresh);
Set a listener to is invoked when the list should is refreshed.
((Pulltorefreshlistview) Getlistview ()). Setonrefreshlistener (New Onrefreshlistener () {
@Override
public void Onrefresh () {
Do work to refresh the list here.
New Getdatatask (). Execute ();
}
});
Mlistitems = new linkedlist<string> ();
Mlistitems.addall (Arrays.aslist (mstrings));
arrayadapter<string> adapter = new Arrayadapter<string> (This,
Android. R.layout.simple_list_item_1, Mlistitems);
Setlistadapter (adapter);
}
Private class Getdatatask extends Asynctask<void, Void, string[]> {
@Override
Protected string[] Doinbackground (Void ... params) {
Simulates a background job.
try {
Thread.Sleep (2000);
catch (Interruptedexception e) {

}
return mstrings;
}
@Override
protected void OnPostExecute (string[] result) {
Mlistitems.addfirst ("Added after Refresh ...");
Call Onrefreshcomplete when the list has been refreshed.
((Pulltorefreshlistview) Getlistview ()). Onrefreshcomplete ();
Super.onpostexecute (result);
}
}
Private string[] Mstrings = {
"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
"Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
"Afuega ' L Pitu", "Airag", "Airedale", "Aisy cendre",
"Allgauer Emmentaler"};
}
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.