ListView下拉加载一(分页)

时间:2022-09-12 18:13:32

首先创建在主xml里放置一个listview列表,代码如下:

<LinearLayout 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"
tools:context=".MainActivity" > <ListView
android:id="@+id/menuList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="" >
</ListView> </LinearLayout>

再创建2个xml,分别为列表项(menu.xml)和加载界面(loadmore.xml)

menu.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/showView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
style="@style/normalText"
android:text="title"
/>
<TextView
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text=""
/>
</LinearLayout>

loadmore.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/list_bg"
>
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="25dp"
android:layout_height="25dp" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在加载数据..." />
</LinearLayout>
</LinearLayout>

loadmore的背景样式list_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#ccc"
android:endColor="#ccc"
android:angle=""
/>
</shape>

入口代码如下:

package com.tp.soft.app;

import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView; public class MainActivity extends Activity{ private ListView mListView; private View mLoadMoreView; private ProgressBar mLoadBtn; private PageAdapter adapter; private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mLoadMoreView = getLayoutInflater().inflate(R.layout.loadmore, null); mLoadBtn = (ProgressBar) mLoadMoreView.findViewById(R.id.progressBar1);
//mLoadBtn.setOnClickListener(this); mListView = (ListView) findViewById(R.id.menuList); //下拉滚动触发事件
mListView.setOnScrollListener(new OnScrollListener() { @Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//不滚动
if(scrollState == OnScrollListener.SCROLL_STATE_IDLE){
//滚动最底部
if(view.getLastVisiblePosition() == view.getCount() -){
//mLoadBtn.setText("正在加载中...");
MyRunnable r = new MyRunnable();
handler.postDelayed(r, );
}
}
} @Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
}); mListView.addFooterView(mLoadMoreView);
List<String> itemList = new ArrayList<String>();
for (int i = ; i < ; i++) {
itemList.add("title"+i);
} adapter = new PageAdapter(itemList);
mListView.setAdapter(adapter);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} /*@Override
public void onClick(View v) {
mLoadBtn.setText("正在加载中...");
MyRunnable r = new MyRunnable();
handler.postDelayed(r, 2000);
}*/ class MyRunnable implements Runnable{ @Override
public void run() {
Log.e("提示", "");
loadMoreDate();
//更新UI
adapter.notifyDataSetChanged();
//mLoadBtn.setText("查看更多...");
}
} class PageAdapter extends BaseAdapter { List<String> itemList; public PageAdapter(List<String> itemList){
this.itemList = itemList;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.menu, null);
}
TextView titleView = (TextView) convertView.findViewById(R.id.showView);
titleView.setText(itemList.get(position));
return convertView;
} @Override
public long getItemId(int position) {
return position;
} @Override
public Object getItem(int position) {
return itemList.get(position);
} @Override
public int getCount() {
return itemList.size();
} public void addItem(String str){
itemList.add(str);
}
} private void loadMoreDate() {
Log.e("总数", ""+adapter.getCount());
int count = adapter.getCount();
for(int i = count+; i < count+; i++){
adapter.addItem("title"+i);
}
}
}

运行结果:

ListView下拉加载一(分页)