版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、175/175import 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.Abs
2、ListView.OnScrollListener;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.RelativeLayout;import android.widget.Scroller;import android.widget.TextView;import com.insthub.ecmobile.R;import android.os.Message;import android.os.Handler;public class XListView exten
3、ds ListView implements OnScrollListener private float mLastY = -1; / save event y private Scroller mScroller; / used for scroll back private OnScrollListener mScrollListener; / users scroll listener / the interface to trigger refresh and load more. private IXListViewListener mListViewListener; / - h
4、eader view private XListViewHeader mHeaderView; / header view content, use it to calculate the Headers height. And hide it / when disable pull refresh. private RelativeLayout mHeaderViewContent; private TextView mHeaderTimeView; private int mHeaderViewHeight; / header views height private boolean mE
5、nablePullRefresh = true; private boolean mPullRefreshing = false; / is refreashing. / - footer view private XListViewFooter mFooterView; private boolean mEnablePullLoad; private boolean mPullLoading; private boolean mIsFooterReady = false; / total list items, used to detect is at the bottom of listv
6、iew. private int mTotalItemCount; / for mScroller, scroll back from header or footer. private int mScrollBack; private final static int SCROLLBACK_HEADER = 0; private final static int SCROLLBACK_FOOTER = 1; private final static int SCROLL_DURATION = 400; / scroll back duration private final static i
7、nt PULL_LOAD_MORE_DELTA = 50; / when pull up = 50px / at bottom, trigger / load more. private final static float OFFSET_RADIO = 1.8f; / support iOS like pull / feature. private int id; /* * param context */ public XListView(Context context) super(context); initWithContext(context); public XListView(
8、Context context, AttributeSet attrs) super(context, attrs); initWithContext(context); public XListView(Context context, AttributeSet attrs, int defStyle) super(context, attrs, defStyle); initWithContext(context); private void initWithContext(Context context) mScroller = new Scroller(context, new Dec
9、elerateInterpolator(); / XListView need the scroll event, and it will dispatch the event to / users listener (as a proxy). super.setOnScrollListener(this); / init header view mHeaderView = new XListViewHeader(context); mHeaderViewContent = (RelativeLayout) mHeaderView .findViewById(R.id.xlistview_he
10、ader_content); mHeaderTimeView = (TextView) mHeaderView .findViewById(R.id.xlistview_header_time); addHeaderView(mHeaderView); / init footer view mFooterView = new XListViewFooter(context); / init header height mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener()
11、 Override public void onGlobalLayout() mHeaderViewHeight = mHeaderViewContent.getHeight(); getViewTreeObserver() .removeGlobalOnLayoutListener(this); ); Override public void setAdapter(ListAdapter adapter) / make sure XListViewFooter is the last footer view, and only add once. if (mIsFooterReady = f
12、alse) mIsFooterReady = true; addFooterView(mFooterView); super.setAdapter(adapter); /* * enable or disable pull down refresh feature. * * param enable */ public void setPullRefreshEnable(boolean enable) mEnablePullRefresh = enable; if (!mEnablePullRefresh) / disable, hide the content mHeaderViewCont
13、ent.setVisibility(View.INVISIBLE); else mHeaderViewContent.setVisibility(View.VISIBLE); /* * enable or disable pull up load more feature. * * param enable */ public void setPullLoadEnable(boolean enable) mEnablePullLoad = enable; if (!mEnablePullLoad) mFooterView.hide(); mFooterView.setOnClickListen
14、er(null); else mPullLoading = false; mFooterView.show(); mFooterView.setState(XListViewFooter.STATE_NORMAL); / both pull up and click will invoke load more. mFooterView.setOnClickListener(new OnClickListener() Override public void onClick(View v) startLoadMore(); ); /* * stop refresh, reset header v
15、iew. */ public void stopRefresh() if (mPullRefreshing = true) mPullRefreshing = false; resetHeaderHeight(); /* * stop load more, reset footer view. */ public void stopLoadMore() if (mPullLoading = true) mPullLoading = false; mFooterView.setState(XListViewFooter.STATE_NORMAL); mFooterView.setEnabled(
16、true); /* * set last refresh time * * param time */ public void setRefreshTime() /mHeaderTimeView.setText(new Date().toLocaleString(); mHeaderTimeView.setText(date(); / 獲取系統(tǒng)時(shí)間 public String date() SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss); Date date = new Date(); String t = sd
17、f.format(date); return t; private void invokeOnScrolling() if (mScrollListener instanceof OnXScrollListener) OnXScrollListener l = (OnXScrollListener) mScrollListener; l.onXScrolling(this); private void updateHeaderHeight(float delta) mHeaderView.setVisiableHeight(int) delta + mHeaderView.getVisiabl
18、eHeight(); if (mEnablePullRefresh & !mPullRefreshing) / 未處于刷新狀態(tài),更新箭頭 if (mHeaderView.getVisiableHeight() mHeaderViewHeight) mHeaderView.setState(XListViewHeader.STATE_READY); else mHeaderView.setState(XListViewHeader.STATE_NORMAL); setSelection(0); / scroll to top each time /* * reset header views h
19、eight. */ private void resetHeaderHeight() int height = mHeaderView.getVisiableHeight(); if (height = 0) / not visible. return; / refreshing and header isnt shown fully. do nothing. if (mPullRefreshing & height mHeaderViewHeight) finalHeight = mHeaderViewHeight; mScrollBack = SCROLLBACK_HEADER; mScr
20、oller.startScroll(0, height, 0, finalHeight - height, SCROLL_DURATION); / trigger computeScroll invalidate(); private void updateFooterHeight(float delta) int height = mFooterView.getBottomMargin() + (int) delta; if (mEnablePullLoad & !mPullLoading) if (height PULL_LOAD_MORE_DELTA) / height enough t
21、o invoke load / more. mFooterView.setState(XListViewFooter.STATE_READY); else mFooterView.setState(XListViewFooter.STATE_NORMAL); mFooterView.setBottomMargin(height);/ setSelection(mTotalItemCount - 1); / scroll to bottom private void resetFooterHeight() int bottomMargin = mFooterView.getBottomMargi
22、n(); if (bottomMargin 0) mScrollBack = SCROLLBACK_FOOTER; mScroller.startScroll(0, bottomMargin, 0, -bottomMargin, SCROLL_DURATION); invalidate(); public void startLoadMore() mPullLoading = true; mFooterView.setState(XListViewFooter.STATE_LOADING); if (mListViewListener != null) mListViewListener.on
23、LoadMore(id); mFooterView.setEnabled(false); Handler mHandler = new Handler() Override public void handleMessage(Message msg) super.handleMessage(msg); XListView.this.stopLoadMore(); ; mHandler.sendEmptyMessageDelayed(0, 4000); Override public boolean onTouchEvent(MotionEvent ev) if (mLastY = -1) mL
24、astY = 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) / the first item
25、 is showing, header has shown or pull down. updateHeaderHeight(deltaY / OFFSET_RADIO); invokeOnScrolling(); else if (getLastVisiblePosition() = mTotalItemCount - 1 & (mFooterView.getBottomMargin() 0 | deltaY mHeaderViewHeight) mPullRefreshing = true; mHeaderView.setState(XListViewHeader.STATE_REFRES
26、HING); if (mListViewListener != null) mListViewListener.onRefresh(id); Handler mHandler = new Handler() Override public void handleMessage(Message msg) super.handleMessage(msg); XListView.this.stopRefresh(); ; mHandler.sendEmptyMessageDelayed(0, 4000); resetHeaderHeight(); else if (getLastVisiblePos
27、ition() = mTotalItemCount - 1) / invoke load more. if (mEnablePullLoad & mFooterView.getBottomMargin() PULL_LOAD_MORE_DELTA) if(mFooterView.isEnabled() startLoadMore(); resetFooterHeight(); break; return super.onTouchEvent(ev); Override public void computeScroll() if (mSputeScrollOffset() if (mScrol
28、lBack = SCROLLBACK_HEADER) mHeaderView.setVisiableHeight(mScroller.getCurrY(); else mFooterView.setBottomMargin(mScroller.getCurrY(); postInvalidate(); invokeOnScrolling(); puteScroll(); Override public void setOnScrollListener(OnScrollListener l) mScrollListener = l; Override public void onScrollSt
29、ateChanged(AbsListView view, int scrollState) if (mScrollListener != null) mScrollListener.onScrollStateChanged(view, scrollState); Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) / send to users listener mTotalItemCount = totalItemCoun
30、t; if (mScrollListener != null) mScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount); public void setXListViewListener(IXListViewListener l,int id) mListViewListener = l; this.id = id; /* * you can listen ListView.OnScrollListener or this one. it will invoke * onXScroll
31、ing when header/footer scroll back. */ public interface OnXScrollListener extends OnScrollListener public void onXScrolling(View view); /* * implements this interface to get refresh/load more event. */ public interface IXListViewListener public void onRefresh(int id); public void onLoadMore(int id);
32、 import android.content.Context;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.ViewTreeObserver.OnGlobalLayoutListener;import android.view.animation.DecelerateInterpolator;import androi
33、d.widget.AbsListView;import android.widget.AbsListView.OnScrollListener;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.RelativeLayout;import android.widget.Scroller;import android.widget.TextView;import com.insthub.ecmobile.R;public class XListViewCart extends
34、 ListView implements OnScrollListener private float mLastY = -1; / save event y private Scroller mScroller; / used for scroll back private OnScrollListener mScrollListener; / users scroll listener / the interface to trigger refresh and load more. private IXListViewListenerCart mListViewListener; / -
35、 header view private XListViewHeaderCart mHeaderView; / header view content, use it to calculate the Headers height. And hide it / when disable pull refresh. private RelativeLayout mHeaderViewContent; private TextView mHeaderTimeView; private int mHeaderViewHeight; / header views height private bool
36、ean mEnablePullRefresh = true; private boolean mPullRefreshing = false; / is refreashing. / - footer view private XListViewFooter mFooterView; private boolean mEnablePullLoad; private boolean mPullLoading; private boolean mIsFooterReady = false; / total list items, used to detect is at the bottom of
37、 listview. private int mTotalItemCount; / for mScroller, scroll back from header or footer. private int mScrollBack; private final static int SCROLLBACK_HEADER = 0; private final static int SCROLLBACK_FOOTER = 1; private final static int SCROLL_DURATION = 400; / scroll back duration private final st
38、atic int PULL_LOAD_MORE_DELTA = 50; / when pull up = 50px / at bottom, trigger / load more. private final static float OFFSET_RADIO = 1.8f; / support iOS like pull / feature. private int id; /* * param context */ public XListViewCart(Context context) super(context); initWithContext(context); public
39、XListViewCart(Context context, AttributeSet attrs) super(context, attrs); initWithContext(context); public XListViewCart(Context context, AttributeSet attrs, int defStyle) super(context, attrs, defStyle); initWithContext(context); private void initWithContext(Context context) mScroller = new Scrolle
40、r(context, new DecelerateInterpolator(); / XListView need the scroll event, and it will dispatch the event to / users listener (as a proxy). super.setOnScrollListener(this); / init header view mHeaderView = new XListViewHeaderCart(context); mHeaderViewContent = (RelativeLayout) mHeaderView .findView
41、ById(R.id.xlistview_header_content); mHeaderTimeView = (TextView) mHeaderView .findViewById(R.id.xlistview_header_time); addHeaderView(mHeaderView); / init footer view mFooterView = new XListViewFooter(context); / init header height mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener( new On
42、GlobalLayoutListener() Override public void onGlobalLayout() mHeaderViewHeight = mHeaderViewContent.getHeight(); getViewTreeObserver() .removeGlobalOnLayoutListener(this); ); Override public void setAdapter(ListAdapter adapter) / make sure XListViewFooter is the last footer view, and only add once.
43、if (mIsFooterReady = false) mIsFooterReady = true; addFooterView(mFooterView); super.setAdapter(adapter); /* * enable or disable pull down refresh feature. * * param enable */ public void setPullRefreshEnable(boolean enable) mEnablePullRefresh = enable; if (!mEnablePullRefresh) / disable, hide the c
44、ontent mHeaderViewContent.setVisibility(View.INVISIBLE); else mHeaderViewContent.setVisibility(View.VISIBLE); /* * enable or disable pull up load more feature. * * param enable */ public void setPullLoadEnable(boolean enable) mEnablePullLoad = enable; if (!mEnablePullLoad) mFooterView.hide(); mFoote
45、rView.setOnClickListener(null); else mPullLoading = false; mFooterView.show(); mFooterView.setState(XListViewFooter.STATE_NORMAL); / both pull up and click will invoke load more. mFooterView.setOnClickListener(new OnClickListener() Override public void onClick(View v) startLoadMore(); ); /* * stop r
46、efresh, reset header view. */ public void stopRefresh() if (mPullRefreshing = true) mPullRefreshing = false; resetHeaderHeight(); /* * stop load more, reset footer view. */ public void stopLoadMore() if (mPullLoading = true) mPullLoading = false; mFooterView.setState(XListViewFooter.STATE_NORMAL); m
47、FooterView.setEnabled(true); /* * set last refresh time * * param time */ public void setRefreshTime() mHeaderTimeView.setText(date(); / 獲取系統(tǒng)時(shí)間 public String date() SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss); Date date = new Date(); String t = sdf.format(date); return t; privat
48、e 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 (mEnablePullRefres
49、h & !mPullRefreshing) / 未處于刷新狀態(tài),更新箭頭 if (mHeaderView.getVisiableHeight() mHeaderViewHeight) mHeaderView.setState(XListViewHeader.STATE_READY); else mHeaderView.setState(XListViewHeader.STATE_NORMAL); setSelection(0); / scroll to top each time /* * reset header views height. */ private void resetHead
50、erHeight() int height = mHeaderView.getVisiableHeight(); if (height = 0) / not visible. return; / refreshing and header isnt shown fully. do nothing. if (mPullRefreshing & height mHeaderViewHeight) finalHeight = mHeaderViewHeight; mScrollBack = SCROLLBACK_HEADER; mScroller.startScroll(0, height, 0,
51、finalHeight - height, SCROLL_DURATION); / trigger computeScroll invalidate(); private void updateFooterHeight(float delta) int height = mFooterView.getBottomMargin() + (int) delta; if (mEnablePullLoad & !mPullLoading) if (height PULL_LOAD_MORE_DELTA) / height enough to invoke load / more. mFooterVie
52、w.setState(XListViewFooter.STATE_READY); else mFooterView.setState(XListViewFooter.STATE_NORMAL); mFooterView.setBottomMargin(height);/ setSelection(mTotalItemCount - 1); / scroll to bottom private void resetFooterHeight() int bottomMargin = mFooterView.getBottomMargin(); if (bottomMargin 0) mScroll
53、Back = SCROLLBACK_FOOTER; mScroller.startScroll(0, bottomMargin, 0, -bottomMargin, SCROLL_DURATION); invalidate(); public void startLoadMore() mPullLoading = true; mFooterView.setState(XListViewFooter.STATE_LOADING); if (mListViewListener != null) mListViewListener.onLoadMore(id); mFooterView.setEna
54、bled(false); Handler mHandler = new Handler() Override public void handleMessage(Message msg) super.handleMessage(msg); XListViewCart.this.stopLoadMore(); ; mHandler.sendEmptyMessageDelayed(0, 4000); Override public boolean onTouchEvent(MotionEvent ev) if (mLastY = -1) mLastY = ev.getRawY(); switch
55、(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 showing, header has show
56、n or pull down. updateHeaderHeight(deltaY / OFFSET_RADIO); invokeOnScrolling(); else if (getLastVisiblePosition() = mTotalItemCount - 1 & (mFooterView.getBottomMargin() 0 | deltaY mHeaderViewHeight) mPullRefreshing = true; mHeaderView.setState(XListViewHeader.STATE_REFRESHING); if (mListViewListener
57、 != null) mListViewListener.onRefresh(id); Handler mHandler = new Handler() Override public void handleMessage(Message msg) super.handleMessage(msg); XListViewCart.this.stopRefresh(); ; mHandler.sendEmptyMessageDelayed(0, 4000); resetHeaderHeight(); else if (getLastVisiblePosition() = mTotalItemCoun
58、t - 1) / invoke load more. if (mEnablePullLoad & mFooterView.getBottomMargin() PULL_LOAD_MORE_DELTA) if(mFooterView.isEnabled() startLoadMore(); resetFooterHeight(); break; return super.onTouchEvent(ev); Override public void computeScroll() if (mSputeScrollOffset() if (mScrollBack = SCROLLBACK_HEADE
59、R) mHeaderView.setVisiableHeight(mScroller.getCurrY(); else mFooterView.setBottomMargin(mScroller.getCurrY(); postInvalidate(); invokeOnScrolling(); puteScroll(); Override public void setOnScrollListener(OnScrollListener l) mScrollListener = l; Override public void onScrollStateChanged(AbsListView v
60、iew, int scrollState) if (mScrollListener != null) mScrollListener.onScrollStateChanged(view, scrollState); Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) / send to users listener mTotalItemCount = totalItemCount; if (mScrollListener !
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025中國郵政集團(tuán)公司江蘇省常熟市分公司招聘快包投遞員高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025中國聯(lián)通福建省分公司招聘(414人+)高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025中國移動黑龍江公司校園招聘224人高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025中國電建(德國)限公司招聘1人高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025中國電信湖北潛江分公司招聘4人高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025下半年浙江麗水市青田縣招聘國企業(yè)工作人員擬聘用(三)閱讀模式高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025下半年廣西桂林市事業(yè)單位招聘應(yīng)征入伍大學(xué)畢業(yè)生153人高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025下半年山東通匯資本投資集團(tuán)限公司社會招聘7人高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025下半年四川省江油市事業(yè)單位招聘95人歷年高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025下半年四川南充市高坪區(qū)事業(yè)單位招聘47人高頻重點(diǎn)提升(共500題)附帶答案詳解
- 社區(qū)開展反詐工作方案
- 城市露營項(xiàng)目商業(yè)計(jì)劃書
- 建筑垃圾清運(yùn)重點(diǎn)分析報(bào)告
- 11396-國家開放大學(xué)2023年春期末統(tǒng)一考試《藥事管理與法規(guī)(本)》答案
- NB-T 47013.7-2012(JB-T 4730.7) 4730.7 承壓設(shè)備無損檢測 第7部分:目視檢測
- 網(wǎng)絡(luò)安全與信息保密培訓(xùn)
- 天津市四校2022-2023學(xué)年高二上學(xué)期期末聯(lián)考數(shù)學(xué)試題(原卷版)
- 2023年建筑工程管理經(jīng)理年終總結(jié)及年后展望
- 小學(xué)數(shù)學(xué)問題解決能力的培養(yǎng)策略
- 京歌《故鄉(xiāng)是北京》的藝術(shù)特征及演唱分析
- 機(jī)動庫護(hù)士培訓(xùn)課件
評論
0/150
提交評論