listView分页加载

来源:互联网 发布:公司流程优化建议 编辑:程序博客网 时间:2024/06/03 12:25

MainActivity .java

package com.example.listview_pager;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import tools.HttpUtil;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;


public class MainActivity extends Activity {
private int number = 0;
private ListView lv_list;
private String str0 = null;
// 适配器
private MyAdapter adapter;


// 总数据源
private List<String> oList = new ArrayList<String>();


// 当前数据源
private List<String> single_list = new ArrayList<String>();


private LinearLayout ll_more;
private ProgressBar pBar;
private TextView tv_more;


// 页数
private int page = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_list = (ListView) this.findViewById(R.id.lv_list);
init_date();


LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.progress_layout, null);


ll_more = (LinearLayout) view.findViewById(R.id.ll_more);
pBar = (ProgressBar) view.findViewById(R.id.pro);
tv_more = (TextView) view.findViewById(R.id.tv_more);


ll_more.setVisibility(View.GONE);
pBar.setVisibility(View.GONE);


lv_list.addFooterView(view);


init_list();


lv_list.setOnScrollListener(listener);


}


private int total = 0;
private OnScrollListener listener = new OnScrollListener() {


@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if (isMore && total == adapter.getCount()
&& scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
lv_list.setOnScrollListener(null);
init_page();
thread_date();
}


}


@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
total = firstVisibleItem + visibleItemCount - 1;
}
};


public void init_page() {
if (isMore) {
ll_more.setVisibility(View.VISIBLE);
pBar.setVisibility(View.VISIBLE);
tv_more.setText("正在加载,请稍后...");
page++;
}
}


public void thread_date() {
new Thread() {
public void run() {
try {
sleep(500);
handler.sendEmptyMessage(123);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
}


private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 123) {
lv_list.setOnScrollListener(listener);
init_list();
if (isMore) {
pBar.setVisibility(View.GONE);
tv_more.setText("更多内容...");
} else {
pBar.setVisibility(View.GONE);
tv_more.setText("总共" + single_list.size() + "项");
}
} else if (msg.what == 01234) {
Log.e("TAG", str0);
try {
JSONArray jsonArray = new JSONArray(str0);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
single_list.add(jsonObject.getString("title"));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
};


private boolean isMore = false;


public void init_list() {
List<String> infos = null;
if (oList.size() - single_list.size() < 10) {
infos = oList.subList((page - 1) * 10, oList.size());
} else {
infos = oList.subList((page - 1) * 10, page * 10);
}


if (infos.size() < 10) {
isMore = false;
} else {
isMore = true;
}


single_list.addAll(infos);


if (adapter == null) {
adapter = new MyAdapter(this, single_list);
lv_list.setAdapter(adapter);
} else {
// ;刷新适配器,更新数据
adapter.notifyDataSetChanged();
}


}


public void init_date() {
new Thread() {
public void run() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("start", number);
str0 = HttpUtil
.doGet(HttpUtil.path + "GetAll_Info_Paging", map);
handler.sendEmptyMessage(01234);
};
}.start();
}

}




MyAdapter .java

package com.example.listview_pager;


import java.util.List;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class MyAdapter extends BaseAdapter {
private Context context;
private List<String> oList;
private LayoutInflater inflater;

public MyAdapter(Context context,List<String> oList) {
this.context=context;
this.oList=oList;
this.inflater=LayoutInflater.from(context);
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return oList.size();
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return oList.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) {
ViewHolder holder;
if (convertView==null) {
holder=new ViewHolder();
convertView=inflater.inflate(R.layout.item_layout, null);
holder.tv_name=(TextView) convertView.findViewById(R.id.tv_name);

convertView.setTag(holder);
}else {
holder=(ViewHolder) convertView.getTag();
}
holder.tv_name.setText(oList.get(position));
return convertView;
}

private class ViewHolder{
TextView tv_name;
}


}




activity_main.xml

<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"
    tools:context=".MainActivity" >


   <ListView 
       android:id="@+id/lv_list"
       android:scrollbars="none"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>


</RelativeLayout>


item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MyAdapter" >


    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="11111"
        android:textSize="24sp" />


</LinearLayout>



item_layout.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:gravity="center"
    android:id="@+id/ll_more"
    android:orientation="horizontal" >
    
    <ProgressBar 
        android:id="@+id/pro"
        style="@android:style/Widget.DeviceDefault.Light.ProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
    <TextView 
        android:id="@+id/tv_more"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="正在加载。。。"/>


</LinearLayout>

0 0
原创粉丝点击