趙雅智_名片夾(3)_下拉重新整理載入更多,趙雅智名片夾

來源:互聯網
上載者:User

趙雅智_名片夾(3)_下拉重新整理載入更多,趙雅智名片夾

轉載請標明地址:http://blog.csdn.net/zhaoyazhi2129/article/details/38751681


建立重新整理的頭部布局

<?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>


在工具包下自訂頭部資訊

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;import com.cards.R;/** *  * @explaination 下拉載入更多 * @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 ROTATE_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) {// 初始情況,設定下拉重新整理view高度為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_SELF,0.5f);mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);mRotateDownAnim.setFillAfter(true);}public void setState(int state) {if (state == mState) return ;if (state == STATE_REFRESHING) {// 顯示進度mArrowImageView.clearAnimation();mArrowImageView.setVisibility(View.INVISIBLE);mProgressBar.setVisibility(View.VISIBLE);} else {// 顯示箭頭圖片mArrowImageView.setVisibility(View.VISIBLE);mProgressBar.setVisibility(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();}}
在工具包下建立listview的自訂檔案PullListView.java
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 下拉載入更多 * @author yazhizhao * @time 2014-8-1上午11:30:45 */public class PullListView extends ListView implements OnScrollListener {private float mLastY = -1; // yprivate Scroller mScroller; // 用於復原private OnScrollListener mScrollListener; // 復原監聽// 介面觸發重新整理和負載更多private IXListViewListener mListViewListener;//private PullListViewHeader mHeaderView;// 整個頭部布局private RelativeLayout mHeaderViewContent;// 上次重新整理的時間布局private TextView mHeaderTimeView;// 頭部的高度private int mHeaderViewHeight;private boolean mEnablePullRefresh = true;// 是否重新整理private boolean mPullRefreshing = false;// 總共的條數private int mTotalItemCount;// 對於照片捲軸,捲軸回來頁首或頁尾。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, attrs);initWithContext(context);}public PullListView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);initWithContext(context);}// ListView需要滾動事件,它將調度事件,使用者的接聽程式(作為一個代理private void initWithContext(Context context) {mScroller = new Scroller(context, new DecelerateInterpolator());super.setOnScrollListener(this);// 初始化控制項mHeaderView = new PullListViewHeader(context);mHeaderViewContent = (RelativeLayout) mHeaderView.findViewById(R.id.listview_header_content);mHeaderTimeView = (TextView) mHeaderView.findViewById(R.id.listview_header_time);addHeaderView(mHeaderView);// 初始化頭部資訊mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {mHeaderViewHeight = mHeaderViewContent.getHeight();getViewTreeObserver().removeGlobalOnLayoutListener(this);}});}/** * 啟用或禁用拉重新整理功能 *  * @param enable */public void setPullRefreshEnable(boolean enable) {// 是否重新整理mEnablePullRefresh = enable;// 如果不是重新整理if (!mEnablePullRefresh) {mHeaderViewContent.setVisibility(View.INVISIBLE);} else {// 如果是重新整理mHeaderViewContent.setVisibility(View.VISIBLE);}}/** * 停止重新整理,複位標題視圖 */public void stopRefresh() {if (mPullRefreshing == true) {mPullRefreshing = false;resetHeaderHeight();}}/** * 設定最後重新整理時間 *  * @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) { // 未處於重新整理狀態,更新箭頭if (mHeaderView.getVisiableHeight() > mHeaderViewHeight) {mHeaderView.setState(PullListViewHeader.STATE_READY);} else {mHeaderView.setState(PullListViewHeader.STATE_NORMAL);}}setSelection(0); // 滾動到頂部每個時間}/** * 重設頭的高度 */private void resetHeaderHeight() {int height = mHeaderView.getVisiableHeight();if (height == 0) // 不顯示return;// 重新整理並且頭部不完全顯示,並且什麼也不做if (mPullRefreshing && height <= mHeaderViewHeight) {return;}int finalHeight = 0; // 預設值:滾動回駁回頭.// 重新整理,復原顯示所有的頭部資訊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();}switch (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)) {// 第一項是顯示,標題顯示或拉下來updateHeaderHeight(deltaY / OFFSET_RADIO);invokeOnScrolling();}break;default:mLastY = -1; // 重設if (getFirstVisiblePosition() == 0) {// 調用重新整理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.computeScrollOffset()) {if (mScrollBack == SCROLLBACK_HEADER) {mHeaderView.setVisiableHeight(mScroller.getCurrY());}postInvalidate();invokeOnScrolling();}super.computeScroll();}@Overridepublic void setOnScrollListener(OnScrollListener l) {mScrollListener = l;}@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {if (mScrollListener != null) {mScrollListener.onScrollStateChanged(view, scrollState);}}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {// 發送到使用者的接聽程式mTotalItemCount = totalItemCount;if (mScrollListener != null) {mScrollListener.onScroll(view, firstVisibleItem, visibleItemCount,totalItemCount);}}public void setXListViewListener(IXListViewListener l) {mListViewListener = l;}/** * 你可以聽列表視圖。OnScrollListener或這一個。它將調用onXScrolling當標題/頁尾滾動回。 */public interface OnXScrollListener extends OnScrollListener {public void onXScrolling(View view);}/** * 實現這個介面擷取/負載更多的事件,重新整理 */public interface IXListViewListener {public void onRefresh();public void onLoadMore();}}

在布局檔案中使用自訂的PullListView檔案

  <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>

在對應的activity類實現IXListViewListener介面,並繼承onRefresh和onLoadMore方法

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 交換名片 * @author yazhizhao * @time 2014-7-8上午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);// 用於安全退出MyApp.getInstance().addActivity(this);// 設定標題TextView tvTitleCommenCenter = (TextView) findViewById(R.id.tv_title);tvTitleCommenCenter.setText("發現");// 查看交換請求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));}});// 開啟線程messageThread = new MessageThread();messageThread.start();// 擷取附近人changeMgr = new ChangeMgr(ChangeCardAct.this, handler);changeMgr.findF();mHandler = new Handler();//是否收到交換資訊changeHint();}/** *  * @explaination 是否收到交換資訊 * @author yazhizhao * @time 2014-8-22上午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 新開啟線程一直請求有沒有人交換 * @author yazhizhao * @time 2014-7-25下午5:20:36 */class MessageThread extends Thread {public void run() {while (true) {try {// 請求有沒有人交換changeMgr.friendInfo();//是否收到交換資訊changeHint();Thread.sleep(1000 * 6);} catch (InterruptedException e) {e.printStackTrace();}}}}/** * 訊息處理機制 */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() {// 用於安全退出MyApp.getInstance().addActivity(this);super.onStart();}/** *  * @explaination 下拉重新整理設定上次時間 * @author yazhizhao * @time 2014-7-28上午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 下拉重新整理 * @author yazhizhao * @time 2014-7-28上午11:10:51 */@Overridepublic void onRefresh() {mHandler.postDelayed(new Runnable() {@Overridepublic void run() {onReload();changeHint();onLoad();}}, 2000);}/** *  * @explaination 載入更多 * @author yazhizhao * @time 2014-7-28上午11:10:51 */@Overridepublic void onLoadMore() {mHandler.postDelayed(new Runnable() {@Overridepublic void run() {}}, 2000);}@Overrideprotected void onResume() {// 把定位狀態設定為第一次定位changeMgr.findF();changeHint();super.onResume();}/** *  * @explaination 停止重新整理並設定時間 * @author yazhizhao * @time 2014-8-22上午10:23:47 */private void onLoad() {pointList.stopRefresh();pointList.setRefreshTime(getCurrentDate());}/** *  * @explaination 再次重新整理 * @author yazhizhao * @time 2014-8-22上午10:23:50 */public boolean onReload() {boolean result = true;changeMgr.findF();return result;}}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.