Android中Xlistview的使用

来源:互联网 发布:100以内质数的和c语言 编辑:程序博客网 时间:2024/05/22 05:20

一.github地址

https://github.com/Maxwin-z/XListView-Android

二.使用 
1.首先将这三个类导入到你的工程中:

这里写图片描述

2.MainActivity的布局文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.administrator.xlistviewdemo.MainActivity">    <com.example.administrator.xlistviewdemo.view.XListView        android:id="@+id/xlistview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:cacheColorHint="#00000000"        android:scrollbars="none" /></RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3.MainActivity中:

public class MainActivity extends Activity implements XListView.IXListViewListener {    private XListView mListView;    private ArrayList<String> datas;    private Handler mHandler;    private MainAdapter mainAdapter;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mListView = (XListView) findViewById(R.id.xlistview);        mainAdapter = new MainAdapter(this);        datas = new ArrayList<>();        mHandler = new Handler();        //设置数据        geneItems();        mListView.setPullLoadEnable(true);        mListView.setAdapter(mainAdapter);        mListView.setXListViewListener(this);        //设置item的点击事件        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {                //这里的i-1,是因为头视图占了一个position                Toast.makeText(MainActivity.this, "i:" + (i - 1), Toast.LENGTH_SHORT).show();            }        });        //设置长按事件        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {            @Override            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {                Toast.makeText(MainActivity.this, "长按事件 i:" + (i-1), Toast.LENGTH_SHORT).show();                return false;            }        });        //添加头布局        View view = getLayoutInflater().inflate(R.layout.head_view, null);        mListView.addHeaderView(view);        //添加尾视图        View view2 = getLayoutInflater().inflate(R.layout.head_view, null);        mListView.addFooterView(view2);        //设置禁止上拉        //mListView.setPullLoadEnable(false);        //设置禁止下拉        //mListView.setPullRefreshEnable(false);    }    private void geneItems() {        for (int i = 0; i <= 20; i++) {            datas.add("refresh item " + i);        }        mainAdapter.setDatas(datas);    }    //下拉刷新    @Override    public void onRefresh() {        mHandler.postDelayed(new Runnable() {            @Override            public void run() {                datas.clear();                geneItems();                // mAdapter.notifyDataSetChanged();                mainAdapter = new MainAdapter(MainActivity.this);                mainAdapter.setDatas(datas);                mListView.setAdapter(mainAdapter);                onLoad();            }        }, 2000);    }    //上拉加载    @Override    public void onLoadMore() {        mHandler.postDelayed(new Runnable() {            @Override            public void run() {                geneItems();                mainAdapter.notifyDataSetChanged();                onLoad();            }        }, 2000);    }    private void onLoad() {        mListView.stopRefresh();        mListView.stopLoadMore();        //获取当前时间        Date curDate = new Date(System.currentTimeMillis());        //格式化        SimpleDateFormat formatter = new SimpleDat});        String time = formatter.format(curDate);        mListView.setRefreshTime(time);        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100

4.MainAdapter中:

public class MainAdapter extends BaseAdapter {    private Context context;    private ArrayList<String> datas;    public MainAdapter(Context context) {        this.context = context;    }    public void setDatas(ArrayList<String> datas) {        this.datas = datas;        notifyDataSetChanged();    }    @Override    public int getCount() {        return datas != null ? datas.size() : 0;    }    @Override    public Object getItem(int i) {        return null;    }    @Override    public long getItemId(int i) {        return 0;    }    @Override    public View getView(int i, View view, ViewGroup viewGroup) {        MyViewHolder myViewHolder = null;        if (view == null) {            view = LayoutInflater.from(context).inflate(R.layout.list_item, viewGroup, false);            myViewHolder = new MyViewHolder(view);            view.setTag(myViewHolder);        }else {            myViewHolder = (MyViewHolder) view.getTag();        }        //设置数据        myViewHolder.textView.setText(datas.get(i));        return view;    }    class MyViewHolder {        private TextView textView;        MyViewHolder(View itemView) {            textView = (TextView) itemView.findViewById(R.id.list_item_textview);        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

5.item的布局文件

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:id="@+id/list_item_textview"    android:textSize="16sp"    android:textColor="#000"    android:padding="5dp"></TextView>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

6.头布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <ImageView        android:layout_width="match_parent"        android:layout_height="150dp"        android:background="@drawable/headiv" /></LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

7.效果图如下 
这里写图片描述
8.下载地址

http://download.csdn.net/detail/afanbaby/9758008

原创粉丝点击