Zhao yazhi _ name card holder (3) _ pull-down refresh to load more, Zhao yazhi name card holder
Reprinted please indicate address: http://blog.csdn.net/zhaoyazhi2129/article/details/38751681
Create a refreshed head Layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="bottom" > <RelativeLayout android:id="@+id/listview_header_content" android:layout_width="fill_parent" android:layout_height="60dp" > <LinearLayout android:id="@+id/listview_header_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/listview_header_hint_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/listview_header_hint_normal" android:textColor="@color/tvcolor"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/listview_header_last_time" android:textSize="12sp" android:textColor="@color/tvcolor"/> <TextView android:id="@+id/listview_header_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp" android:textColor="@color/tvcolor" /> </LinearLayout> </LinearLayout> <ImageView android:id="@+id/listview_header_arrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/listview_header_text" android:layout_centerVertical="true" android:layout_marginLeft="-35dp" android:src="@drawable/listview_arrow" /> <ProgressBar android:id="@+id/listview_header_progressbar" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignLeft="@id/listview_header_text" android:layout_centerVertical="true" android:layout_marginLeft="-40dp" android:visibility="invisible" /> </RelativeLayout></LinearLayout>
Customize the header information in the Toolkit
PullListViewHeader. java
Package com. cards. basic. util; import android. content. context; import android. util. attributeSet; import android. view. gravity; import android. view. layoutInflater; import android. view. view; import android. view. animation. animation; import android. view. animation. rotateAnimation; import android. widget. imageView; import android. widget. linearLayout; import android. widget. progressBar; import android. widget. textView; im Port com. cards. r;/***** @ explaination pull-down to load more * @ author yazhizhao * @ time 2014-8-1 11:30:35 */public class PullListViewHeader extends LinearLayout {private LinearLayout mContainer; private ImageView mArrowImageView; private ProgressBar mProgressBar; private TextView mHintTextView; private int mState = STATE_NORMAL; private Animation mRotateUpAnim; private Animation mRotateDownAnim; private final int ROT ATE_ANIM_DURATION = 180; public final static int STATE_NORMAL = 0; public final static int STATE_READY = 1; public final static int STATE_REFRESHING = 2; public PullListViewHeader (Context context) {super (context ); initView (context);}/*** @ param context * @ param attrs */public PullListViewHeader (Context context, AttributeSet attrs) {super (context, attrs ); initView (context);} private void initView (Context Context) {// In the initial condition, set the drop-down refresh view height to 0LinearLayout. layoutParams lp = new LinearLayout. layoutParams (LayoutParams. FILL_PARENT, 0); mContainer = (LinearLayout) LayoutInflater. from (context ). inflate (R. layout. listview_header, null); addView (mContainer, lp); setGravity (Gravity. BOTTOM); mArrowImageView = (ImageView) findViewById (R. id. listview_header_arrow); mHintTextView = (TextView) findViewById (R. id. listview_header _ Hint_textview); mProgressBar = (ProgressBar) findViewById (R. id. listview_header_progressbar); mRotateUpAnim = new RotateAnimation (0.0f,-180.0f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); mRotateUpAnim. setDuration (ROTATE_ANIM_DURATION); mRotateUpAnim. setFillAfter (true); mRotateDownAnim = new RotateAnimation (-180.0f, 0.0f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SEL F, 0.5f); mRotateDownAnim. setDuration (ROTATE_ANIM_DURATION); mRotateDownAnim. setFillAfter (true);} public void setState (int state) {if (state = mState) return; if (state = STATE_REFRESHING) {// display progress mArrowImageView. clearAnimation (); mArrowImageView. setVisibility (View. INVISIBLE); mProgressBar. setVisibility (View. VISIBLE);} else {// display the arrow picture mArrowImageView. setVisibility (View. VISIBLE); mProgressBar. setVisibili Ty (View. INVISIBLE);} switch (state) {case STATE_NORMAL: if (mState = STATE_READY) {mArrowImageView. startAnimation (mRotateDownAnim);} if (mState = STATE_REFRESHING) {mArrowImageView. clearAnimation ();} mHintTextView. setText (R. string. listview_header_hint_normal); break; case STATE_READY: if (mState! = STATE_READY) {mArrowImageView. clearAnimation (); mArrowImageView. startAnimation (mRotateUpAnim); mHintTextView. setText (R. string. listview_header_hint_ready);} break; case STATE_REFRESHING: mHintTextView. setText (R. string. listview_header_hint_loading); break; default:} mState = state;} public void setVisiableHeight (int height) {if (height <0) height = 0; LinearLayout. layoutParams lp = (LinearLayout. layoutParams) mContainer. getLayoutParams (); lp. height = height; mContainer. setLayoutParams (lp);} public int getVisiableHeight () {return mContainer. getHeight ();}}
Create the custom file PullListView. java of listview in the Toolkit
Package com. cards. basic. util; import android. content. context; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; import android. view. viewTreeObserver. onGlobalLayoutListener; import android. view. animation. decelerateInterpolator; import android. widget. absListView; import android. widget. absListView. onScrollListener; import android. widget. listView; import android. widget. RelativeLayout; import android. widget. scroller; import android. widget. textView; import com. cards. r;/***** @ explaination pull-down to load more * @ author yazhizhao * @ time 2014-8-1 11:30:45 */public class PullListView extends ListView implements OnScrollListener {private float mLastY =-1; // yprivate Scroller mScroller; // used to roll back the private OnScrollListener mScrollListener; // The rollback listener // interface triggers refresh and load more private IXListViewListene R mListViewListener; // private PullListViewHeader mHeaderView; // The entire header layout is private RelativeLayout mHeaderViewContent; // The Last refreshed time layout is private TextView mHeaderTimeView; // The height of the header is private int mHeaderViewHeight; private boolean mEnablePullRefresh = true; // whether to refresh private boolean mPullRefreshing = false; // total number of private int mTotalItemCount; // for the photo scroll, scroll back to the header or footer. Private int mScrollBack; private final static int SCROLLBACK_HEADER = 0; private final static int SCROLL_DURATION = 400; // scroll back durationprivate final static float OFFSET_RADIO = 1.8f; // support iOS like pull // feature. /*** @ param context */public PullListView (Context context) {super (context); initWithContext (context);} public PullListView (Context context, AttributeSet attrs) {super (context, Ttrs); initWithContext (context);} public PullListView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); initWithContext (context );} // ListView needs to scroll the event, which will schedule the event, the user's listener (as a proxy private void initWithContext (Context context) {mScroller = new Scroller (context, new DecelerateInterpolator (); super. setOnScrollListener (this); // initialization control mHeaderView = new PullListViewHeader (context); mHe AderViewContent = (RelativeLayout) mHeaderView. findViewById (R. id. listview_header_content); mHeaderTimeView = (TextView) mHeaderView. findViewById (R. id. listview_header_time); addHeaderView (mHeaderView); // initialize the header information mHeaderView. getViewTreeObserver (). addOnGlobalLayoutListener (new OnGlobalLayoutListener () {@ Overridepublic void onGlobalLayout () {mHeaderViewHeight = mHeaderViewContent. getHeight (); getViewTreeO Bserver (). removeGlobalOnLayoutListener (this) ;}});}/*** enable or disable the pull refresh function ** @ param enable */public void setPullRefreshEnable (boolean enable) {// whether to refresh mEnablePullRefresh = enable; // if not, refresh if (! MEnablePullRefresh) {mHeaderViewContent. setVisibility (View. INVISIBLE);} else {// if it is refreshing mHeaderViewContent. setVisibility (View. VISIBLE) ;}}/*** stop refreshing, reset title view */public void stopRefresh () {if (mPullRefreshing = true) {mPullRefreshing = false; resetHeaderHeight () ;}}/*** sets the last refresh time ** @ param time */public void setRefreshTime (String time) {mHeaderTimeView. setText (time);} private void invokeOnScrolling () {if (MScrollListener instanceof OnXScrollListener) {OnXScrollListener l = (OnXScrollListener) mScrollListener; l. onXScrolling (this) ;}} private void updateHeaderHeight (float delta) {mHeaderView. setVisiableHeight (int) delta + mHeaderView. getVisiableHeight (); if (mEnablePullRefresh &&! MPullRefreshing) {// not refreshed, update the arrow if (mHeaderView. getVisiableHeight ()> mHeaderViewHeight) {mHeaderView. setState (PullListViewHeader. STATE_READY);} else {mHeaderView. setState (PullListViewHeader. STATE_NORMAL) ;}} setSelection (0); // scroll to the top of each time}/*** reset header height */private void resetHeaderHeight () {int height = mHeaderView. getVisiableHeight (); if (height = 0) // return is not displayed; // refresh and the header is not fully displayed, and if (mPullRefresh Ing & height <= mHeaderViewHeight) {return;} int finalHeight = 0; // default value: Scroll back. // refresh and roll back to display all header information if (mPullRefreshing & height> mHeaderViewHeight) {finalHeight = mHeaderViewHeight;} mScrollBack = SCROLLBACK_HEADER; mScroller. startScroll (0, height, 0, finalHeight-height, SCROLL_DURATION); invalidate () ;}@ Overridepublic boolean onTouchEvent (MotionEvent ev) {if (mLastY =-1) {mLastY = ev. getRawY ();} sw Itch (ev. getAction () {case MotionEvent. ACTION_DOWN: mLastY = ev. getRawY (); break; case MotionEvent. ACTION_MOVE: final float deltaY = ev. getRawY ()-mLastY; mLastY = ev. getRawY (); if (getFirstVisiblePosition () = 0 & (mHeaderView. getVisiableHeight ()> 0 | deltaY> 0) {// The first item is display. The title is displayed or pulled down. updateHeaderHeight (deltaY/OFFSET_RADIO); invokeOnScrolling ();} break; default: mLastY =-1; // reset if (getFirstVisiblePos Ition () = 0) {// call to refresh if (mEnablePullRefresh & mHeaderView. getVisiableHeight ()> mHeaderViewHeight) {mPullRefreshing = true; mHeaderView. setState (PullListViewHeader. STATE_REFRESHING); if (mListViewListener! = Null) {mListViewListener. onRefresh () ;}} resetHeaderHeight () ;}break ;}return super. onTouchEvent (ev) ;}@ Overridepublic void computeScroll () {if (mScroller. computescroloffset () {if (mScrollBack = SCROLLBACK_HEADER) {mHeaderView. setVisiableHeight (mScroller. getCurrY ();} postInvalidate (); invokeOnScrolling ();} super. computeScroll () ;}@ Overridepublic void setOnScrollListener (OnScrollListener l) {mScrollLis Tener = l ;}@ Overridepublic void onScrollStateChanged (AbsListView view, int scrollState) {if (mScrollListener! = Null) {mScrollListener. onScrollStateChanged (view, scrollState) ;}@ Overridepublic void onScroll (AbsListView view, int detail, int visibleItemCount, int totalItemCount) {// the listener sent to the user mTotalItemCount = totalItemCount; if (mScrollListener! = Null) {mScrollListener. onScroll (view, firstVisibleItem, visibleItemCount, totalItemCount);} public void setXListViewListener (IXListViewListener l) {mListViewListener = l;}/*** you can listen to the List view. OnScrollListener or this one. It will call onXScrolling when the title/footer is rolled back. */Public interface OnXScrollListener extends OnScrollListener {public void onXScrolling (View view);}/*** implement this interface to get/load more events, refresh */public interface IXListViewListener {public void onRefresh (); public void onLoadMore ();}}
Use the custom PullListView file in the layout File
<com.cards.basic.util.PullListView android:id="@+id/lv_changelist" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/titlebar_above" android:layout_margin="10dp" android:cacheColorHint="#00000000" android:divider="#00000000" android:fadingEdge="none" android:listSelector="#00000000" > </com.cards.basic.util.PullListView>
Implements the IXListViewListener interface in the corresponding activity class and inherits the onRefresh and onLoadMore methods.
Package com. cards. activity; import java. text. simpleDateFormat; import java. util. arrayList; import java. util. list; import android. app. activity; import android. content. intent; import android. content. sharedPreferences; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; import android. widget. textView; import com. cards. r; import com. cards. adapter. changeCardAdapter; import com. cards. basic. util. pullListView; import com. cards. basic. util. pullListView. IXListViewListener; import com. cards. commom. common; import com. cards. commom. myApp; import com. cards. entity. findf; import com. cards. manager. changeMgr;/*** @ explaination exchange business card * @ author yazhizhao * @ time 9:30:59 */public class ChangeCardAct extends Activity implements IXListViewListener {private ChangeCardAdapter adapter; private Handler mHandler; private PullListView pointList; private List <Findf> list; private ChangeMgr changeMgr; private Button btnRegistered; private MessageThread messageThread; private SharedPreferences sp; private int size; private ImageView imageView1; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. act_change_card); // used to safely exit MyApp. getInstance (). addActivity (this); // set the title TextView tvTitleCommenCenter = (TextView) findViewById (R. id. TV _title); tvTitleCommenCenter. setText (""); // view the Exchange Request btnRegistered = (Button) findViewById (R. id. btn_right_title); btnRegistered. setOnClickListener (new OnClickListener () {public void onClick (View v) {startActivity (new Intent (). setClass (ChangeCardAct. this, ChangeReqAct. class) ;}}); // enable the messageThread thread = new MessageThread (); messageThread. start (); // get the nearby person changeMgr = new ChangeMgr (ChangeCardAct. this, handler); changeMgr. findF (); mHandler = new Handler (); // whether the exchange information changeHint ();} /***** @ explaination whether to receive the exchanged information * @ author yazhizhao * @ time 10:19:39 */private void changeHint () {sp = getSharedPreferences ("common_data", MODE_PRIVATE ); size = sp. getInt ("Status_size", 0); imageView1 = (ImageView) findViewById (R. id. imageView1); if (size> 0) {imageView1.setVisibility (View. VISIBLE);} else if (size = 0) {imageView1.setVisibility (View. GONE );}} /***** @ explaination the newly opened Thread always requests whether or not anyone exchanges * @ author yazhizhao * @ time 5:20:36 */class MessageThread extends Thread {public void run () {while (true) {try {// whether the request has been exchanged with changeMgr. friendInfo (); // whether the exchange information changeHint (); Thread. sleep (1000*6);} catch (InterruptedException e) {e. printStackTrace () ;}}}/*** Message Processing Mechanism */private Handler handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {Common. cancelLoading (); switch (msg. what) {case 0: Bundle bundle = msg. getData (); ArrayList arraylist = bundle. getParcelableArrayList ("list"); list = (List <Findf>) arraylist. get (0); pointList = (PullListView) findViewById (R. id. lv_changelist); adapter = new ChangeCardAdapter (ChangeCardAct. this, list, handler); pointList. setAdapter (adapter); pointList. setXListViewListener (ChangeCardAct. this); break; case 2: pointList = (PullListView) findViewById (R. id. lv_changelist); list = new ArrayList <Findf> (); adapter = new ChangeCardAdapter (ChangeCardAct. this, list, handler); pointList. setAdapter (adapter); pointList. setXListViewListener (ChangeCardAct. this); break ;}};@ Overrideprotected void onStart () {// used to safely exit MyApp. getInstance (). addActivity (this); super. onStart ();}/***** @ explaination pull-down Refresh setting last time * @ author yazhizhao * @ time 11:10:51 * @ return */private String getCurrentDate () {SimpleDateFormat sdf = new java. text. simpleDateFormat ("yyyy-MM-dd HH: mm: ss"); String str = sdf. format (new java. util. date (); return str;}/***** @ explaination pull-down refresh * @ author yazhizhao * @ time 11:10:51 */@ Overridepublic void onRefresh () {mHandler. postDelayed (new Runnable () {@ Overridepublic void run () {onReload (); changeHint (); onLoad () ;}, 2000 );} /***** @ explaination load more * @ author yazhizhao * @ time 11:10:51 */@ Overridepublic void onLoadMore () {mHandler. postDelayed (new Runnable () {@ Overridepublic void run () {}}, 2000) ;}@ Overrideprotected void onResume () {// set the positioning status to changeMgr for the first time. findF (); changeHint (); super. onResume ();}/***** @ explaination stop refreshing and set the time * @ author yazhizhao * @ time 10:23:47 */private void onLoad () {pointList. stopRefresh (); pointList. setRefreshTime (getCurrentDate ();}/***** @ explaination refresh again * @ author yazhizhao * @ time 10:23:50 */public boolean onReload () {boolean result = true; changeMgr. findF (); return result ;}}