1. 程式人生 > >Android ListView下拉重新整理上拉載入

Android ListView下拉重新整理上拉載入

把這兩天的心得記錄下來,以後用到不會忘。

先說下拉重新整理,下拉重新整理我們主要用到了android自帶v4jar包中的一個控制元件SwipeRefreshLayout,如果下面是xml檔案, 如果你在寫報錯的話,換一個最新的v4包就可以了。

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ListView
            android:id="@+id/list_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ListView>
    </android.support.v4.widget.SwipeRefreshLayout>
佈局檔案寫好之後就是程式碼這一塊了
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
		swipeRefreshLayout.setColorScheme(android.R.color.background_dark, android.R.color.background_light, android.R.color.black, android.R.color.darker_gray);
		swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				list.clear();
				list.addAll(Arrays.asList("小明","小花","小紅","小綠","小白","小剛","小小"));
				adapter.notifyDataSetChanged();
				swipeRefreshLayout.setRefreshing(false);
			}
		});
下拉主要就是在這個onRefresh中實現你的資料請求,其中swipeRefreshLayout.setRefreshing(false);這句話是表示停止list上邊的重新整理條,也就是停止重新整理。下拉重新整理就是這些,接下來我們來看上拉載入更多。

上拉載入更多我們要實現一個介面OnScrollListener,這個介面主要有兩個方法onScroll和onScrollStateChanged,onScroll主要是用來檢視當前螢幕的item個數、位置等,onScrollStateChanged主要用來監聽當前ListView處於靜止、滑動、慣性的狀態。一會程式碼有詳細的註釋。

首先要一個上拉載入時的一個佈局檔案

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView 
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="正在載入..."/>
程式碼這塊主要注意的就是addFooterView和removeFooterView的時機,下邊是上拉載入和下拉重新整理的全部程式碼
public class MainActivity extends Activity implements OnScrollListener{
	private SwipeRefreshLayout swipeRefreshLayout;
	private ListView listview;
	private View footer;
	private List<String> list;
	private ArrayAdapter adapter;
	private int visibleItem;
	//當前是否處於上拉載入中
	private boolean isUpdateData = false;
	
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 0x100:
				listview.removeFooterView(footer);
				list.addAll(Arrays.asList("大明","大花","大紅","大綠","大白","大剛"));
				adapter.notifyDataSetChanged();
				isUpdateData = false;
				break;
			}
		};
	};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        infoView();
    }

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
        /** 
        * firstVisibleItem 表示在當前螢幕顯示的第一個listItem在整個listView裡面的位置(下標從0開始) 
        * visibleItemCount表示在現時螢幕可以見到的ListItem(部分顯示的ListItem也算)總數 
        * totalItemCount表示ListView的ListItem總數  
        * listView.getLastVisiblePosition()表示在現時螢幕最後一個ListItem 
        * (最後ListItem要完全顯示出來才算)在整個ListView的位置(下標從0開始)  
        */  
		visibleItem = firstVisibleItem + visibleItemCount;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if (adapter.getCount() == visibleItem && scrollState == SCROLL_STATE_IDLE) {
		    /**
			    *scrollState有三種狀態,分別是SCROLL_STATE_IDLE、SCROLL_STATE_TOUCH_SCROLL、SCROLL_STATE_FLING
			    *SCROLL_STATE_IDLE是當螢幕停止滾動時
			    *SCROLL_STATE_TOUCH_SCROLL是當用戶在以觸屏方式滾動螢幕並且手指仍然還在螢幕上時(The user is scrolling using touch, and their finger is still on the screen)
			    *SCROLL_STATE_FLING是當用戶由於之前划動螢幕並擡起手指,螢幕產生慣性滑動時(The user had previously been scrolling using touch and had performed a fling)
			    */  
			if (!isUpdateData) {
				listview.addFooterView(footer);
				handler.sendEmptyMessageDelayed(0x100, 5000);
				isUpdateData = true;
			}

		}
	}
	
	private void infoView() {
		swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
		listview = (ListView) findViewById(R.id.list_view);
		footer = getLayoutInflater().inflate(R.layout.menu_layout, null);
		listview.setOnScrollListener(this);
		list = new ArrayList<String>();
		list.addAll(Arrays.asList("小明","小花","小紅","小綠","小白","小剛","小小"));
		adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, list);
		listview.setAdapter(adapter);
		swipeRefreshLayout.setColorScheme(android.R.color.background_dark, android.R.color.background_light, android.R.color.black, android.R.color.darker_gray);
		swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				list.clear();
				list.addAll(Arrays.asList("小明","小花","小紅","小綠","小白","小剛","小小"));
				adapter.notifyDataSetChanged();
				swipeRefreshLayout.setRefreshing(false);
			}
		});
	}
    
}
希望對你起到作用吧