生活随笔
收集整理的這篇文章主要介紹了
Android自定义控件(四)仿网易客户端上拉加载更多
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上一篇仿得網頁客戶端的抽屜模式,這一篇繼續,來寫一寫加載更多這個功能,通過自定義實現加載更多,先上圖:
今天實現的就是如圖中最下面的20條載入中...這個功能啦!
先來說一下思路:
1.在listview中加入20條載入中的這個布局并隱藏
2.加入OnScrollListener監聽,通過監聽滾動事件,當滾動到最低端的時候,顯示上面的布局
3.通過接口回調實現加載更多的功能
4.加載完數據時,通知listview加載結束,隱藏上面的布局文件
下面直接上代碼:
1.在listview中加入20條載入中的這個布局并隱藏
[java]?view plaincopy
LayoutInflater?inflater?=?LayoutInflater.from(context);?? ????????footView?=?inflater.inflate(R.layout.foot_layout,?null);?? ????????addFooterView(footView);?? ????????footView.findViewById(R.id.foot_layout).setVisibility(View.GONE);??
2.加入OnScrollListener監聽,通過監聽滾動事件,當滾動到最低端的時候,顯示上面的布局
[java]?view plaincopy
@Override?? ????public?void?onScrollStateChanged(AbsListView?view,?int?scrollState)?{?? ?? ????????if?(lastItem?==?totalItemCount?&&?scrollState?==?SCROLL_STATE_IDLE)?{?? ????????????if?(!isLoading)?{?? ????????????????isLoading=true;?? ????????????????footView.findViewById(R.id.foot_layout).setVisibility(View.VISIBLE);?? ????????????????isLoadingListener.onLoad();?? ????????????}?? ????????}?? ????}?? ?? ????@Override?? ????public?void?onScroll(AbsListView?view,?int?firstVisibleItem,?? ????????????int?visibleItemCount,?int?totalItemCount)?{?? ????????lastItem?=?firstVisibleItem?+?visibleItemCount;?? ????????this.totalItemCount?=?totalItemCount;?? ????}??
3.通過接口回調實現加載更多的功能
[java]?view plaincopy
public?void?setOnLoadingListener(IsLoadingListener?isLoadingListener){?? ????????this.isLoadingListener=isLoadingListener;?? ????}?? ?????? ????public?interface?IsLoadingListener{?? ????????public?void?onLoad();?? ????}??
[java]?view plaincopy
@Override?? ????public?void?onLoad()?{?? ?????????? ????????handler.postDelayed(new?Runnable()?{?? ?????????????? ????????????@Override?? ????????????public?void?run()?{?? ????????????????list.add("爸爸");?? ????????????????list.add("媽媽");?? ????????????????list.add("我");?? ?????????????????? ????????????????adapter.notifyDataSetChanged();?? ????????????????listView.complateLoad();?? ????????????}?? ????????},?3000);?? ?????????? ????}??
4.加載完數據時,通知listview加載結束,隱藏上面的布局文件?
[java]?view plaincopy
public?void?complateLoad(){?? ????????isLoading=false;?? ????????footView.findViewById(R.id.foot_layout).setVisibility(View.GONE);?? ????}??
ok,自定義控件就是這些.下面是完整的代碼
[java]?view plaincopy
package?com.sdufe.thea.guo.view;?? ?? import?com.sdufe.thea.guo.R;?? ?? import?android.content.Context;?? import?android.util.AttributeSet;?? import?android.view.LayoutInflater;?? import?android.view.View;?? import?android.widget.AbsListView;?? import?android.widget.ListView;?? import?android.widget.AbsListView.OnScrollListener;?? ?? public?class?ListViewLoadMore?extends?ListView?implements?OnScrollListener?{?? ?? ????View?footView;?? ????int?lastItem;??? ????int?totalItemCount;??? ????boolean?isLoading=false;?? ????IsLoadingListener?isLoadingListener;?? ?? ????public?ListViewLoadMore(Context?context,?AttributeSet?attrs,?int?defStyle)?{?? ????????super(context,?attrs,?defStyle);?? ????????initView(context);?? ????}?? ?? ????public?ListViewLoadMore(Context?context,?AttributeSet?attrs)?{?? ????????super(context,?attrs);?? ????????initView(context);?? ????}?? ?? ????public?ListViewLoadMore(Context?context)?{?? ????????super(context);?? ????????initView(context);?? ????}?? ?? ????? ? ? ? ?? ????void?initView(Context?context)?{?? ????????LayoutInflater?inflater?=?LayoutInflater.from(context);?? ????????footView?=?inflater.inflate(R.layout.foot_layout,?null);?? ????????addFooterView(footView);?? ????????footView.findViewById(R.id.foot_layout).setVisibility(View.GONE);?? ????????setOnScrollListener(this);?? ????}?? ?? ????@Override?? ????public?void?onScrollStateChanged(AbsListView?view,?int?scrollState)?{?? ?? ????????if?(lastItem?==?totalItemCount?&&?scrollState?==?SCROLL_STATE_IDLE)?{?? ????????????if?(!isLoading)?{?? ????????????????isLoading=true;?? ????????????????footView.findViewById(R.id.foot_layout).setVisibility(View.VISIBLE);?? ????????????????isLoadingListener.onLoad();?? ????????????}?? ????????}?? ????}?? ?? ????@Override?? ????public?void?onScroll(AbsListView?view,?int?firstVisibleItem,?? ????????????int?visibleItemCount,?int?totalItemCount)?{?? ????????lastItem?=?firstVisibleItem?+?visibleItemCount;?? ????????this.totalItemCount?=?totalItemCount;?? ????}?? ?????? ????public?void?setOnLoadingListener(IsLoadingListener?isLoadingListener){?? ????????this.isLoadingListener=isLoadingListener;?? ????}?? ?????? ????public?interface?IsLoadingListener{?? ????????public?void?onLoad();?? ????}?? ?????? ????public?void?complateLoad(){?? ????????isLoading=false;?? ????????footView.findViewById(R.id.foot_layout).setVisibility(View.GONE);?? ????}?? ?? }??
主界面布局:
[java]?view plaincopy
<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?? ????xmlns:tools="http://schemas.android.com/tools"?? ????android:layout_width="match_parent"?? ????android:layout_height="match_parent">?? ?? ????<com.sdufe.thea.guo.view.ListViewLoadMore?? ????????android:id="@+id/listview"?? ????????android:layout_width="match_parent"?? ????????android:layout_height="match_parent"?? ????????android:divider="@null"/>?? ?? </LinearLayout>??
主界面代碼:
[java]?view plaincopy
package?com.sdufe.thea.guo;?? ?? import?java.util.ArrayList;?? import?java.util.List;?? ?? import?com.sdufe.thea.guo.view.ListViewLoadMore;?? import?com.sdufe.thea.guo.view.ListViewLoadMore.IsLoadingListener;?? ?? import?android.os.Bundle;?? import?android.os.Handler;?? import?android.app.Activity;?? import?android.view.Menu;?? import?android.widget.ArrayAdapter;?? import?android.widget.ListView;?? ?? public?class?MainActivity?extends?Activity?implements?IsLoadingListener{?? ?? ????private?ListViewLoadMore?listView;?? ????private?List<String>?list;?? ????private?ArrayAdapter<String>?adapter;?? ????private?Handler?handler=new?Handler();?? ?? ????@Override?? ????protected?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.activity_main);?? ?? ????????listView?=?(ListViewLoadMore)?findViewById(R.id.listview);?? ????????initData();?? ????????adapter?=?new?ArrayAdapter<String>(this,?? ????????????????android.R.layout.simple_list_item_1,?list);?? ????????listView.setAdapter(adapter);?? ????????listView.setOnLoadingListener(this);?? ????}?? ?? ????? ? ?? ????private?void?initData()?{?? ????????list?=?new?ArrayList<String>();?? ?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????????list.add("123456789");?? ????}?? ?? ????@Override?? ????public?void?onLoad()?{?? ?????????? ????????handler.postDelayed(new?Runnable()?{?? ?????????????? ????????????@Override?? ????????????public?void?run()?{?? ????????????????list.add("爸爸");?? ????????????????list.add("媽媽");?? ????????????????list.add("我");?? ?????????????????? ????????????????adapter.notifyDataSetChanged();?? ????????????????listView.complateLoad();?? ????????????}?? ????????},?3000);?? ?????????? ????}?? ?? }??
不明白的留言,盡力回答你!
csnd代碼下載地址:http://download.csdn.net/detail/elinavampire/8204105
github下載地址:https://github.com/zimoguo/PullToRefreshLoadMore
總結
以上是生活随笔為你收集整理的Android自定义控件(四)仿网易客户端上拉加载更多的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。