将修正好的Xlistview镶嵌进项目中

最近公司项目中要用到上拉加载更多,下拉刷新功能。githup中提供现成的Xlistview,只是xlistview中在没有加载满数据的时候,上拉加载的时候会出现一些小问题,不能及时加载会卡住,这个需要在xlistview中将if (getLastVisiblePosition() == mTotalItemCount – 1) (304行左右)中的if 前面是else去掉,这样就可以用了 修改好的 xlistview下载地址,有了xlistview我们就开始往项目中镶入吧。

首先将XListView,XListViewFooter,XListViewHeader拷贝到类中,将list_item,xlistview_footer,xlistview_header放进laydout中,将values中的strings内的内容相应的考呗进项目中,将drawable中的xlistview_arrow相应的考呗到项目中,接下来就是修改最重要的MainActivity了。

要用到xlistview所以你的activity_main中的listview必须用类名下的xlistview 示例如下:

<span style="font-size:14px;"><com.tyb.tools.XListViewandroid:id="@+id/listview"android:layout_width="fill_parent"android:layout_height="match_parent"android:cacheColorHint="#00000000" > </com.tyb.tools.XListView></span> 在MainActivity 中去实现IXListViewListener有两个方法onLoadMore()上拉加载更多,onRefresh()下拉刷新,cleanData();pross.setVisibility(View.VISIBLE);loadData();listview.setPullLoadEnable(true);cleanData();用来清除数据,pross.setVisibility(View.VISIBLE);进入列表页面进步条可见,loadData();访问网络获取数据,listview.setPullLoadEnable(true);设置listview可滑动。我先把自己项目中的一个上拉加载更多和下拉刷新的activity放上来讲解。

