ListView动态显示或隐藏FooterView

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>


 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值