xlistview实现上拉加载,下拉刷新

来源:互联网 发布:9090端口 编辑:程序博客网 时间:2024/06/01 10:47

导入xlistview包,这里我就不展示了

子条目布局

<ImageView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/img"    />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/text"        android:layout_marginTop="50px"        />

首先,写好我们的适配器

public class myadapter extends BaseAdapter {    Context context;    ArrayList<Goods> list;    public myadapter(Context context, ArrayList<Goods> list) {        this.context = context;        this.list = list;    }    @Override    public int getCount() {        return list.size();    }    @Override    public Goods getItem(int i) {        return list.get(i);    }    @Override    public long getItemId(int i) {        return i;    }    @Override    public View getView(int i, View view, ViewGroup viewGroup) {        ViewHoler holer;        if (view==null){            view= LayoutInflater.from(context).inflate(R.layout.itemmobel,viewGroup,false);            holer=new ViewHoler();            holer.img=(ImageView) view.findViewById(R.id.img);            holer.text=(TextView)view.findViewById(R.id.text);            view.setTag(holer);        }else {            holer=(ViewHoler)view.getTag();        }        Goods g=getItem(i);        holer.text.setText(g.title);        holer.img.setImageResource(g.img);        return view;    }    class ViewHoler{        ImageView img;        TextView text;    }然后就是我们的数据配置情况
 mlistview=(XListView)findViewById(R.id.xlistview);    mlistview.setPullLoadEnable(true);    list=new ArrayList<Goods>();    list.add(new Goods("我的第一个个产品",R.mipmap.a11));    list.add(new Goods("我的第二个个产品",R.mipmap.a12));    list.add(new Goods("我的第三个个产品",R.mipmap.a13));    list.add(new Goods("我的第四个个产品",R.mipmap.a14));    list.add(new Goods("我的第五个个产品",R.mipmap.a15));     myadapter = new myadapter(MainActivity.this, list);     mlistview.setAdapter(myadapter);     mlistview.setXListViewListener(this);}@Overridepublic void onRefresh(){    handler.postDelayed(new Runnable() {        @Override        public void run() {            //获取当前时间            Date curDate = new Date(System.currentTimeMillis());            //格式化            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHH:mm:ss");            String time = formatter.format(curDate);            mlistview.setRefreshTime(time);            myadapter = new myadapter(MainActivity.this, list);            mlistview.setAdapter(myadapter);            onLoad();        }    },2000);}@Overridepublic void onLoadMore() {    handler.postDelayed(new Runnable() {        @Override        public void run() {            list.add(new Goods("我的第一个个产品",R.mipmap.a11));            list.add(new Goods("我的第二个个产品",R.mipmap.a12));            list.add(new Goods("我的第三个个产品",R.mipmap.a13));            list.add(new Goods("我的第四个个产品",R.mipmap.a14));            list.add(new Goods("我的第五个个产品",R.mipmap.a15));            myadapter.notifyDataSetChanged();            onLoad();        }    },2000);}public  void onLoad(){    mlistview.stopLoadMore();    mlistview.stopRefresh();    mlistview.setRefreshTime("刚刚");}
 

原创粉丝点击