2.显示更多既可以点击,也可以拖拽。
3.ListView item有绑定有事件,可以点击!
直接上代码:
以下是最主要的Activity:
package com.example.helloworld;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.adapter.ListItemAdapter;
import com.example.domain.GoodsRes;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
public class GoodsListActivity extends Activity implements OnScrollListener{
//private List<GoodsRes> list = new ArrayList<GoodsRes>();
private List<GoodsRes> glist;
ListView grLv ;
private Button searchMoreBtn;
private LinearLayout loadView;
private int currentPageCount = 1;
private int lastItem;
private Handler myHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what == 1){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
searchMoreBtn.setVisibility(View.VISIBLE);
loadView.setVisibility(View.GONE);
ListItemAdapter adapter = new ListItemAdapter(glist,GoodsListActivity.this);
grLv.setAdapter(adapter);
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.goods_list);
grLv = (ListView) findViewById(R.id.goods_listView);
//searchMoreBtn = (Button) findViewById(R.id.search_more);
addPageMore();
MyThread thread = new MyThread(1, 20);
thread.start();
//list = thread.getGlist();
grLv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int index,long id) {
GoodsRes gr = glist.get(index);
Intent intent = new Intent(GoodsListActivity.this, GoodsDetailActivity.class);
Bundle data = new Bundle();
intent.putExtra("gr", gr);
startActivity(intent);
}
});
grLv.setOnScrollListener(this);
}
public void addPageMore(){
View view = LayoutInflater.from(this).inflate(R.layout.list_page_load, null);
searchMoreBtn = (Button) view.findViewById(R.id.search_more);
loadView = (LinearLayout) view.findViewById(R.id.load_id);
searchMoreBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
currentPageCount++;
MyThread th = new MyThread(currentPageCount, 20);
searchMoreBtn.setVisibility(View.GONE);
loadView.setVisibility(View.VISIBLE);
th.start();
}
});
grLv.addFooterView(view);
}
class MyThread extends Thread{
private HttpClient httpClient;
private int pageTo = 1; //默认第一页
private int pageNum = 20; //默认每页显示15条
public MyThread(int pageTo, int pageNum) {
super();
this.pageTo = pageTo;
this.pageNum = pageNum;
}
@Override
public void run() {
Looper.prepare();
httpClient = new DefaultHttpClient();
String url = "http://192.168.1.103:8080/TransportMarket/mGoodsResourceAction!getGoodsResourceData.do?pg="+this.pageTo+"&nm="+this.pageNum;
HttpPost post = new HttpPost(url);
try {
HttpResponse resp = httpClient.execute(post);
String msg = EntityUtils.toString(resp.getEntity());
JSONObject obj = new JSONObject(msg);
String gl = obj.getString("gl");
JSONArray ga = new JSONArray(gl);
glist = new ArrayList<GoodsRes>();
for(int i = 0; i<ga.length(); i++){
JSONObject js = (JSONObject) ga.get(i);
String id = js.getString("id");
String tt = js.getString("tt");
String cl = js.getString("cl");
String sd = js.getString("sd");
String pn = js.getString("pn");
String ct = js.getString("ct");
GoodsRes gr = new GoodsRes();
gr.setId(id);
gr.setPn(pn);
gr.setCt(ct);
gr.setTt(tt);
gr.setSd(sd);
glist.add(gr);
}
//ListView grLv = (ListView) findViewById(R.id.goods_listView);
//ListItemAdapter adapter = new ListItemAdapter(glist,GoodsListActivity.this);
//grLv.setAdapter(adapter);
Message msgs = new Message();
msgs.what = 1;
myHandler.sendMessage(msgs);
Looper.loop();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
public int getPageTo() {
return pageTo;
}
public void setPageTo(int pageTo) {
this.pageTo = pageTo;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
}
public List<GoodsRes> getGlist() {
return glist;
}
public void setGlist(List<GoodsRes> glist) {
this.glist = glist;
}
public int getCurrentPageCount() {
return currentPageCount;
}
public void setCurrentPageCount(int currentPageCount) {
this.currentPageCount = currentPageCount;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
lastItem = firstVisibleItem + visibleItemCount - 1; //减1是因为上面加了个addFooterView
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(lastItem == glist.size() && scrollState == this.SCROLL_STATE_IDLE){
currentPageCount++;
MyThread th = new MyThread(currentPageCount, 20);
searchMoreBtn.setVisibility(View.GONE);
loadView.setVisibility(View.VISIBLE);
th.start();
}
}
}
以下是Adapter
package com.example.adapter;
import java.util.List;
import com.example.domain.GoodsRes;
import com.example.helloworld.R;
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 ListItemAdapter extends BaseAdapter{
private List<GoodsRes> list;
private Context ctx;
public ListItemAdapter(List<GoodsRes> list, Context ctx) {
super();
this.list = list;
this.ctx = ctx;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int index) {
return list.get(index);
}
@Override
public long getItemId(int index) {
return index;
}
@Override
public View getView(int index, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.goods_item,null);
}
if(list != null && list.size() > 0){
TextView textView_title = (TextView) v.findViewById(R.id.goods_item_title);
textView_title.setText(list.get(index).getTt());
TextView textView_brief = (TextView) v.findViewById(R.id.goods_item_brief);
textView_brief.setText(list.get(index).getSd());
}
return v;
}
public List<GoodsRes> getList() {
return list;
}
public void setList(List<GoodsRes> list) {
this.list = list;
}
public Context getCtx() {
return ctx;
}
public void setCtx(Context ctx) {
this.ctx = ctx;
}
}
以下是列表显示数据List的POJO
package com.example.domain;
import java.io.Serializable;
public class GoodsRes implements Serializable{
private String id;
private String tt;
private String cl;
private String sd;
private String pn;
private String ct;
private String ot;
private String cn;
private String pd;
private String pg;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTt() {
return tt;
}
public void setTt(String tt) {
this.tt = tt;
}
public String getCl() {
return cl;
}
public void setCl(String cl) {
this.cl = cl;
}
public String getSd() {
return sd;
}
public void setSd(String sd) {
this.sd = sd;
}
public String getPn() {
return pn;
}
public void setPn(String pn) {
this.pn = pn;
}
public String getCt() {
return ct;
}
public void setCt(String ct) {
this.ct = ct;
}
public String getOt() {
return ot;
}
public void setOt(String ot) {
this.ot = ot;
}
public String getCn() {
return cn;
}
public void setCn(String cn) {
this.cn = cn;
}
public String getPd() {
return pd;
}
public void setPd(String pd) {
this.pd = pd;
}
public String getPg() {
return pg;
}
public void setPg(String pg) {
this.pg = pg;
}
}
以下是xml文件
1.goods_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/goods_listView"
android:layout_width="wrap_content"
android:divider="@color/black"
android:dividerHeight="2dp"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
2.goods_item.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:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/goods_item_img"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="4"
android:orientation="vertical" >
<TextView
android:id="@+id/goods_item_title"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center"
android:text="nihao" />
<TextView
android:id="@+id/goods_item_brief"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:singleLine="true"
android:ellipsize="end"
android:layout_weight="1" />
</LinearLayout>
<!-- <LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:focusable="false"
android:id="@+id/goods_item_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/phone" />
</LinearLayout> -->
</LinearLayout>
</LinearLayout>
3.list_page_load.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:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="5dp">
<Button
android:id="@+id/search_more"
android:layout_width="fill_parent"
android:layout_height="32dp"
android:background="@drawable/sear_more"/>
<LinearLayout
android:id="@+id/load_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/red"
android:orientation="horizontal"
android:visibility="gone">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleSmall"
android:indeterminateDrawable="@drawable/progressbar_shape"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:text="正在加载..."
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>