android XListView使用详解

来源:互联网 发布:mac npm安装gulp 编辑:程序博客网 时间:2024/06/03 20:12
在github网站,下载xlistview项目,导入Eclipse转成库文件,在新建项目里,添加此库。
\
 
在布局中使用自定义xlistview控件

view sourceprint?
01.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02.xmlns:tools="http://schemas.android.com/tools"
03.android:layout_width="match_parent"
04.android:layout_height="match_parent"
05.tools:context=".MainActivity" >
06.<me.maxwin.view.XListView
07.android:id="@+id/xListView"
08.android:layout_width="fill_parent"
09.android:layout_height="fill_parent"
10.android:cacheColorHint="#00000000" >
11.</me.maxwin.view.XListView>
12.</RelativeLayout>

代码如下:

 

view sourceprint?
01.public class MainActivity extends Activity implements IXListViewListener
02.{
03.private XListView mListview;
04.ArrayList<String> mlist = new ArrayList<String>();
05.private ArrayAdapter<String> mAdapter;
06.private Handler mhandler;
07.@Override
08.protected void onCreate(Bundle savedInstanceState)
09.{
10.super.onCreate(savedInstanceState);
11.setContentView(R.layout.activity_main);
12.mhandler = new Handler();
13.mListview = (XListView) findViewById(R.id.xListView);
14.mListview.setPullRefreshEnable(true);//设置下拉刷新
15.mListview.setXListViewListener(this);//设置监听事件,重写两个方法
16.mListview.setPullLoadEnable(true);//设置上拉刷新
17.mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mlist);
18.for (int i = 0; i < 20; i++)
19.{
20.mlist.add("data" + i);
21.}
22.mListview.setAdapter(mAdapter);
23.}
24.@Override
25.public boolean onCreateOptionsMenu(Menu menu)
26.{
27.// Inflate the menu; this adds items to the action bar if it is present.
28.getMenuInflater().inflate(R.menu.main, menu);
29.return true;
30.}
31.@Override
32.public void onRefresh()
33.{
34.mhandler.postDelayed(new Runnable()
35.{
36. 
37.@Override
38.public void run()
39.{
40.mlist.add(0, new Date().toString());
41.mAdapter.notifyDataSetChanged();
42.mListview.stopRefresh();//完成
43. 
44.}
45.}, 2000);
46.}
47.@Override
48.public void onLoadMore()
49.{
50.mhandler.postDelayed(new Runnable()
51.{
52. 
53.@Override
54.public void run()
55.{
56.mlist.add(new Date().toString());
57.mAdapter.notifyDataSetChanged();
58.mListview.stopLoadMore();
59.}
60.}, 2000);
61.}
62.}
0 0
原创粉丝点击