二、手机客户端
①activity_main.xml中加一个ListView控件,注意一定要更改android:layout_height属性参数为android:layout_height="match_parent"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android_listview_paging.MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
</ListView>
</RelativeLayout>
②添加网络授权
③问题难点:怎么判断listview滑到最下面,滑到下面的状态和动作怎么处理。
创建数据源->自定义一个适配器,显示数据如下。
④当上下滑动的时候,某个item项会滑进滑出,滑进滑出的item都是需要重绘的。判断是否滑到下面,则绑定事件setOnScrollListener(new OnScrollListener());
@OverridefirstVisibleItem第一个进来的条目
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
System.out.println("---firstVisibleItem-->>"+firstVisibleItem);
System.out.println("---visibleItemCount-->>"+visibleItemCount);
System.out.println("---totalItemCount-->>"+totalItemCount);
}
visibleItemCount可以看到的条目,即整个屏幕可以装载的条目
totalItemCount整个记录总条目
从图中可以看出当滑到最下面的时候,存在firstVisibleItem+visibleItemCount=totalItemCount
@OverridescrollState停止是0( SCROLL_STATE_IDLE),移动是1(SCROLL_STATE_TOUCH_SCROLL)
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
System.out.println("---scrollState-->>"+scrollState);
}
如果条目达到总条数,并且用户手指已经从屏幕上移走不再移动了,则要分页了。
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(isPaging&&scrollState==OnScrollListener.SCROLL_STATE_IDLE){
Toast.makeText(MainActivity.this, "分页,获取数据", 1).show();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
isPaging=(firstVisibleItem+visibleItemCount==totalItemCount);
}
});
package com.example.android_listview_paging;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private ListView listView;
private MyAdapter adapter;
private boolean isPaging;//是否进行分页操作
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) this.findViewById(R.id.listView1);
adapter=new MyAdapter();
adapter.binData(getData());
listView.setAdapter(adapter);
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(isPaging&&scrollState==OnScrollListener.SCROLL_STATE_IDLE){
Toast.makeText(MainActivity.this, "分页,获取数据", 1).show();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
isPaging=(firstVisibleItem+visibleItemCount==totalItemCount);
}
});
}
//自定义适配器
class MyAdapter extends BaseAdapter{
List<String> list;//本地化list
public MyAdapter() {
// TODO Auto-generated constructor stub
}
//绑定数据
public void binData(List<String> list){
this.list=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView textView=null;
if(convertView==null){
textView=new TextView(MainActivity.this);
}else{
textView=(TextView) convertView;
}
textView.setTextSize(20);
textView.setText(list.get(position));
return textView;
}
}
//数据源
public List<String> getData(){
List<String> list=new ArrayList<String>();
for(int i=0;i<=35;i++){
list.add("CCNU"+i);
}
return list;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}