慕课网ListView分页笔记(简化的Adapter)

1.    设置分页加载更多的提示页面footer_layout.xml:

代码入下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/footerll"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:layout_gravity="center" >
    
    <ProgressBar 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleSmall"/>

    <TextView 
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="正在加载"/>
</LinearLayout>

然后自定义一个类继承ListView:

LoadListView.java

package com.example.listview;

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 LoadListView extends ListView implements OnScrollListener {

	private View footer = null;//底部布局

	private int lastVisibleItem;//最后一条记录
	private int totalVisibleItem;//总的记录
	private boolean isLoading ; //是否正在加载
	private ILoadListener iLoadListener = null;//用于回调的接口

	public LoadListView(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public LoadListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		this.initView(context);
	}

	public LoadListView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 添加底部加载提示布局到listView
	 */
	private void initView(Context context) {
		LayoutInflater inflater = LayoutInflater.from(context);
		footer = inflater.inflate(R.layout.footer_layout, null);
		footer.findViewById(R.id.footerll).setVisibility(View.GONE);
		this.addFooterView(footer);
		this.setOnScrollListener(this);
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		if (this.lastVisibleItem == this.totalVisibleItem
				&& scrollState == SCROLL_STATE_IDLE) {//当滚动到最后一条时,同时滚动的动作停止时
			if(!isLoading)
			{
				this.isLoading = true;
				this.footer.findViewById(R.id.footerll).setVisibility(View.VISIBLE);
				//加载更多的数据
				this.iLoadListener.onLoad();
			}
		}
	}
	
	/**
	 * 此方法是当数据加载完毕时,将底部的footer隐藏
	 */
	public void complete()
	{
		this.isLoading = false;
		this.footer.findViewById(R.id.footerll).setVisibility(View.GONE);
	}

	/**
	 * 此方法是将MainActivity中的interface传递进来,就是回调onLoad方法,使数据加载
	 */
	public void setInterface(ILoadListener iLoadListener)
	{
		this.iLoadListener = iLoadListener ;
	}
	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		this.lastVisibleItem = firstVisibleItem + visibleItemCount;
		this.totalVisibleItem = totalItemCount;
	}
	
	/**
	 * 加载更多数据的回调接口
	 */
	public interface ILoadListener
	{
		public void onLoad();
	}
}

activity_main.xml代码:

<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"
    android:orientation="vertical" >

    <com.example.listview.LoadListView在此处用自定的ListView
        android:id="@+id/loadlistview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

MainActivity.java中

package com.example.listview;

import java.util.ArrayList;
import com.example.listview.LoadListView.ILoadListener;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;

public class MainActivity extends ActionBarActivity implements ILoadListener{

	private LoadListView loadview = null;
	private ArrayAdapter<String> adapter = null;
	private ArrayList<String> array = new ArrayList<String>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.loadview = (LoadListView) super.findViewById(R.id.loadlistview);
		this.loadview.setInterface(this);//用于传递接口,以实现回调LoadListView中的接口的方法,来加载数据
		this.initView();
	}


	public void initView()
	{
		if(this.adapter==null)//这是首次加载ListView时,没有往下滚动
		{			

			this.array.add("1");
			this.array.add("2");
			this.array.add("3");
			this.array.add("1");
			this.array.add("2");
			this.array.add("3");
			this.array.add("1");
			this.array.add("2");
			this.array.add("3");
			this.adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array);
			loadview.setAdapter(adapter);
		}
		else
		{

			this.array.add("11");这里是新增加的数据
			this.array.add("12");
			this.array.add("13");
			this.adapter.notifyDataSetChanged();// 通知ListView数据改变了同时实现更新
		}
	}

	@Override
	public void onLoad() {
		// TODO Auto-generated method stub
		Handler handler = new Handler();//此处用线程延迟是因为加载太快看不到效果
		handler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				//更新ListView,我这里同时将数据设置在里面了,所以没有获取更多的数据
				initView();
				//通知ListView加载完毕
				loadview.complete();
			}
		}, 4000);
	}
}

2.   思路:

1.    首先定义好了各个界面底部界面footer_layout.xml , activity_main.xml

2.    然后 写一个继承ListView的类(LoadListView)

3.    接着在这个类中设置onScrollListener(AbsListView包下的)

在这个监听中,我们要判断是否是最后一条记录了

if (this.lastVisibleItem == this.totalVisibleItem
				&& scrollState == SCROLL_STATE_IDLE) {//当滚动到最后一条时,同时滚动的动作停止时

如果是最后一条记录,同时滚动停止时(在没有做加载数据的动作时,滚动到最后面就没有数据了,我们也不能是实现滚动了)

4    做滚动到最后加载数据的动作

1.    在自定义LoadListView.java类中写一个接口

/**
	 * 加载更多数据的回调接口
	 */
	public interface ILoadListener
	{
		public void onLoad();
	}

2.    在MainActivity.java类中实现这个接口

public class MainActivity extends ActionBarActivity implements ILoadListener
	@Override
	public void onLoad() {
		// TODO Auto-generated method stub
		这里调用要添加的数据,同时将调用更新ListView的方法
	}

这个接口是为了传递数据的

this.loadview.setInterface(this);//用于传递接口,以实现回调LoadListView中的接口的方法,来加载数据

在MainActivity这个类中有这么一句。

而后在LoadListView类中:

/**
	 * 此方法是将MainActivity中的interface传递进来,就是回调onLoad方法,使数据加载
	 */
	public void setInterface(ILoadListener iLoadListener)
	{
		this.iLoadListener = iLoadListener ;
	}

设置传递的接口

同时在这个方法中调用加载数据

@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		if (this.lastVisibleItem == this.totalVisibleItem
				&& scrollState == SCROLL_STATE_IDLE) {//当滚动到最后一条时,同时滚动的动作停止时
			if(!isLoading)
			{
				this.isLoading = true;
				this.footer.findViewById(R.id.footerll).setVisibility(View.VISIBLE);
				//加载更多的数据
				this.iLoadListener.onLoad();这里调用接口中的方法加载更多的数据
			}
		}
	}


注:以上笔记来自看慕课网的视频,这应该不算侵权吧???


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值