package com.tyb.custom;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 android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.SharedPreferences;import android.os.AsyncTask;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.ListView;import android.widget.ProgressBar;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;import com.tyb.R;import com.tyb.tools.BaseActivity;import com.tyb.tools.GetPost;import com.tyb.tools.GlobalConts;import com.tyb.tools.RequestParams;import com.tyb.tools.XListView;import com.tyb.tools.XListView.IXListViewListener;public class CustomActivity extends BaseActivity implements IXListViewListener{private final String PREFERENCES_NAME = GlobalConts.SHAREPREFRENCE_NAME;private String uid="",cid="",pwd="";private customdata customdata;private XListView listview;private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();;private ImageView btn_back,btn_up;private String name="";private RelativeLayout data;private TextView tv_nodata;private Handler mHandler;private int page=1;private MyAdapter adapter;private ProgressBar pross;private Context context;private View view;private EditText et_name,et_phoe;private boolean ifloadsearch=false;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState,R.layout.custom_main);SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);cid=preferences.getString("cid", "");uid=preferences.getString("uid", "");pwd=preferences.getString("pwd", "");if(GlobalConts.isNetworkAvailable(this)){data.setVisibility(View.GONE);}else{data.setVisibility(View.VISIBLE);tv_nodata.setText("请确认连接网络!");return ;}cleanData();pross.setVisibility(View.VISIBLE);loadData();listview.setPullLoadEnable(true);}private void cleanData() {// TODO Auto-generated method stubifloadsearch=false;list.clear();customdata=new customdata();customdata.setLinkman("");customdata.setMob("");customdata.setPageall("1");customdata.setCurrentpage("1");page=1;}private void loadData() {// TODO Auto-generated method stub/* * :8023/port/custom.aspx? * cid=001&user=super&pwd=123&action=list * &pagenum=&cno=&cname=&linkman=&mob=& * point=&address=&vtime=2015-02-11&content= * &nexttime=2015-02-12&nextcontent=&attention=&vstime= * 2015-02-11&vetime=2015-02-11 */RequestParams params=new RequestParams();params.put("cid", cid);params.put("user", uid);params.put("pwd", pwd);params.put("pagenum", customdata.getCurrentpage());params.put("linkman", customdata.getLinkman());params.put("mob", customdata.getMob());params.put("action", "list");new GetLodeTask().execute(params.toString());}class GetLodeTask extends AsyncTask<String, String, String>{@Overrideprotected String doInBackground(String… arg0) {// TODO Auto-generated method stubGetPost getPost=new GetPost();try {return getPost.urlPost(arg0[0], getPost.URL_CUSTOM);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return "4";}@Overrideprotected void onPostExecute(String result) {// TODO Auto-generated method stubsuper.onPostExecute(result);try {if(result.equals("4")){pross.setVisibility(View.GONE);Toast.makeText(CustomActivity.this, R.string.conn, 1).show();}else{pross.setVisibility(View.GONE);BindAdapter(result);}} catch (JSONException e) {e.printStackTrace();}}}@Overridepublic void findViews() {// TODO Auto-generated method stubcontext=this;btn_up=(ImageView)findViewById(R.id.btn_up);pross=(ProgressBar) findViewById(R.id.pross);tv_nodata=(TextView) findViewById(R.id.tv_nodata);data=(RelativeLayout) findViewById(R.id.data);listview=(XListView) findViewById(R.id.listview);btn_back=(ImageView) findViewById(R.id.btn_back);}@Overridepublic void addListeners() {// TODO Auto-generated method stubbtn_up.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubview=View.inflate(context, R.layout.custom_search, null);et_name=(EditText) view.findViewById(R.id.linkman);et_phoe=(EditText) view.findViewById(R.id.mob);new AlertDialog.Builder(context).setTitle("客户列表查询").setView(view).setNegativeButton("取消", null).setPositiveButton("确定", new AlertDialog.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubString mob=et_phoe.getText().toString();String linkman=et_name.getText().toString();customdata.setLinkman(linkman);customdata.setMob(mob);list.clear();loadData();ifloadsearch=true;}}).create().show();}});btn_back.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubfinish();}});}protected void BindAdapter(String result) throws JSONException {// TODO Auto-generated method stubJSONObject jsonObject = new JSONObject(result.toString());if ("success".equals(jsonObject.getString("flag"))) {onLoad();data.setVisibility(View.GONE);name=jsonObject.getString("ename");JSONArray array = jsonObject.getJSONArray("Custom");customdata.setPageall(jsonObject.getString("pageall"));int l = array.length();for (int i = 0; i < l; i++) {Map<String, Object> map = new HashMap<String, Object>();map.put("id", array.getJSONObject(i).getString("id"));map.put("CustomNo", array.getJSONObject(i).getString("CustomNo"));map.put("Address", array.getJSONObject(i).getString("Address"));map.put("Custom", array.getJSONObject(i).getString("Custom"));map.put("LinkMan", array.getJSONObject(i).getString("LinkMan"));map.put("Mobile", array.getJSONObject(i).getString("Mobile"));map.put("CustomGrade", array.getJSONObject(i).getString("CustomGrade"));list.add(map);}adapter = new MyAdapter(this, list);listview.setAdapter(adapter);adapter.notifyDataSetChanged();listview.setXListViewListener(this);mHandler = new Handler();listview.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stubLog.i("values", "pos:"+arg2);Intent intent=new Intent(CustomActivity.this,CustomDetail.class);int pos=arg2-1;Log.i("values", list.get(pos).get("CustomNo").toString());intent.putExtra("CustomNo", list.get(pos).get("CustomNo").toString());startActivity(intent);}});} else {listview.setAdapter(null);tv_nodata.setText("暂无数据");data.setVisibility(View.VISIBLE);}}private class MyAdapter extends BaseAdapter{private Context context;private List<Map<String, Object>> list;public MyAdapter(Context context,List<Map<String, Object>> list){this.context=context;if(list==null){list=new ArrayList<Map<String,Object>>();}else{this.list=list;}}@Overridepublic int getCount() {// TODO Auto-generated method stubif (list != null)return list.size();elsereturn 0;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubif (list != null)return list.get(position);elsereturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(final int position, View convervierw, ViewGroup arg2) {// TODO Auto-generated method stubViewHolder holder=new ViewHolder();if(convervierw==null){convervierw=View.inflate(context, R.layout.customactivity_item, null);holder.CustomNo=(TextView) convervierw.findViewById(R.id.CustomNo);holder.Custom=(TextView) convervierw.findViewById(R.id.Custom);holder.LinkMan=(TextView) convervierw.findViewById(R.id.LinkMan);holder.Mobile=(TextView) convervierw.findViewById(R.id.Mobile);holder.CustomGrade=(TextView) convervierw.findViewById(R.id.CustomGrade);holder.btn_ct=(Button) convervierw.findViewById(R.id.btn_ct);convervierw.setTag(holder);}else{holder=(ViewHolder) convervierw.getTag();}holder.CustomNo.setText((String)list.get(position).get("CustomNo"));holder.Custom.setText((String)list.get(position).get("Custom"));holder.LinkMan.setText((String)list.get(position).get("LinkMan"));holder.Mobile.setText((String)list.get(position).get("Mobile"));holder.CustomGrade.setText((String)list.get(position).get("CustomGrade"));holder.btn_ct.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent=new Intent(CustomActivity.this,CustomAdd.class);intent.putExtra("CustomNo", (String)list.get(position).get("CustomNo"));intent.putExtra("Custom", (String)list.get(position).get("Custom"));intent.putExtra("name", name);startActivity(intent);}});return convervierw;}public final class ViewHolder {public TextView CustomNo;public TextView Custom;public TextView LinkMan;public TextView Mobile;public TextView CustomGrade;public Button btn_ct;}}@Overridepublic void onRefresh() {// TODO Auto-generated method stubcleanData();loadData();}@Overridepublic void onLoadMore() {// TODO Auto-generated method stubif(ifloadsearch){Toast.makeText(CustomActivity.this,"没有新数据了!", 1).show();mHandler.postDelayed(new Runnable() {@Overridepublic void run() {onLoad();}}, 500);return;}//customdata.setCurrentpage(String.valueOf(page));if(customdata.getCurrentpage().equals(customdata.getPageall())){Toast.makeText(CustomActivity.this,"没有新数据了!", 1).show();mHandler.postDelayed(new Runnable() {@Overridepublic void run() {onLoad();}}, 500);return;}else{page=page+1;customdata.setCurrentpage(String.valueOf(page));loadData();adapter.notifyDataSetChanged();}}private void onLoad() {listview.stopRefresh();listview.stopLoadMore();listview.setRefreshTime("刚刚");}} 好了 咱们接着写,在onRefresh()中写入cleanData()清除数据;loadData()重新加载网路数据;在loadData中获取后的数据如下

孤独是一种无奈的选择,孤独是因为没有找到合适的同行者,

将修正好的Xlistview镶嵌进项目中

相关文章:

你感兴趣的文章:

标签云: