zoukankan      html  css  js  c++  java
  • Android学习之listview的下拉刷新、上拉载入

    本例是在上例的基础上完成的。本例实现的listview上拉载入、下拉刷新功能,是在开源网站上别人写好的listview,主要是对listview的控件进行重写,添加了footer和header。

    1.listview_footer

    listview_footer是listview的底部。

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="fill_parent"
     4     android:layout_height="wrap_content" >
     5 
     6     <RelativeLayout
     7         android:id="@+id/xlistview_footer_content"
     8         android:layout_width="fill_parent"
     9         android:layout_height="wrap_content"
    10         android:padding="10dp" >
    11 
    12         <ProgressBar
    13             android:id="@+id/xlistview_footer_progressbar"
    14             android:layout_width="wrap_content"
    15             android:layout_height="wrap_content"
    16             android:layout_centerInParent="true"
    17             android:visibility="invisible" />
    18 
    19         <TextView
    20             android:id="@+id/xlistview_footer_hint_textview"
    21             android:layout_width="wrap_content"
    22             android:layout_height="wrap_content"
    23             android:layout_centerInParent="true"
    24             android:text="@string/xlistview_footer_hint_normal" />
    25     </RelativeLayout>
    26 
    27 </LinearLayout>
    lsitview_footer

    listview_footer由ProgressBar和textview组成。用户点击textview或者上拉的时候,会触发监听事件,实现更多数据的载入。

     1 public final static int STATE_NORMAL = 0;
     2     public final static int STATE_READY = 1;
     3     public final static int STATE_LOADING = 2;
     4 
     5     private Context mContext;
     6 
     7     private View mContentView;
     8     private View mProgressBar;
     9     private TextView mHintView;
    10     
    11     public XListViewFooter(Context context) {
    12         super(context);
    13         initView(context);
    14     }
    15     
    16     public XListViewFooter(Context context, AttributeSet attrs) {
    17         super(context, attrs);
    18         initView(context);
    19     }
    20 
    21     
    22     public void setState(int state) {
    23         mHintView.setVisibility(View.INVISIBLE);
    24         mProgressBar.setVisibility(View.INVISIBLE);
    25         mHintView.setVisibility(View.INVISIBLE);
    26         if (state == STATE_READY) {
    27             mHintView.setVisibility(View.VISIBLE);
    28             mHintView.setText(R.string.xlistview_footer_hint_ready);
    29         } else if (state == STATE_LOADING) {
    30             mProgressBar.setVisibility(View.VISIBLE);
    31         } else {
    32             mHintView.setVisibility(View.VISIBLE);
    33             mHintView.setText(R.string.xlistview_footer_hint_normal);
    34         }
    35     }
    36     
    37     public void setBottomMargin(int height) {
    38         if (height < 0) return ;
    39         LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams();
    40         lp.bottomMargin = height;
    41         mContentView.setLayoutParams(lp);
    42     }
    43     
    44     public int getBottomMargin() {
    45         LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams();
    46         return lp.bottomMargin;
    47     }
    48     
    49     /**
    50      * normal status
    51      */
    52     public void normal() {
    53         mHintView.setVisibility(View.VISIBLE);
    54         mProgressBar.setVisibility(View.GONE);
    55     }
    56     
    57     
    58     /**
    59      * loading status 
    60      */
    61     public void loading() {
    62         mHintView.setVisibility(View.GONE);
    63         mProgressBar.setVisibility(View.VISIBLE);
    64     }
    65     
    66     /**
    67      * hide footer when disable pull load more
    68      */
    69     public void hide() {
    70         LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams();
    71         lp.height = 0;
    72         mContentView.setLayoutParams(lp);
    73     }
    74     
    75     /**
    76      * show footer
    77      */
    78     public void show() {
    79         LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams();
    80         lp.height = LayoutParams.WRAP_CONTENT;
    81         mContentView.setLayoutParams(lp);
    82     }
    83     
    84     private void initView(Context context) {
    85         mContext = context;
    86         LinearLayout moreView = (LinearLayout)LayoutInflater.from(mContext).inflate(R.layout.xlistview_footer, null);
    87         addView(moreView);
    88         moreView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    89         
    90         mContentView = moreView.findViewById(R.id.xlistview_footer_content);
    91         mProgressBar = moreView.findViewById(R.id.xlistview_footer_progressbar);
    92         mHintView = (TextView)moreView.findViewById(R.id.xlistview_footer_hint_textview);
    93     }
    listview_footer

    后台代码定义了listview_footer的状态变化,包括加载、停止加载等,实现了对事件的监听。

    2.listview_header

    listview_header是listview显示的时候头部文件。用户下拉的时候,实现数据的重新载入。

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="fill_parent"
     4     android:layout_height="wrap_content"
     5     android:gravity="bottom" >
     6 
     7     <RelativeLayout
     8         android:id="@+id/xlistview_header_content"
     9         android:layout_width="fill_parent"
    10         android:layout_height="60dp" >
    11 
    12         <LinearLayout
    13             android:layout_width="wrap_content"
    14             android:layout_height="wrap_content"
    15             android:layout_centerInParent="true"
    16             android:gravity="center"
    17             android:orientation="vertical" android:id="@+id/xlistview_header_text">
    18 
    19             <TextView
    20                 android:id="@+id/xlistview_header_hint_textview"
    21                 android:layout_width="wrap_content"
    22                 android:layout_height="wrap_content"
    23                 android:text="@string/xlistview_header_hint_normal" />
    24 
    25             <LinearLayout
    26                 android:layout_width="wrap_content"
    27                 android:layout_height="wrap_content"
    28                 android:layout_marginTop="3dp" >
    29 
    30                 <TextView
    31                     android:layout_width="wrap_content"
    32                     android:layout_height="wrap_content"
    33                     android:text="@string/xlistview_header_last_time"
    34                     android:textSize="12sp" />
    35 
    36                 <TextView
    37                     android:id="@+id/xlistview_header_time"
    38                     android:layout_width="wrap_content"
    39                     android:layout_height="wrap_content"
    40                     android:textSize="12sp" />
    41             </LinearLayout>
    42         </LinearLayout>
    43 
    44         <ImageView
    45             android:id="@+id/xlistview_header_arrow"
    46             android:layout_width="wrap_content"
    47             android:layout_height="wrap_content"
    48             android:layout_alignLeft="@id/xlistview_header_text"
    49             android:layout_centerVertical="true"
    50             android:layout_marginLeft="-35dp"
    51             android:src="@drawable/xlistview_arrow" />
    52 
    53         <ProgressBar
    54             android:id="@+id/xlistview_header_progressbar"
    55             android:layout_width="30dp"
    56             android:layout_height="30dp"
    57             android:layout_alignLeft="@id/xlistview_header_text"
    58             android:layout_centerVertical="true"
    59             android:layout_marginLeft="-40dp"
    60             android:visibility="invisible" />
    61     </RelativeLayout>
    62 
    63 </LinearLayout>
    listview_header

    xml部分由textview和imageview组成。

      1 private LinearLayout mContainer;
      2     private ImageView mArrowImageView;
      3     private ProgressBar mProgressBar;
      4     private TextView mHintTextView;
      5     private int mState = STATE_NORMAL;
      6 
      7     private Animation mRotateUpAnim;
      8     private Animation mRotateDownAnim;
      9     
     10     private final int ROTATE_ANIM_DURATION = 180;
     11     
     12     public final static int STATE_NORMAL = 0;
     13     public final static int STATE_READY = 1;
     14     public final static int STATE_REFRESHING = 2;
     15 
     16     public XListViewHeader(Context context) {
     17         super(context);
     18         initView(context);
     19     }
     20 
     21     /**
     22      * @param context
     23      * @param attrs
     24      */
     25     public XListViewHeader(Context context, AttributeSet attrs) {
     26         super(context, attrs);
     27         initView(context);
     28     }
     29 
     30     private void initView(Context context) {
     31         // 鍒濆�鎯呭喌锛岃�缃�笅鎷夊埛鏂皏iew楂樺害涓?
     32         LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
     33                 LayoutParams.FILL_PARENT, 0);
     34         mContainer = (LinearLayout) LayoutInflater.from(context).inflate(
     35                 R.layout.xlistview_header, null);
     36         addView(mContainer, lp);
     37         setGravity(Gravity.BOTTOM);
     38 
     39         mArrowImageView = (ImageView)findViewById(R.id.xlistview_header_arrow);
     40         mHintTextView = (TextView)findViewById(R.id.xlistview_header_hint_textview);
     41         mProgressBar = (ProgressBar)findViewById(R.id.xlistview_header_progressbar);
     42         
     43         mRotateUpAnim = new RotateAnimation(0.0f, -180.0f,
     44                 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
     45                 0.5f);
     46         mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
     47         mRotateUpAnim.setFillAfter(true);
     48         mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f,
     49                 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
     50                 0.5f);
     51         mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
     52         mRotateDownAnim.setFillAfter(true);
     53     }
     54 
     55     public void setState(int state) {
     56         if (state == mState) return ;
     57         
     58         if (state == STATE_REFRESHING) {    // 鏄剧ず杩涘害
     59             mArrowImageView.clearAnimation();
     60             mArrowImageView.setVisibility(View.INVISIBLE);
     61             mProgressBar.setVisibility(View.VISIBLE);
     62         } else {    // 鏄剧ず绠�ご鍥剧墖
     63             mArrowImageView.setVisibility(View.VISIBLE);
     64             mProgressBar.setVisibility(View.INVISIBLE);
     65         }
     66         
     67         switch(state){
     68         case STATE_NORMAL:
     69             if (mState == STATE_READY) {
     70                 mArrowImageView.startAnimation(mRotateDownAnim);
     71             }
     72             if (mState == STATE_REFRESHING) {
     73                 mArrowImageView.clearAnimation();
     74             }
     75             mHintTextView.setText(R.string.xlistview_header_hint_normal);
     76             break;
     77         case STATE_READY:
     78             if (mState != STATE_READY) {
     79                 mArrowImageView.clearAnimation();
     80                 mArrowImageView.startAnimation(mRotateUpAnim);
     81                 mHintTextView.setText(R.string.xlistview_header_hint_ready);
     82             }
     83             break;
     84         case STATE_REFRESHING:
     85             mHintTextView.setText(R.string.xlistview_header_hint_loading);
     86             break;
     87             default:
     88         }
     89         
     90         mState = state;
     91     }
     92     
     93     public void setVisiableHeight(int height) {
     94         if (height < 0)
     95             height = 0;
     96         LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContainer
     97                 .getLayoutParams();
     98         lp.height = height;
     99         mContainer.setLayoutParams(lp);
    100     }
    101 
    102     public int getVisiableHeight() {
    103         return mContainer.getHeight();
    104     }
    listview_header

    后台通过状态的改变,控制header的高度。

    3.xlistview

    xlistview是对listview控件的重写

      1 public class XListView extends ListView implements OnScrollListener {
      2 
      3     private float mLastY = -1; // save event y
      4     private Scroller mScroller; // used for scroll back
      5     private OnScrollListener mScrollListener; // user's scroll listener
      6 
      7     // the interface to trigger refresh and load more.
      8     private IXListViewListener mListViewListener;
      9 
     10     // -- header view
     11     private XListViewHeader mHeaderView;
     12     // header view content, use it to calculate the Header's height. And hide it
     13     // when disable pull refresh.
     14     private RelativeLayout mHeaderViewContent;
     15     private TextView mHeaderTimeView;
     16     private int mHeaderViewHeight; // header view's height
     17     private boolean mEnablePullRefresh = true;
     18     private boolean mPullRefreshing = false; // is refreashing.
     19 
     20     // -- footer view
     21     private XListViewFooter mFooterView;
     22     private boolean mEnablePullLoad;
     23     private boolean mPullLoading;
     24     private boolean mIsFooterReady = false;
     25 
     26     // total list items, used to detect is at the bottom of listview.
     27     private int mTotalItemCount;
     28 
     29     // for mScroller, scroll back from header or footer.
     30     private int mScrollBack;
     31     private final static int SCROLLBACK_HEADER = 0;
     32     private final static int SCROLLBACK_FOOTER = 1;
     33 
     34     private final static int SCROLL_DURATION = 400; // scroll back duration
     35     private final static int PULL_LOAD_MORE_DELTA = 50; // when pull up >= 50px
     36                                                         // at bottom, trigger
     37                                                         // load more.
     38     private final static float OFFSET_RADIO = 1.8f; // support iOS like pull
     39                                                     // feature.
     40 
     41     /**
     42      * @param context
     43      */
     44     public XListView(Context context) {
     45         super(context);
     46         initWithContext(context);
     47     }
     48 
     49     public XListView(Context context, AttributeSet attrs) {
     50         super(context, attrs);
     51         initWithContext(context);
     52     }
     53 
     54     public XListView(Context context, AttributeSet attrs, int defStyle) {
     55         super(context, attrs, defStyle);
     56         initWithContext(context);
     57     }
     58 
     59     private void initWithContext(Context context) {
     60         mScroller = new Scroller(context, new DecelerateInterpolator());
     61         // XListView need the scroll event, and it will dispatch the event to
     62         // user's listener (as a proxy).
     63         super.setOnScrollListener(this);
     64 
     65         // init header view
     66         mHeaderView = new XListViewHeader(context);
     67         mHeaderViewContent = (RelativeLayout) mHeaderView
     68                 .findViewById(R.id.xlistview_header_content);
     69         mHeaderTimeView = (TextView) mHeaderView
     70                 .findViewById(R.id.xlistview_header_time);
     71         addHeaderView(mHeaderView);
     72 
     73         // init footer view
     74         mFooterView = new XListViewFooter(context);
     75 
     76         // init header height
     77         mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(
     78                 new OnGlobalLayoutListener() {
     79                     @SuppressWarnings("deprecation")
     80                     @Override
     81                     public void onGlobalLayout() {
     82                         mHeaderViewHeight = mHeaderViewContent.getHeight();
     83                         getViewTreeObserver()
     84                                 .removeGlobalOnLayoutListener(this);
     85                     }
     86                 });
     87     }
     88 
     89     @Override
     90     public void setAdapter(ListAdapter adapter) {
     91         // make sure XListViewFooter is the last footer view, and only add once.
     92         if (mIsFooterReady == false) {
     93             mIsFooterReady = true;
     94             addFooterView(mFooterView);
     95         }
     96         super.setAdapter(adapter);
     97     }
     98 
     99     /**
    100      * enable or disable pull down refresh feature.
    101      * 
    102      * @param enable
    103      */
    104     public void setPullRefreshEnable(boolean enable) {
    105         mEnablePullRefresh = enable;
    106         if (!mEnablePullRefresh) { // disable, hide the content
    107             mHeaderViewContent.setVisibility(View.INVISIBLE);
    108         } else {
    109             mHeaderViewContent.setVisibility(View.VISIBLE);
    110         }
    111     }
    112 
    113     /**
    114      * enable or disable pull up load more feature.
    115      * 
    116      * @param enable
    117      */
    118     public void setPullLoadEnable(boolean enable) {
    119         mEnablePullLoad = enable;
    120         if (!mEnablePullLoad) {
    121             mFooterView.hide();
    122             mFooterView.setOnClickListener(null);
    123         } else {
    124             mPullLoading = false;
    125             mFooterView.show();
    126             mFooterView.setState(XListViewFooter.STATE_NORMAL);
    127             // both "pull up" and "click" will invoke load more.
    128             mFooterView.setOnClickListener(new OnClickListener() {
    129                 @Override
    130                 public void onClick(View v) {
    131                     startLoadMore();
    132                 }
    133             });
    134         }
    135     }
    136 
    137     /**
    138      * stop refresh, reset header view.
    139      */
    140     public void stopRefresh() {
    141         if (mPullRefreshing == true) {
    142             mPullRefreshing = false;
    143             resetHeaderHeight();
    144         }
    145     }
    146 
    147     /**
    148      * stop load more, reset footer view.
    149      */
    150     public void stopLoadMore() {
    151         if (mPullLoading == true) {
    152             mPullLoading = false;
    153             mFooterView.setState(XListViewFooter.STATE_NORMAL);
    154         }
    155     }
    156 
    157     /**
    158      * set last refresh time
    159      * 
    160      * @param time
    161      */
    162     public void setRefreshTime(String time) {
    163         mHeaderTimeView.setText(time);
    164     }
    165 
    166     private void invokeOnScrolling() {
    167         if (mScrollListener instanceof OnXScrollListener) {
    168             OnXScrollListener l = (OnXScrollListener) mScrollListener;
    169             l.onXScrolling(this);
    170         }
    171     }
    172 
    173     private void updateHeaderHeight(float delta) {
    174         mHeaderView.setVisiableHeight((int) delta
    175                 + mHeaderView.getVisiableHeight());
    176         if (mEnablePullRefresh && !mPullRefreshing) { // 鏈��浜庡埛鏂扮姸鎬侊紝鏇存柊绠�ご
    177             if (mHeaderView.getVisiableHeight() > mHeaderViewHeight) {
    178                 mHeaderView.setState(XListViewHeader.STATE_READY);
    179             } else {
    180                 mHeaderView.setState(XListViewHeader.STATE_NORMAL);
    181             }
    182         }
    183         setSelection(0); // scroll to top each time
    184     }
    185 
    186     /**
    187      * reset header view's height.
    188      */
    189     private void resetHeaderHeight() {
    190         int height = mHeaderView.getVisiableHeight();
    191         if (height == 0) // not visible.
    192             return;
    193         // refreshing and header isn't shown fully. do nothing.
    194         if (mPullRefreshing && height <= mHeaderViewHeight) {
    195             return;
    196         }
    197         int finalHeight = 0; // default: scroll back to dismiss header.
    198         // is refreshing, just scroll back to show all the header.
    199         if (mPullRefreshing && height > mHeaderViewHeight) {
    200             finalHeight = mHeaderViewHeight;
    201         }
    202         mScrollBack = SCROLLBACK_HEADER;
    203         mScroller.startScroll(0, height, 0, finalHeight - height,
    204                 SCROLL_DURATION);
    205         // trigger computeScroll
    206         invalidate();
    207     }
    208 
    209     private void updateFooterHeight(float delta) {
    210         int height = mFooterView.getBottomMargin() + (int) delta;
    211         if (mEnablePullLoad && !mPullLoading) {
    212             if (height > PULL_LOAD_MORE_DELTA) { // height enough to invoke load
    213                                                     // more.
    214                 mFooterView.setState(XListViewFooter.STATE_READY);
    215             } else {
    216                 mFooterView.setState(XListViewFooter.STATE_NORMAL);
    217             }
    218         }
    219         mFooterView.setBottomMargin(height);
    220 
    221         // setSelection(mTotalItemCount - 1); // scroll to bottom
    222     }
    223 
    224     private void resetFooterHeight() {
    225         int bottomMargin = mFooterView.getBottomMargin();
    226         if (bottomMargin > 0) {
    227             mScrollBack = SCROLLBACK_FOOTER;
    228             mScroller.startScroll(0, bottomMargin, 0, -bottomMargin,
    229                     SCROLL_DURATION);
    230             invalidate();
    231         }
    232     }
    233 
    234     private void startLoadMore() {
    235         mPullLoading = true;
    236         mFooterView.setState(XListViewFooter.STATE_LOADING);
    237         if (mListViewListener != null) {
    238             mListViewListener.onLoadMore();
    239         }
    240     }
    241 
    242     @Override
    243     public boolean onTouchEvent(MotionEvent ev) {
    244         if (mLastY == -1) {
    245             mLastY = ev.getRawY();
    246         }
    247 
    248         switch (ev.getAction()) {
    249         case MotionEvent.ACTION_DOWN:
    250             mLastY = ev.getRawY();
    251             break;
    252         case MotionEvent.ACTION_MOVE:
    253             final float deltaY = ev.getRawY() - mLastY;
    254             mLastY = ev.getRawY();
    255             System.out.println(getFirstVisiblePosition() + "---->"
    256                     + getLastVisiblePosition());
    257             if (getFirstVisiblePosition() == 0
    258                     && (mHeaderView.getVisiableHeight() > 0 || deltaY > 0)) {
    259                 // the first item is showing, header has shown or pull down.
    260                 updateHeaderHeight(deltaY / OFFSET_RADIO);
    261                 invokeOnScrolling();
    262             } else if (getLastVisiblePosition() == mTotalItemCount - 1
    263                     && (mFooterView.getBottomMargin() > 0 || deltaY < 0)) {
    264                 // last item, already pulled up or want to pull up.
    265                 updateFooterHeight(-deltaY / OFFSET_RADIO);
    266             }
    267             break;
    268         default:
    269             mLastY = -1; // reset
    270             if (getFirstVisiblePosition() == 0) {
    271                 // invoke refresh
    272                 if (mEnablePullRefresh
    273                         && mHeaderView.getVisiableHeight() > mHeaderViewHeight) {
    274                     mPullRefreshing = true;
    275                     mHeaderView.setState(XListViewHeader.STATE_REFRESHING);
    276                     if (mListViewListener != null) {
    277                         mListViewListener.onRefresh();
    278                     }
    279                 }
    280                 resetHeaderHeight();
    281             }
    282             if (getLastVisiblePosition() == mTotalItemCount - 1) {
    283                 // invoke load more.
    284                 if (mEnablePullLoad
    285                         && mFooterView.getBottomMargin() > PULL_LOAD_MORE_DELTA) {
    286                     startLoadMore();
    287                 }
    288                 resetFooterHeight();
    289             }
    290             break;
    291         }
    292         return super.onTouchEvent(ev);
    293     }
    294 
    295     @Override
    296     public void computeScroll() {
    297         if (mScroller.computeScrollOffset()) {
    298             if (mScrollBack == SCROLLBACK_HEADER) {
    299                 mHeaderView.setVisiableHeight(mScroller.getCurrY());
    300             } else {
    301                 mFooterView.setBottomMargin(mScroller.getCurrY());
    302             }
    303             postInvalidate();
    304             invokeOnScrolling();
    305         }
    306         super.computeScroll();
    307     }
    308 
    309     @Override
    310     public void setOnScrollListener(OnScrollListener l) {
    311         mScrollListener = l;
    312     }
    313 
    314     @Override
    315     public void onScrollStateChanged(AbsListView view, int scrollState) {
    316         if (mScrollListener != null) {
    317             mScrollListener.onScrollStateChanged(view, scrollState);
    318         }
    319     }
    320 
    321     @Override
    322     public void onScroll(AbsListView view, int firstVisibleItem,
    323             int visibleItemCount, int totalItemCount) {
    324         // send to user's listener
    325         mTotalItemCount = totalItemCount;
    326         if (mScrollListener != null) {
    327             mScrollListener.onScroll(view, firstVisibleItem, visibleItemCount,
    328                     totalItemCount);
    329         }
    330     }
    331 
    332     public void setXListViewListener(IXListViewListener l) {
    333         mListViewListener = l;
    334     }
    335 
    336     /**
    337      * you can listen ListView.OnScrollListener or this one. it will invoke
    338      * onXScrolling when header/footer scroll back.
    339      */
    340     public interface OnXScrollListener extends OnScrollListener {
    341         public void onXScrolling(View view);
    342     }
    343 
    344     /**
    345      * implements this interface to get refresh/load more event.
    346      */
    347     public interface IXListViewListener {
    348         public void onRefresh();
    349 
    350         public void onLoadMore();
    351     }
    352 }
    xlistview

    在xlistview中定义了一个事件接口,来监听加载和刷新。

    4.mainactivity

     1 protected void onCreate(Bundle savedInstanceState) {
     2         super.onCreate(savedInstanceState);
     3         setContentView(R.layout.activity_main);
     4         handler=new Handler();
     5         sqlhelper=new SQLiteHelper(MainActivity.this, "test", null, 1);
     6         list=new ArrayList<PersonInfo>();
     7         getData();
     8         listview=(XListView)findViewById(R.id.listView1);
     9         registerForContextMenu(listview);
    10         listview.setPullLoadEnable(true);
    11         // View moreview=getLayoutInflater().inflate(R.layout.moredata, null);
    12         //listview.addFooterView(moreview);
    13          adapter=new MyAdapter(this);
    14         listview.setAdapter(adapter);
    15         listview.setXListViewListener(this);
    16         listview.setOnItemLongClickListener(new OnItemLongClickListener() {
    17             @Override
    18             public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
    19                     int arg2, long arg3) {
    20                 // TODO Auto-generated method stub
    21                 PersonInfo person=list.get(arg2);
    22                  delname=person.getName();
    23                 return false;
    24             }
    25         });
    26        
    27         /* btnpro=(Button)moreview.findViewById(R.id.bt_load);
    28          pg=(ProgressBar)moreview.findViewById(R.id.pg);
    29         btnpro.setOnClickListener(new View.OnClickListener() {
    30             
    31             @Override
    32             public void onClick(View v) {
    33                 // TODO Auto-generated method stub
    34                 pg.setVisibility(View.VISIBLE);// 将进度条可见
    35                    btnpro.setVisibility(View.GONE);// 按钮不可见
    36                     Handler hanldre=new Handler();
    37                     hanldre.postDelayed(new Runnable(){
    38 
    39                         @Override
    40                         public void run() {
    41                             // TODO Auto-generated method stub
    42                             
    43                             adapter.count+=5;
    44                             
    45                              btnpro.setVisibility(View.VISIBLE);
    46                                pg.setVisibility(View.GONE);
    47                                if(adapter.count>list.size())
    48                                    
    49                                {
    50                                    adapter. count=list.size();
    51                                    btnpro.setVisibility(View.INVISIBLE);
    52                                }
    53                                 adapter.notifyDataSetChanged();
    54                         }}, 2000);
    55             }
    56         });*/
    57     
    58     }
    59     private void getData()
    60     {
    61         Cursor cur=sqlhelper.getReadableDatabase().query("person", null, null, null, null, null, null);
    62         if(cur.moveToFirst())
    63         {
    64             while(cur.moveToNext())
    65             {
    66             PersonInfo person=new PersonInfo();
    67             person.name=cur.getString(cur.getColumnIndex("name"));
    68             person.age=cur.getInt(cur.getColumnIndex("age"));
    69             list.add(person);
    70             }
    71         }
    72     }
    73     private void onLoad() {
    74         listview.stopRefresh();
    75         listview.stopLoadMore();
    76         SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); 
    77     String    strTime = timeFormat.format(new java.util.Date());  
    78         listview.setRefreshTime(strTime);
    79     }
    oncreate
     1     @Override
     2     public void onRefresh() {
     3         // TODO Auto-generated method stub
     4         handler.postDelayed(new Runnable(){
     5 
     6             @Override
     7             public void run() {
     8                 // TODO Auto-generated method stub
     9                 if(list!=null &&list.size()>0)
    10                 {
    11                     list.clear();
    12                     getData();
    13                     adapter.notifyDataSetChanged();
    14                     onLoad();
    15                 }
    16             }}, 2000);
    17     }
    18     @Override
    19     public void onLoadMore() {
    20         // TODO Auto-generated method stub
    21 
    22         handler.postDelayed(new Runnable(){
    23 
    24             @Override
    25             public void run() {
    26                 // TODO Auto-generated method stub
    27                 adapter.count+=5;
    28                 
    29                    if(adapter.count>list.size())
    30                        
    31                    {
    32                        adapter. count=list.size();
    33                    }
    34                     adapter.notifyDataSetChanged();
    35                     onLoad();
    36             }}, 2000);
    37     }
    onloadmore

    刷新和加载都是新建了一个handler。在handler中执行。

    5.demo

  • 相关阅读:
    Evaluation
    Version
    bzoj4184-shallot
    jQuery 获取并设置 CSS 类
    jQuery 删除元素
    jQuery 添加元素
    jQuery 设置内容和属性
    jQuery
    jQuery Chaining
    CI 框架增加公用函数-如何使用Helper辅助函数
  • 原文地址:https://www.cnblogs.com/ggz19/p/3897478.html
Copyright © 2011-2022 走看看