300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Android自定义控件(四)仿网易客户端上拉加载更多

Android自定义控件(四)仿网易客户端上拉加载更多

时间:2018-11-15 03:13:13

相关推荐

Android自定义控件(四)仿网易客户端上拉加载更多

上一篇仿得网页客户端的抽屉模式,这一篇继续,来写一写加载更多这个功能,通过自定义实现加载更多,先上图:

今天实现的就是如图中最下面的20条载入中...这个功能啦!

先来说一下思路:

1.在listview中加入20条载入中的这个布局并隐藏

2.加入OnScrollListener监听,通过监听滚动事件,当滚动到最低端的时候,显示上面的布局

3.通过接口回调实现加载更多的功能

4.加载完数据时,通知listview加载结束,隐藏上面的布局文件

下面直接上代码:

1.在listview中加入20条载入中的这个布局并隐藏

[java]view plaincopyLayoutInflaterinflater=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 publicvoidonScrollStateChanged(AbsListViewview,intscrollState){ if(lastItem==totalItemCount&&scrollState==SCROLL_STATE_IDLE){ if(!isLoading){ isLoading=true; footView.findViewById(R.id.foot_layout).setVisibility(View.VISIBLE); isLoadingListener.onLoad(); } } } @Override publicvoidonScroll(AbsListViewview,intfirstVisibleItem, intvisibleItemCount,inttotalItemCount){ lastItem=firstVisibleItem+visibleItemCount; this.totalItemCount=totalItemCount; }

3.通过接口回调实现加载更多的功能

[java]view plaincopypublicvoidsetOnLoadingListener(IsLoadingListenerisLoadingListener){ this.isLoadingListener=isLoadingListener; } publicinterfaceIsLoadingListener{ publicvoidonLoad(); }[java]view plaincopy@Override publicvoidonLoad(){ handler.postDelayed(newRunnable(){ @Override publicvoidrun(){ list.add("爸爸"); list.add("妈妈"); list.add("我"); adapter.notifyDataSetChanged(); plateLoad(); } },3000); }

4.加载完数据时,通知listview加载结束,隐藏上面的布局文件[java]view plaincopypublicvoidcomplateLoad(){ isLoading=false; footView.findViewById(R.id.foot_layout).setVisibility(View.GONE); } ok,自定义控件就是这些.下面是完整的代码[java]view plaincopypackagecom.sdufe.thea.guo.view; importcom.sdufe.thea.guo.R; importandroid.content.Context; importandroid.util.AttributeSet; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.widget.AbsListView; importandroid.widget.ListView; importandroid.widget.AbsListView.OnScrollListener; publicclassListViewLoadMoreextendsListViewimplementsOnScrollListener{ ViewfootView; intlastItem;//最后一项 inttotalItemCount;//此刻一共有多少项 booleanisLoading=false; IsLoadingListenerisLoadingListener; publicListViewLoadMore(Contextcontext,AttributeSetattrs,intdefStyle){ super(context,attrs,defStyle); initView(context); } publicListViewLoadMore(Contextcontext,AttributeSetattrs){ super(context,attrs); initView(context); } publicListViewLoadMore(Contextcontext){ super(context); initView(context); } /** *初始化footView * *@paramcontext */ voidinitView(Contextcontext){ LayoutInflaterinflater=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 publicvoidonScrollStateChanged(AbsListViewview,intscrollState){ if(lastItem==totalItemCount&&scrollState==SCROLL_STATE_IDLE){ if(!isLoading){ isLoading=true; footView.findViewById(R.id.foot_layout).setVisibility(View.VISIBLE); isLoadingListener.onLoad(); } } } @Override publicvoidonScroll(AbsListViewview,intfirstVisibleItem, intvisibleItemCount,inttotalItemCount){ lastItem=firstVisibleItem+visibleItemCount; this.totalItemCount=totalItemCount; } publicvoidsetOnLoadingListener(IsLoadingListenerisLoadingListener){ this.isLoadingListener=isLoadingListener; } publicinterfaceIsLoadingListener{ publicvoidonLoad(); } publicvoidcomplateLoad(){ isLoading=false; footView.findViewById(R.id.foot_layout).setVisibility(View.GONE); } }

主界面布局:[java]view plaincopy<LinearLayoutxmlns:android="/apk/res/android" xmlns:tools="/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 plaincopypackagecom.sdufe.thea.guo; importjava.util.ArrayList; importjava.util.List; importcom.sdufe.thea.guo.view.ListViewLoadMore; importcom.sdufe.thea.guo.view.ListViewLoadMore.IsLoadingListener; importandroid.os.Bundle; importandroid.os.Handler; importandroid.app.Activity; importandroid.view.Menu; importandroid.widget.ArrayAdapter; importandroid.widget.ListView; publicclassMainActivityextendsActivityimplementsIsLoadingListener{ privateListViewLoadMorelistView; privateList<String>list; privateArrayAdapter<String>adapter; privateHandlerhandler=newHandler(); @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListViewLoadMore)findViewById(R.id.listview); initData(); adapter=newArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list); listView.setAdapter(adapter); listView.setOnLoadingListener(this); } /** *初始化list值 */ privatevoidinitData(){ list=newArrayList<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 publicvoidonLoad(){ handler.postDelayed(newRunnable(){ @Override publicvoidrun(){ list.add("爸爸"); list.add("妈妈"); list.add("我"); adapter.notifyDataSetChanged(); plateLoad(); } },3000); } }

不明白的留言,尽力回答你!

csnd代码下载地址:/detail/elinavampire/8204105

github下载地址:/zimoguo/PullToRefreshLoadMore

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。