标签:
下拉刷新,使用的是Android-PullToRefresh,Github--https://github.com/chrisbanes/Android-PullToRefresh
PullToRefresh的简单使用办法
1.通过GitHub把相关的lib下载下来,导入到ADT
下载下来是这些文件
然后导入到ADT,导入以后是4个工程
LauncherActivity
PullToRefreshListFragment
PullToRefreshViewPager
library
假如导入以后,工程上有红色错误的话,点击工程右键-》Properties-》Android,在Library中,把错误的library remove掉,然后点击add按钮,重新添加就可以了。
2.新建一个工程,假如新建的工程的Console中出现support.v4包版本不一致的提示信息,把新建工程中的V4都复制和替换下载下来的lib的v4
3.PullToRefresh的使用
在XML文件中
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 android:orientation="vertical" 10 tools:context="com.lijingbo.learnpulltorefruse.MainActivity" > 11 12 <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/lv" 13 android:layout_width="match_parent" 14 android:layout_height="match_parent"/> 15 16 </LinearLayout>
在JAVA文件中
1 public class MainActivity extends Activity { 2 private ArrayAdapter<String> adapter; 3 private PullToRefreshListView lV; 4 private List<String> data; 5 private Handler handler=new Handler(){ 6 @Override 7 public void handleMessage(Message msg) { 8 // TODO Auto-generated method stub 9 switch (msg.what) { 10 case 1: 11 data.add("天津"); 12 data.add("西安"); 13 lV.onRefreshComplete(); 14 break; 15 default: 16 break; 17 } 18 } 19 }; 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 26 data = new ArrayList<String>(); 27 data.add("南京"); 28 data.add("上海"); 29 data.add("承德"); 30 31 lV=(PullToRefreshListView) findViewById(R.id.lv); 32 adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, data); 33 lV.setAdapter(adapter); 34 lV.setOnRefreshListener(new OnRefreshListener<ListView>() { 35 36 @Override 37 public void onRefresh(PullToRefreshBase<ListView> refreshView) { 38 // TODO Auto-generated method stub 39 new Thread(new Runnable() { 40 41 @Override 42 public void run() { 43 // TODO Auto-generated method stub 44 try { 45 Thread.sleep(3000); 46 } catch (InterruptedException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } 50 Message msg = new Message(); 51 msg.what = 1; 52 handler.sendMessage(msg); 53 } 54 }).start(); 55 } 56 }); 57 } 58 59 }
PullToRefreshListView主要使用:
1.通过findviewbyid()方法找到下拉刷新控件
2.设置adapter,然后使用setAdapter()方法,把适配器和数据列表绑定
3.使用OnRefreshListener<ListView>()监听器,重写该监听器的onRefresh()方法,
4.使用setOnRefreshListener()方法设置监听器。
5.onRefresh()方法中通过Handler实现异步处理。
Android开发之控件-Android-PullToRefresh
标签:
原文地址:http://www.cnblogs.com/liyiran/p/4647921.html