日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

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

發(fā)布時(shí)間:2025/6/15 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android自定义控件(四)仿网易客户端上拉加载更多 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

上一篇仿得網(wǎng)頁客戶端的抽屜模式,這一篇繼續(xù),來寫一寫加載更多這個(gè)功能,通過自定義實(shí)現(xiàn)加載更多,先上圖:


今天實(shí)現(xiàn)的就是如圖中最下面的20條載入中...這個(gè)功能啦!

先來說一下思路:

1.在listview中加入20條載入中的這個(gè)布局并隱藏

2.加入OnScrollListener監(jiān)聽,通過監(jiān)聽滾動(dòng)事件,當(dāng)滾動(dòng)到最低端的時(shí)候,顯示上面的布局

3.通過接口回調(diào)實(shí)現(xiàn)加載更多的功能

4.加載完數(shù)據(jù)時(shí),通知listview加載結(jié)束,隱藏上面的布局文件

下面直接上代碼:

1.在listview中加入20條載入中的這個(gè)布局并隱藏

[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監(jiān)聽,通過監(jiān)聽滾動(dòng)事件,當(dāng)滾動(dòng)到最低端的時(shí)候,顯示上面的布局

    [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.通過接口回調(diào)實(shí)現(xiàn)加載更多的功能

    [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.加載完數(shù)據(jù)時(shí),通知listview加載結(jié)束,隱藏上面的布局文件?

    [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;?//?最后一項(xiàng)??
  • ????int?totalItemCount;?//?此刻一共有多少項(xiàng)??
  • ????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);??
  • ????}??
  • ??
  • ????/**?
  • ?????*?初始化footView?
  • ?????*??
  • ?????*?@param?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);??
  • ????}??
  • ??
  • ????/**?
  • ?????*?初始化list值?
  • ?????*/??
  • ????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

    總結(jié)

    以上是生活随笔為你收集整理的Android自定义控件(四)仿网易客户端上拉加载更多的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。