上拉載入更多之ListView實現
阿新 • • 發佈:2019-01-06
在Android的應用程式中,使用列表來展示內容的應用是最多的。當然,我們從早期的ListView到目前的RecyclerView,列表控制元件的實現更加的優秀。但無論怎樣,我們都會在使用列表控制元件的時候新增下拉重新整理和上拉載入更多。這的效果有多種實現當然也會針對ListView與RecyclerView有著對應的實現,本篇我首先以ListView來實現一下對於下拉重新整理和上拉載入更多的效果。
本篇的效果是通過對ListView進行新增腳佈局的方式來實現的。主要就是對於ListView的滑動效果的監聽,在不同的時機顯示或隱藏腳佈局。
本篇主要是上拉載入更多的實現。
1.在layout目錄下新增腳佈局檔案listview_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/footer_layout"
android:layout_width ="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center"
>
<!-- 設定進度條的樣式為小的圓形進度條 -->
<ProgressBar
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleSmall"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在載入..."
/>
</LinearLayout>
</LinearLayout>
2.在view的包中新建類LoadListView繼承自ListView。在類中定義添加布局的方法並在每一個構造方法中呼叫,從而保證在初始化的時候就為ListView添加布局。
public LoadListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
addView(context);
}
public LoadListView(Context context, AttributeSet attrs) {
super(context, attrs);
addView(context);
}
public LoadListView(Context context) {
super(context);
addView(context);
}
3.將xml檔案中的佈局載入並初始化,然後為ListView新增腳佈局。
footerView=LayoutInflater.from(context).inflate(R.layout.listview_footer, null);
this.addFooterView(footerView);
由於我們在剛剛開啟應用介面的時候沒有滑動,所以預設情況下的ListView的腳佈局是隱藏的,我們現在讓自定義的ListView實現滾動監聽的介面,讓滾動事件交給控制元件自己處理。
this.footerView.findViewById(R.id.footer_layout).setVisibility(View.GONE)
// 初始化的同時就設定上滾動的監聽
this.setOnScrollListener(this);
4.當然為了真正監聽滾動事件從而在不同的時機顯示或隱藏頭佈局與腳佈局,要在介面的未實現方法上做文章。
/*
* (non-Javadoc)
* @see android.widget.AbsListView.OnScrollListener#onScroll(android.widget.
* AbsListView, int, int, int) 1:這個ListView控制元件 2:第一個可見的item位置 3:可見的item的數量
* 4:總的item數量
*/
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//記錄下最後一個可見的條目的位置,通過判斷最後一個可見條目的位置是否等於總的條目的數量來判斷是否載入到最後一個條目A
this.lastVisibility=firstVisibleItem+visibleItemCount;
this.itemCount=totalItemCount;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//判斷當前的可見的最後一個item就是最後一個item了,並且此時的狀態是靜止的
if(itemCount==lastVisibility&&scrollState==SCROLL_STATE_IDLE){
if(!isLoading){
isLoading=true;
footerView.findViewById(R.id.footer_layout).setVisibility(View.VISIBLE);
//通過介面回撥的方式來實現載入更多資料的展示效果
listener.onLoad();
}
}
}
5.通過介面回撥的方式來進行當出現載入條目的時候的動作。
@Override
public void onLoad() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
for (int i=1;i <= 10; i++) {
lists.add(new String("這是第" + i + "個條目"));
}
mAdapter.notifyDataSetChanged();
listView.loadComplete();
}
}, 3000);
}