xlistview

xlistview的所在类,需要实现IXlistview的一个监听,主代码如下:

package example.xuexue.yuekao;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import example.xuexue.yuekao.view.XListView;


public class Main2Activity extends Activity implements XListView.IXListViewListener {

    private XListView xList;
    private MyAdapter  adapter;
    private  int count=0;
    private  List<String> list;


    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

           xList= (XListView) findViewById(R.id.listview);

           xList.setPullLoadEnable(true);
           xList.setXListViewListener(this);
        list =new ArrayList<>();
          getdata();

          SetAdapter();

    }

    Handler  handler =new Handler();

    public void SetAdapter(){
        if (adapter==null){
            adapter=new MyAdapter();
            xList.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }

    private void getdata() {

        for (int i =count;i<count+20;i++){
            list.add("这是第"+(i+1)+"条数据");
        }
    }

    @Override
    public void onRefresh() {

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
//                list.clear();
                getdata();
                SetAdapter();
                xList.stopRefresh();
            }
        }, 2000);
    }

    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                count=count+20;
                getdata();
                SetAdapter();
                xList.stopLoadMore();
            }
        }, 2000);
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView==null){
                convertView =View.inflate(Main2Activity.this, R.layout.item,null);
            }
            TextView t = (TextView) convertView.findViewById(R.id.txet);

            t.setText(list.get(position));

            return convertView;
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值