Android ListView下拉加载更多,http请求加载数据,Item点击事件
来源:互联网 发布:淘宝打折软件怎么设置 编辑:程序博客网 时间:2024/05/02 00:43
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 blocke.printStackTrace();}searchMoreBtn.setVisibility(View.VISIBLE);loadView.setVisibility(View.GONE);ListItemAdapter adapter = new ListItemAdapter(glist,GoodsListActivity.this);grLv.setAdapter(adapter);}};};@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic 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;}@Overridepublic 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;}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {lastItem = firstVisibleItem + visibleItemCount - 1; //减1是因为上面加了个addFooterView}@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {// TODO Auto-generated method stubif(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;}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int index) {return list.get(index);}@Overridepublic long getItemId(int index) {return index;}@Overridepublic 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>
- Android ListView下拉加载更多,http请求加载数据,Item点击事件
- Android ListView 下拉刷新 点击加载更多
- Android ListView点击加载更多
- ListView的下拉刷新,点击加载更多
- listView下拉加载更多
- android listview 下拉刷新以及加载更多
- android listview 下拉加载更多demo
- Android ListView实现下拉刷新、加载更多
- RecyclerView基础-数据适配、自定义分割线、上拉加载、下拉加载、item点击事件
- android 仿google play效果,ListView/GridView下拉刷新,上拉加载更多,自动加载异步请求数据以及分页数据
- android下拉加载更多
- android ListView的上部下拉刷新下部点击加载更多具体实现及拓展
- android ListView的上部下拉刷新下部点击加载更多具体实现及拓展
- android ListView的上部下拉刷新下部点击加载更多具体实现及拓展
- android ListView的上部下拉刷新下部点击加载更多具体实现及拓展
- android ListView的上部下拉刷新下部点击加载更多具体实现及拓展
- Android自定义View之快速实现下拉刷新, 点击加载更多ListView
- ListView 下拉刷新 加载更多
- 深入Struts2的配置文件
- SharePoint 2013 list froms(new/update/view) 某个字段只对某个SharePoint组的成员可见
- js 获取前天、昨天、今天、明天、后天的时间 (转)
- PHP cookie和session的分析
- 标准C++中的string类的用法总结
- Android ListView下拉加载更多,http请求加载数据,Item点击事件
- Eclipse helios 配置struts2图解 .
- 李开复:给中国学生的一封信
- Android WindowManager$BadTokenException异常
- 《班级管理系统》sql语句
- Java集合类详解
- jquery ui 代码重写(还没想到更好说法,暂时这么叫吧)
- Androidpn 消息推送总结
- JAVA中clone浅复制