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

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


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

先来说一下思路:

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

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

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

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

下面直接上代码:

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

[java]  view plain copy
  1. LayoutInflater inflater = LayoutInflater.from(context);  
  2.         footView = inflater.inflate(R.layout.foot_layout, null);  
  3.         addFooterView(footView);  
  4.         footView.findViewById(R.id.foot_layout).setVisibility(View.GONE);  

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

[java]  view plain copy
  1. @Override  
  2.     public void onScrollStateChanged(AbsListView view, int scrollState) {  
  3.   
  4.         if (lastItem == totalItemCount && scrollState == SCROLL_STATE_IDLE) {  
  5.             if (!isLoading) {  
  6.                 isLoading=true;  
  7.                 footView.findViewById(R.id.foot_layout).setVisibility(View.VISIBLE);  
  8.                 isLoadingListener.onLoad();  
  9.             }  
  10.         }  
  11.     }  
  12.   
  13.     @Override  
  14.     public void onScroll(AbsListView view, int firstVisibleItem,  
  15.             int visibleItemCount, int totalItemCount) {  
  16.         lastItem = firstVisibleItem + visibleItemCount;  
  17.         this.totalItemCount = totalItemCount;  
  18.     }  

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

[java]  view plain copy
  1. public void setOnLoadingListener(IsLoadingListener isLoadingListener){  
  2.         this.isLoadingListener=isLoadingListener;  
  3.     }  
  4.       
  5.     public interface IsLoadingListener{  
  6.         public void onLoad();  
  7.     }  

[java]  view plain copy
  1. @Override  
  2.     public void onLoad() {  
  3.           
  4.         handler.postDelayed(new Runnable() {  
  5.               
  6.             @Override  
  7.             public void run() {  
  8.                 list.add("爸爸");  
  9.                 list.add("妈妈");  
  10.                 list.add("我");  
  11.                   
  12.                 adapter.notifyDataSetChanged();  
  13.                 listView.complateLoad();  
  14.             }  
  15.         }, 3000);  
  16.           
  17.     }  


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

[java]  view plain copy
  1. public void complateLoad(){  
  2.         isLoading=false;  
  3.         footView.findViewById(R.id.foot_layout).setVisibility(View.GONE);  
  4.     }  
ok,自定义控件就是这些.下面是完整的代码

[java]  view plain copy
  1. package com.sdufe.thea.guo.view;  
  2.   
  3. import com.sdufe.thea.guo.R;  
  4.   
  5. import android.content.Context;  
  6. import android.util.AttributeSet;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.widget.AbsListView;  
  10. import android.widget.ListView;  
  11. import android.widget.AbsListView.OnScrollListener;  
  12.   
  13. public class ListViewLoadMore extends ListView implements OnScrollListener {  
  14.   
  15.     View footView;  
  16.     int lastItem; // 最后一项  
  17.     int totalItemCount; // 此刻一共有多少项  
  18.     boolean isLoading=false;  
  19.     IsLoadingListener isLoadingListener;  
  20.   
  21.     public ListViewLoadMore(Context context, AttributeSet attrs, int defStyle) {  
  22.         super(context, attrs, defStyle);  
  23.         initView(context);  
  24.     }  
  25.   
  26.     public ListViewLoadMore(Context context, AttributeSet attrs) {  
  27.         super(context, attrs);  
  28.         initView(context);  
  29.     }  
  30.   
  31.     public ListViewLoadMore(Context context) {  
  32.         super(context);  
  33.         initView(context);  
  34.     }  
  35.   
  36.     /** 
  37.      * 初始化footView 
  38.      *  
  39.      * @param context 
  40.      */  
  41.     void initView(Context context) {  
  42.         LayoutInflater inflater = LayoutInflater.from(context);  
  43.         footView = inflater.inflate(R.layout.foot_layout, null);  
  44.         addFooterView(footView);  
  45.         footView.findViewById(R.id.foot_layout).setVisibility(View.GONE);  
  46.         setOnScrollListener(this);  
  47.     }  
  48.   
  49.     @Override  
  50.     public void onScrollStateChanged(AbsListView view, int scrollState) {  
  51.   
  52.         if (lastItem == totalItemCount && scrollState == SCROLL_STATE_IDLE) {  
  53.             if (!isLoading) {  
  54.                 isLoading=true;  
  55.                 footView.findViewById(R.id.foot_layout).setVisibility(View.VISIBLE);  
  56.                 isLoadingListener.onLoad();  
  57.             }  
  58.         }  
  59.     }  
  60.   
  61.     @Override  
  62.     public void onScroll(AbsListView view, int firstVisibleItem,  
  63.             int visibleItemCount, int totalItemCount) {  
  64.         lastItem = firstVisibleItem + visibleItemCount;  
  65.         this.totalItemCount = totalItemCount;  
  66.     }  
  67.       
  68.     public void setOnLoadingListener(IsLoadingListener isLoadingListener){  
  69.         this.isLoadingListener=isLoadingListener;  
  70.     }  
  71.       
  72.     public interface IsLoadingListener{  
  73.         public void onLoad();  
  74.     }  
  75.       
  76.     public void complateLoad(){  
  77.         isLoading=false;  
  78.         footView.findViewById(R.id.foot_layout).setVisibility(View.GONE);  
  79.     }  
  80.   
  81. }  

