ListView动态显示或隐藏FooterView

时间:2022-05-29 19:31:12

MainActivity如下:

package cn.com.bravesoft.testlistviewloadmore;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
/**
 * Demo描述:
 * 精确监听ListView滑动到底部
 * 且自动显示或异常隐藏FooterView
 * 
 * 注意事项:
 * 1 addFooterView()应该在要setAdapter()前执行.
 *   否则报错:类型转换异常
 * 2 ListView设置属性
 *   android:fastScrollEnabled="false"
 *   android:smoothScrollbar="true"
 *   防止滑动的时候卡屏
 * 3 关于FooterView的添加和删除
 *   3.1每次总是先remove掉FooterView
 *   3.2若有需求再add上FooterView
 */
public class MainActivity extends Activity {
	private ListView mListView;
	private SimpleAdapter mSimpleAdapter;
	private HashMap<String, Object> mHashMap;
	private View mFooterView;
	private ArrayList<HashMap<String, Object>> mArrayList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
	private void init(){
		LayoutInflater layoutInflater = 
		(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		mFooterView = layoutInflater.inflate(R.layout.listview_footer, null);
		
		mListView=(ListView) findViewById(R.id.listView);
		mArrayList=new ArrayList<HashMap<String,Object>>();
		addDataForListView();
		mSimpleAdapter=new SimpleAdapter
	   (MainActivity.this, mArrayList, R.layout.listviewitem, new String []{"id"},new int []{R.id.textView});
		//为ListView设置FooterView
		mListView.addFooterView(mFooterView);
		//为ListView设置Adapter
		mListView.setAdapter(mSimpleAdapter);
		mListView.setOnScrollListener(new OnScrollListenerImple());
		
		
	}
	private void addDataForListView(){
		System.out.println("====>Add data to ListView");
		for (int i = 0; i < 10; i++) {
			mHashMap=new HashMap<String, Object>();
			mHashMap.put("id", ""+i);
			mArrayList.add(mHashMap);
		}
	}
	private class OnScrollListenerImple implements OnScrollListener{
		@Override
		public void onScroll(AbsListView listView, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
			int lastItem = firstVisibleItem + visibleItemCount;
			System.out.println("firstVisibleItem="+firstVisibleItem);
			System.out.println("visibleItemCount="+visibleItemCount);
			System.out.println("totalItemCount="+totalItemCount);
			System.out.println("mArrayList.size()="+mArrayList.size());
            if(lastItem == totalItemCount) {
            	View lastItemView=(View) listView.getChildAt(listView.getChildCount()-1);
            	if ((listView.getBottom())==lastItemView.getBottom()) {
					System.out.println("==Scroll to the listview bottom ==");
					if (mFooterView != null&&mListView.getFooterViewsCount()!=0) {
						try {
							//模拟数据加载业务
							Thread.sleep(3000);
							
							//每次总是先remove掉FooterView
							mListView.removeFooterView(mFooterView);
						} catch (Exception e) {
							
						}
					}
					if (mArrayList.size()<30) {
						if (mFooterView != null) {
							//若有需求再add上FooterView
							mListView.addFooterView(mFooterView);
							addDataForListView();
							mSimpleAdapter.notifyDataSetChanged();
						}
					}
				}
            }
		}

		@Override
		public void onScrollStateChanged(AbsListView listview, int scrollState) {
			
		}
		
	}

}


main.xml如下:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fastScrollEnabled="false"
        android:smoothScrollbar="true"
        android:layout_centerInParent="true"
   />

</RelativeLayout>

 

listview_footer.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/footer_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="30dip"
    android:paddingBottom="30dip"
    android:clickable="true" >

    <TextView
        android:id="@+id/footer_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="loading more"
        android:textSize="25sp" 
    />

    <ProgressBar
        android:id="@+id/footer_progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" 
    />

</RelativeLayout>