您的位置:首页 > 其它

安卓开发中,listview数量过多,加更多按钮显示

2014-04-01 14:24 218 查看
更多按钮的页面,如:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="30dp"

android:orientation="horizontal"

>

<TextView

android:id="@+id/load_more_view_tv"

android:layout_width="200dp"

android:layout_height="30dp"

android:gravity="center"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

android:layout_margin="18sp"

android:background="@color/sj_bg_blue"

android:textColor="@color/white"

android:text="更多"

android:textSize="20sp" />

<ProgressBar

android:id="@+id/load_more_view_progress"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:layout_marginLeft="8dp"

android:layout_toRightOf="@id/load_more_tv"

android:indeterminate="true"

android:visibility="invisible" />

</RelativeLayout>

在要显示listview的activity中,有

View loadMoreView;

TextView loadMoreTextView;

ProgressBar loadMoreProgressBar;

private int currentCount = 1;

private int incrementCount = 5;

private ArrayList list;

LayoutInflater l = LayoutInflater.from(this);

loadMoreView = l.inflate(R.layout.load_more_view, null);

loadMoreTextView = (TextView) loadMoreView.findViewById(R.id.load_more_view_tv);

loadMoreProgressBar = (ProgressBar) loadMoreView.findViewById(R.id.load_more_view_progress);

下载listview的数据后,进行数据显示,如:

private void setListView(){

if (list.getFooterViewsCount() < 1) {

list.addFooterView(loadMoreView);//添加页脚(放在ListView最后)。该方法要求在setAdapter之前调用。

}

int newsSize = list.size();

if (currentCount >= newsSize) {

currentCount = newsSize;

showNoLoadMore();

} else {

showLoadMore();

}

loaded();

}

private void loaded(){

list.setAdapter(myAdapter);

list.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

// TODO Auto-generated method stub

if (arg1.equals(loadMoreView)) {

loadMoreStart();

currentCount += incrementCount;

if (currentCount >= list.size()) {

myAdapter.setCount(list.size());

myAdapter.notifyDataSetChanged();

showNoLoadMore();

} else {

myAdapter.setCount(currentCount);

myAdapter.notifyDataSetChanged();

loadMoreEnd();

}

}else{

/////

}

}

});

}

这样就可以控制每次显示的数量,并加上更多按钮,点击时,会增加显示数据的数量
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: