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; } } }