android listview分页显示,android ListView的分段显示、分页显示(附源码)
Java代码 packagecom.yangguangfu.listview;importandroid.app.ListActivity;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.Gravity;importandroid.view.View;importandroid.view.ViewGroup;im.
Java代码
packagecom.yangguangfu.listview;
importandroid.app.ListActivity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.Gravity;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.AbsListView;
importandroid.widget.BaseAdapter;
importandroid.widget.LinearLayout;
importandroid.widget.ListView;
importandroid.widget.ProgressBar;
importandroid.widget.TextView;
importandroid.widget.AbsListView.OnScrollListener;
importandroid.widget.LinearLayout.LayoutParams;
publicclassMainActivityextendsListActivityimplementsOnScrollListener {
privatestaticfinalString TAG ="MainActivity";
privatelistViewAdapter adapter =newlistViewAdapter();
privateintlastItem =0;
privateLayoutParams WClayoutParams =newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
privateLayoutParams FFlayoutParams =newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
privateProgressBar progressBar;
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG,"onCreate(Bundle savedInstanceState)");
//线性布局
LinearLayout layout =newLinearLayout(this);
//设置布局 水平方向
layout.setOrientation(LinearLayout.HORIZONTAL);
//进度条
progressBar =newProgressBar(this);
//进度条显示位置
progressBar.setPadding(0,0,15,0);
layout.addView(progressBar, WClayoutParams);
TextView textView =newTextView(this);
textView.setText("加载中...");
textView.setGravity(Gravity.CENTER_VERTICAL);
layout.addView(textView, FFlayoutParams);
layout.setGravity(Gravity.CENTER);
LinearLayout loadingLayout =newLinearLayout(this);
loadingLayout.addView(layout, WClayoutParams);
loadingLayout.setGravity(Gravity.CENTER);
//得到一个ListView用来显示条目
ListView listView = getListView();
//添加到脚页显示
listView.addFooterView(loadingLayout);
//
registerForContextMenu(listView);
//
setListAdapter(adapter);
listView.setOnScrollListener(this);
}
publicvoidonScroll(AbsListView v,intfirstVisibleItem,
intvisibleItemCount,inttotalItemCount) {
lastItem = firstVisibleItem + visibleItemCount -1;
System.out.println("lastItem:"+ lastItem);
Log.i(TAG,"lastItem:"+ lastItem);
}
publicvoidonScrollStateChanged(AbsListView v,intstate) {
if(lastItem == adapter.count
&& state == OnScrollListener.SCROLL_STATE_IDLE) {
adapter.count +=10;
adapter.notifyDataSetChanged();
Log.i(TAG,"lastItem:"+ lastItem);
}
}
classlistViewAdapterextendsBaseAdapter {
intcount =10;
publicintgetCount() {
Log.i(TAG,"count:"+ count);
returncount;
}
publicObject getItem(intpos) {
Log.i(TAG,"pos:"+ pos);
returnpos;
}
publiclonggetItemId(intpos) {
returnpos;
}
publicView getView(intpos, View v, ViewGroup p) {
TextView view =newTextView(MainActivity.this);
view.setText("阿福播放器 "+ pos);
Log.i(TAG,"entry: "+ pos);
view.setTextSize(20f);
view.setHeight(80);
returnview;
}
}
}
package com.yangguangfu.listview;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends ListActivity implements OnScrollListener {
private static final String TAG = "MainActivity";
private listViewAdapter adapter = new listViewAdapter();
private int lastItem = 0;
private LayoutParams WClayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
private LayoutParams FFlayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
private ProgressBar progressBar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate(Bundle savedInstanceState)" );
//线性布局
LinearLayout layout = new LinearLayout(this);
//设置布局 水平方向
layout.setOrientation(LinearLayout.HORIZONTAL);
//进度条
progressBar = new ProgressBar(this);
//进度条显示位置
progressBar.setPadding(0, 0, 15, 0);
layout.addView(progressBar, WClayoutParams);
TextView textView = new TextView(this);
textView.setText("加载中...");
textView.setGravity(Gravity.CENTER_VERTICAL);
layout.addView(textView, FFlayoutParams);
layout.setGravity(Gravity.CENTER);
LinearLayout loadingLayout = new LinearLayout(this);
loadingLayout.addView(layout, WClayoutParams);
loadingLayout.setGravity(Gravity.CENTER);
//得到一个ListView用来显示条目
ListView listView = getListView();
//添加到脚页显示
listView.addFooterView(loadingLayout);
//
registerForContextMenu(listView);
//
setListAdapter(adapter);
listView.setOnScrollListener(this);
}
public void onScroll(AbsListView v, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
lastItem = firstVisibleItem + visibleItemCount - 1;
System.out.println("lastItem:" + lastItem);
Log.i(TAG, "lastItem:" + lastItem);
}
public void onScrollStateChanged(AbsListView v, int state) {
if (lastItem == adapter.count
&& state == OnScrollListener.SCROLL_STATE_IDLE) {
adapter.count += 10;
adapter.notifyDataSetChanged();
Log.i(TAG, "lastItem:" + lastItem);
}
}
class listViewAdapter extends BaseAdapter {
int count = 10;
public int getCount() {
Log.i(TAG, "count:" + count);
return count;
}
public Object getItem(int pos) {
Log.i(TAG, "pos:" + pos);
return pos;
}
public long getItemId(int pos) {
return pos;
}
public View getView(int pos, View v, ViewGroup p) {
TextView view = new TextView(MainActivity.this);
view.setText("阿福播放器 " + pos);
Log.i(TAG, "entry: " + pos);
view.setTextSize(20f);
view.setHeight(80);
return view;
}
}
}
Java代码
packagecom.yangguangfu.listview;
importandroid.app.ListActivity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.Gravity;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.AbsListView;
importandroid.widget.BaseAdapter;
importandroid.widget.Button;
importandroid.widget.LinearLayout;
importandroid.widget.ListView;
importandroid.widget.TextView;
importandroid.widget.LinearLayout.LayoutParams;
publicclassButtonLoadingActivityextendsListActivity {
privatestaticfinalString TAG ="ButtonLoadingActivity";
privatelistViewAdapter adapter =newlistViewAdapter();
privateLayoutParams WClayoutParams =newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
privateLayoutParams FFlayoutParams =newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG,"onCreate(Bundle savedInstanceState):");
LinearLayout layout =newLinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
Button button =newButton(this);
button.setText("点击加载下五条...");
button.setGravity(Gravity.CENTER_VERTICAL);
layout.addView(button,FFlayoutParams);
layout.setGravity(Gravity.CENTER);
LinearLayout loadingLayout =newLinearLayout(this);
loadingLayout.addView(layout,WClayoutParams);
loadingLayout.setGravity(Gravity.CENTER);
ListView listView = getListView();
listView.addFooterView(loadingLayout);
button.setOnClickListener(newButton.OnClickListener()
{@Override
publicvoidonClick(View v)
{
adapter.count +=5;
Log.i(TAG,"setOnClickListener:"+ adapter.count);
adapter.notifyDataSetChanged();
}
});
setListAdapter(adapter);
}
publicvoidonScrollStateChanged(AbsListView v,ints) {
Log.i(TAG,"onScrollStateChanged(AbsListView v, int s)");
}
classlistViewAdapterextendsBaseAdapter {
intcount =10;
publicintgetCount() {returncount; }
publicObject getItem(intpos) {returnpos; }
publiclonggetItemId(intpos) {returnpos; }
publicView getView(intpos, View v, ViewGroup p) {
TextView textView =newTextView(ButtonLoadingActivity.this);
textView.setHeight(80);
textView.setTextSize(20);
textView.setText("阿福播放器 "+ pos);
Log.i(TAG,"getView:pos:"+ pos);
returntextView;
}
}
}
package com.yangguangfu.listview;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class ButtonLoadingActivity extends ListActivity {
private static final String TAG = "ButtonLoadingActivity";
private listViewAdapter adapter = new listViewAdapter();
private LayoutParams WClayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
private LayoutParams FFlayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate(Bundle savedInstanceState):" );
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
Button button = new Button(this);
button.setText("点击加载下五条...");
button.setGravity(Gravity.CENTER_VERTICAL);
layout.addView(button,FFlayoutParams);
layout.setGravity(Gravity.CENTER);
LinearLayout loadingLayout = new LinearLayout(this);
loadingLayout.addView(layout,WClayoutParams);
loadingLayout.setGravity(Gravity.CENTER);
ListView listView = getListView();
listView.addFooterView(loadingLayout);
button.setOnClickListener(new Button.OnClickListener()
{ @Override
public void onClick(View v)
{
adapter.count += 5;
Log.i(TAG, "setOnClickListener:" + adapter.count);
adapter.notifyDataSetChanged();
}
});
setListAdapter(adapter);
}
public void onScrollStateChanged(AbsListView v, int s) {
Log.i(TAG, "onScrollStateChanged(AbsListView v, int s)");
}
class listViewAdapter extends BaseAdapter {
int count = 10;
public int getCount() { return count; }
public Object getItem(int pos) { return pos; }
public long getItemId(int pos) { return pos; }
public View getView(int pos, View v, ViewGroup p) {
TextView textView = new TextView(ButtonLoadingActivity.this);
textView.setHeight(80);
textView.setTextSize(20);
textView.setText("阿福播放器 " + pos);
Log.i(TAG, "getView:pos:" + pos);
return textView;
}
}
}
更多推荐
所有评论(0)