ListView paging function (2) Custom View to achieve paging function

(1) Custom View inherit ListView to achieve OnScrollListener;

(2) Set up a callback interface for the customer to load data, the customer implements this interface

(3) Provide a method to notify the completion of data loading

(3) Judge whether to slide to the bottom through onScroll() and onScrollStateChanged()

Load new data.

\

\

\

Custom ListView:

package com.example.listviewpagedemo; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.LinearLayout; import android.widget.ListView; public class MyListView extends ListView implements OnScrollListener{ private LinearLayout footLayout; private boolean isLoading; Private int bottomIndex = 0; public MyListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } public MyListView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyListView(Context context) { this(context, null); } /** * initView * @param context */ private void initView(context context) {LayoutInflater inflater = LayoutInflater.from(context); footLayout = (LinearLayout) inflater.inflate(R.layout.footer, null); this.addFooterView(footLayout); // Set the listener to this.setonScrollListener (this); footLayout.setVisibility(View.GONE); } @override public void onScrollStateChanged(AbsListView view, int scrollState) {try {// pull to the bottom and stop scrolling Load new data if(isBottom && scrollState == SCROLL_STATE_IDLE){if(! isLoading){ Log.i("tag", "onScrollStateChanged>>>isbottom"); footLayout.setVisibility(View.VISIBLE); // Load new data mlistener.onload (); } } } catch (Exception e) { e.printStackTrace(); } } boolean isBottom = false; @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, If (firstVisibleItem + visibleItemCount >= totalItemCount) {log. I ("tag", "onScroll=-====isBottom = true;" ); bottomIndex = firstVisibleItem + visibleItemCount ; isBottom = true; }else { isBottom = false; } /** * public void doComplete() {footlayout.setvisibility (view.gone); isLoading = false; this.setSelection(bottomIndex); } private onLoadListener mListener; public void setOnLoadListener(onLoadListener mListener) { this.mListener = mListener; } public interface onLoadListener { public void onLoad(); }}Copy the code

\

\

MainActivity.java

Load and update data data using Thread+Handler

(1) Implement the OnLoadListener interface defined in the custom Listview for callback loading data.

(2) Create a child thread to load data, and send an empty message to the handler to notify the data

Loading is completed, use Adapter in the handler. NotifyDataChanged () notifies the data update (footerVie disappear)

Note: MyListView calls doComplete() to notify ListView of where the data has finished loading. The first time I called lv.docomplete () in a child thread, I kept blinking.

When the child thread is started, the listView adapter has not been set.

So lv.docomplete () notifying listView that the data load is complete is put in Handler.

package com.example.listviewpagedemo;

import java.util.ArrayList;
import java.util.List;

import com.example.listviewpagedemo.MyListView.onLoadListener;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements onLoadListener {
	/* * Bottom refresh load layout */
	LinearLayout footLayout ;

	MyListView lv;

	ArrayAdapter<String> adapter;

	List<String>  datas = new ArrayList<String>();;
	boolean isLoadOver = false;
	/** * Notifies root */ when data is loaded
	Handler handler = new  Handler() {
		public void handleMessage(Message msg) {
			if(msg.what == 1) {if(adapter == null){
					adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,datas);
					lv.setAdapter(adapter);
				}else {
					adapter.notifyDataSetChanged();
				}
			}
			lv.doComplete();
		}; 
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		initView();
		initDatas();
	}

	/** * Initializes data */
	private void initDatas(a) {
		new Thread(new Runnable(){
			@Override
			public void run(a) {
				try {
					Thread.sleep(2000);

					// Load data
					for (int i = 0; i < 20; i++) {
						datas.add("Project Presentation" + datas.size());
					}
					lv.doComplete();
					handler.sendEmptyMessage(1);

				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			}
		}).start();

	}
	/** * Initializes view */
	private void initView(a) {
		lv = (MyListView)findViewById(R.id.listView1);
		lv.setOnLoadListener(this);
	}

	@Override
	public void onLoad(a) {
		try {
			// Load data
			initDatas();
			For (int I = 0; i < 20; I++) {datas.add(" project display "+ datas.size()); } lv.doComplete(); handler.sendEmptyMessage(1); * /
		} catch(Exception e) { e.printStackTrace(); }}}Copy the code

\