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

导入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);
}

@Override
public 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);
}
@Override
public 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("刚刚");
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值