首页 > 代码库 > Android自定义控件(四)仿网易客户端上拉加载更多

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

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


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

先来说一下思路:

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

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

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

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

下面直接上代码:

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

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监听,通过监听滚动事件,当滚动到最低端的时候,显示上面的布局

@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.通过接口回调实现加载更多的功能

public void setOnLoadingListener(IsLoadingListener isLoadingListener){
		this.isLoadingListener=isLoadingListener;
	}
	
	public interface IsLoadingListener{
		public void onl oad();
	}

@Override
	public void onl oad() {
		
		handler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				list.add("爸爸");
				list.add("妈妈");
				list.add("我");
				
				adapter.notifyDataSetChanged();
				listView.complateLoad();
			}
		}, 3000);
		
	}


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

public void complateLoad(){
		isLoading=false;
		footView.findViewById(R.id.foot_layout).setVisibility(View.GONE);
	}
ok,自定义控件就是这些.下面是完整的代码

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);
	}

	/**
	 * 初始化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 onl oad();
	}
	
	public void complateLoad(){
		isLoading=false;
		footView.findViewById(R.id.foot_layout).setVisibility(View.GONE);
	}

}

主界面布局:

<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>

主界面代码:

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 onl oad() {
		
		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自定义控件(四)仿网易客户端上拉加载更多