效果

最简单的上拉刷新和下拉刷新,当listview滚动到底部时向上拉刷新数据。当listview滚动到最顶部时下拉刷新。

ListView(2)最简单的上拉刷新、下拉刷新代码-LMLPHP     ListView(2)最简单的上拉刷新、下拉刷新代码-LMLPHP

    图1,上拉刷新                  图2,下拉刷新

1.设置lisview

加载header refresh,footer refresh,同时可选择关闭滚动到底部,顶部的动画。

         listview = (ListView) tbhost.findViewById(R.id.tab_weixin_list);
adapter = new TabWeixinListAdapter(context,listview); headerRefresh = tabHost.findViewById(R.id.ViewStub_header_refresh);
footerRefresh = tabHost.findViewById(R.id.ViewStub_footer_refresh); listview.setAdapter(adapter); if(android.os.Build.VERSION.SDK_INT >=){//关闭滚动到顶部,底部动画。
listview.setOverScrollMode(View.OVER_SCROLL_NEVER);
}
listview.setOnTouchListener(touchListener);

2.在onTouchEvent中处理事件

   float startY;
View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startY = event.getY();;
break;
case MotionEvent.ACTION_MOVE:
float y = event.getY();
if (y - startY < ){//上拉
if (!listview.canScrollVertically()){
footerRefresh.setVisibility(View.VISIBLE);
}
}else{//下拉
View cld = listview.getChildAt();
int bottom = cld.getBottom();
int het = cld.getHeight();
if (listview.getFirstVisiblePosition() == && bottom >= het ){
headerRefresh.setVisibility(View.VISIBLE);
}
}
break;
case MotionEvent.ACTION_UP:
headerRefresh.setVisibility(View.GONE);
footerRefresh.setVisibility(View.GONE);
if (listview.getFirstVisiblePosition() == || !listview.canScrollVertically()){
addSomeTableWeixinItem();
}
break;
default:
System.out.println("default ");
break;
}
return false;
}
};

listview.canScrollVertically() 这个函数可以用来判断当前view还可以向上拉多少,如果向上拉0都不可以,就说明在view底部。此函数对下拉不好用。

当 listview.getFirstVisiblePosition() == 0 时 listview滚动到了顶部。

3.footer_refresh_layout.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer_fresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D1D1D1"
android:gravity="center_horizontal"
android:orientation="horizontal" > <ProgressBar
android:id="@+id/footer_refresh_prgs"
style="@style/CustomProgressStyle"
android:layout_width="wrap_content"
android:indeterminateDuration="700"
android:layout_height="match_parent" /> <TextView
android:id="@+id/footer_refresh_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:text="刷新"
android:textSize="20sp" /> </LinearLayout>

4.header_refresh_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer_fresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:background="#D1D1D1"
android:orientation="horizontal" > <ProgressBar
android:id="@+id/header_refresh_prgs"
style="@style/CustomProgressStyle"
android:layout_width="wrap_content"
android:indeterminateDuration="700"
android:layout_height="match_parent" /> <TextView
android:id="@+id/header_refresh_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:text="刷新"
android:textSize="20sp" /> </LinearLayout>

5.listview的layout

 <?xml version="1.0" encoding="utf-8"?>
<!-- <merge xmlns:android="http://schemas.android.com/apk/res/android" > --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_weixin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ViewStub
android:id="@+id/ViewStub_header_refresh"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#BFBFBF"
android:inflatedId="@+id/tab_weixin_refresh"
android:layout="@layout/header_refresh_layout" > </ViewStub> <TextView android:id="@+id/footer_fresh_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#BFBFBF"
android:gravity="center_horizontal|center_vertical"
android:textSize="20sp"
android:text="@string/find_qq_friends" /> <!-- android:overScrollMode=”never” -->
<ListView
android:id="@+id/tab_weixin_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView> <ViewStub
android:id="@+id/ViewStub_footer_refresh"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#BFBFBF"
android:inflatedId="@+id/tab_weixin_footer_refresh"
android:layout="@layout/footer_refresh_layout" /> </LinearLayout> <!-- </merge> -->
05-07 11:50