項(xiàng)目用途
1.由于現(xiàn)在 Android design 的出現(xiàn)寸爆,很多情況下design里面的下拉刷新其實(shí)更實(shí)用礁鲁。
2.那么問題來了,用了design的下拉加載更多赁豆,跟傳統(tǒng)的上下拉刷新的ListView框架就會(huì)有所沖突了仅醇,其實(shí)給ListView加一個(gè)footer就可以了,沒必要去搞一大堆框架不容易理解歌憨。
Demo截圖
這年頭你沒個(gè)動(dòng)態(tài)截圖貼上來都沒人會(huì)往下看了~
實(shí)現(xiàn)方式
- 首先看下頁面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/fra_one_refresh" >
<com.tommy.recyclerview.view.LoadMoreListView
android:id="@+id/fra_one_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
- 說明
用Android design 自己的下拉刷新包裹住一個(gè)自定義的上拉加載更多的ListView
- 自定義ListView的代碼如下
public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener {
private int totalItemCount;// 總數(shù)量
private int lastVisibleItem;// 最后一個(gè)可見的item;
private boolean isLoading = false;// 判斷變量
private RelativeLayout mRv_footer;
private TextView mTv_footer;
private ProgressBar mPb_footer;
public OnRefreshInterface loadListener;// 接口變量
// 加載更多數(shù)據(jù)的回調(diào)接口
public interface OnRefreshInterface {
public void onLoad();
}
// 設(shè)置監(jiān)聽
public void setOnRefreshInterface(OnRefreshInterface loadListener) {
this.loadListener = loadListener;
}
// 加載完成
public void refreshComplete() {
isLoading = false;
mRv_footer.setVisibility(View.GONE);
}
// 加載沒有更多
public void refreshNoMore() {
isLoading = false;
mPb_footer.setVisibility(View.GONE);
mTv_footer.setText("沒有更多");
}
public LoadMoreListView(Context context) {
super(context);
initView(context);
}
public LoadMoreListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
private void initView(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
View footer = inflater.inflate(R.layout.list_loadmore_footer, null);
mRv_footer = (RelativeLayout) footer.findViewById(R.id.list_load_more_footer);
mTv_footer = (TextView) footer.findViewById(R.id.list_load_textView);
mPb_footer = (ProgressBar) footer.findViewById(R.id.list_load_progress);
mRv_footer.setVisibility(View.GONE);
this.addFooterView(footer);
this.setOnScrollListener(this);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (totalItemCount == lastVisibleItem && scrollState == SCROLL_STATE_IDLE) {
if (!isLoading) {
isLoading = true;
mRv_footer.setVisibility(View.VISIBLE);
loadListener.onLoad();
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.lastVisibleItem = firstVisibleItem + visibleItemCount;
this.totalItemCount = totalItemCount;
}
}
- 說明
這里面只涉及到一個(gè) ListView footer 的布局
- 代碼如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_load_more_footer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:orientation="horizontal">
<ProgressBar
android:id="@+id/list_load_progress"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_gravity="center" />
<TextView
android:id="@+id/list_load_textView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="center"
android:textSize="14dp"
android:textColor="#666666"
android:text="加載更多..." />
</LinearLayout>
</RelativeLayout>
- Android design 的刷新方式調(diào)用代碼如下
//找view
SwipeRefreshLayout fraOneRefresh = (SwipeRefreshLayout)findViewById(R.id.fra_one_refresh);
//設(shè)置loading那個(gè)圖標(biāo)的變換顏色
fraOneRefresh.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimaryDark,R.color.orange);
fraOneRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
fraOneRefresh.postDelayed(new Runnable() {
@Override
public void run() {
fraOneRefresh.setRefreshing(false); //消失掉刷新
}
},3000);
}
});
- ListView的上拉加載更多的監(jiān)聽
LoadMoreListView fraOneList = (LoadMoreListView)findViewById(R.id.fra_one_list);
fraOneList.setAdapter(adapter); //Adapter自己定義咯着憨,這個(gè)太簡單就不多說了
fraOneList.setOnRefreshInterface(new LoadMoreListView.OnRefreshInterface() {
@Override
public void onLoad() {
fraOneList.postDelayed(new Runnable() {
@Override
public void run() {
fraOneList.refreshComplete();
}
},2000);
}
});
總結(jié)
整個(gè)上拉加載的ListView到此結(jié)束了,感謝您的耐心看完~
這種布局刷新方式其實(shí)就跟簡書APP的發(fā)現(xiàn)那里很像了务嫡,我也覺得這種設(shè)計(jì)模式非常簡潔甲抖、輕量~
只恨大部分產(chǎn)品發(fā)現(xiàn)不了 Android design 的好(總是跑過來跟你說,做跟IOS的一樣)心铃,安卓有他很好的design非要全部改成IOS風(fēng)格准谚,可悲~
好了,就啰嗦這么多了去扣,源碼就不放上來了悄但,上面除了Adapter其他代碼都有了~