android中使用开源项目做出上拉、下拉刷新功能
16lz
2021-01-25
在github网站,下载xlistview项目,导入Eclipse转成库文件,在新建项目里,添加此库。
在布局中使用自定义xlistview控件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <me.maxwin.view.XListView android:id="@+id/xListView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" > </me.maxwin.view.XListView></RelativeLayout>
代码如下:
public class MainActivity extends Activity implements IXListViewListener{ private XListView mListview; ArrayList<String> mlist = new ArrayList<String>(); private ArrayAdapter<String> mAdapter; private Handler mhandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mhandler = new Handler(); mListview = (XListView) findViewById(R.id.xListView); mListview.setPullRefreshEnable(true);//设置下拉刷新 mListview.setXListViewListener(this);//设置监听事件,重写两个方法 mListview.setPullLoadEnable(true);//设置上拉刷新 mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mlist); for (int i = 0; i < 20; i++) { mlist.add("data" + i); } mListview.setAdapter(mAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onRefresh() { mhandler.postDelayed(new Runnable() { @Override public void run() { mlist.add(0, new Date().toString()); mAdapter.notifyDataSetChanged(); mListview.stopRefresh();//完成 } }, 2000); } @Override public void onLoadMore() { mhandler.postDelayed(new Runnable() { @Override public void run() { mlist.add(new Date().toString()); mAdapter.notifyDataSetChanged(); mListview.stopLoadMore(); } }, 2000); }}
更多相关文章
- Android(安卓)网络通信框架Volley简介(Google IO 2013)
- 安卓EditText控件底部红线如何解决?
- Android(安卓)-- 查看手机中所有进程
- 王立平--include在Android应用
- SetContentView 到底Set去哪里呢?
- Android(安卓)ImageView控件的MaxWidth、MaxHeight不起作用
- android的软盘影响背景问题
- ImageButton与Button
- android微信第三方登录怎么通过code获取openid?