主界面布局:

[java]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <com.sdufe.thea.guo.view.ListViewLoadMore  
  7.         android:id="@+id/listview"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:divider="@null"/>  
  11.   
  12. </LinearLayout>  

主界面代码:

[java]  view plain copy
  1. package com.sdufe.thea.guo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.sdufe.thea.guo.view.ListViewLoadMore;  
  7. import com.sdufe.thea.guo.view.ListViewLoadMore.IsLoadingListener;  
  8.   
  9. import android.os.Bundle;  
  10. import android.os.Handler;  
  11. import android.app.Activity;  
  12. import android.view.Menu;  
  13. import android.widget.ArrayAdapter;  
  14. import android.widget.ListView;  
  15.   
  16. public class MainActivity extends Activity implements IsLoadingListener{  
  17.   
  18.     private ListViewLoadMore listView;  
  19.     private List<String> list;  
  20.     private ArrayAdapter<String> adapter;  
  21.     private Handler handler=new Handler();  
  22.   
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_main);  
  27.   
  28.         listView = (ListViewLoadMore) findViewById(R.id.listview);  
  29.         initData();  
  30.         adapter = new ArrayAdapter<String>(this,  
  31.                 android.R.layout.simple_list_item_1, list);  
  32.         listView.setAdapter(adapter);  
  33.         listView.setOnLoadingListener(this);  
  34.     }  
  35.   
  36.     /** 
  37.      * 初始化list值 
  38.      */  
  39.     private void initData() {  
  40.         list = new ArrayList<String>();  
  41.   
  42.         list.add("123456789");  
  43.         list.add("123456789");  
  44.         list.add("123456789");  
  45.         list.add("123456789");  
  46.         list.add("123456789");  
  47.         list.add("123456789");  
  48.         list.add("123456789");  
  49.         list.add("123456789");  
  50.         list.add("123456789");  
  51.         list.add("123456789");  
  52.         list.add("123456789");  
  53.         list.add("123456789");  
  54.         list.add("123456789");  
  55.         list.add("123456789");  
  56.         list.add("123456789");  
  57.         list.add("123456789");  
  58.         list.add("123456789");  
  59.     }  
  60.   
  61.     @Override  
  62.     public void onLoad() {  
  63.           
  64.         handler.postDelayed(new Runnable() {  
  65.               
  66.             @Override  
  67.             public void run() {  
  68.                 list.add("爸爸");  
  69.                 list.add("妈妈");  
  70.                 list.add("我");  
  71.                   
  72.                 adapter.notifyDataSetChanged();  
  73.                 listView.complateLoad();  
  74.             }  
  75.         }, 3000);  
  76.           
  77.     }  
  78.   
  79. }  

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


csnd代码下载地址:http://download.youkuaiyun.com/detail/elinavampire/8204105

github下载地址:https://github.com/zimoguo/PullToRefreshLoadMore

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值