下拉刷新很多地方都用到了,新浪微博,微信,百度新闻
这里我们使用一个开源的库叫:PullToRefresh
开源地址:https://github.com/chenyoca/pull-to-refresh
下载地址:https://github.com/chenyoca/pull-to-refresh/archive/master.zip
解压代码之后通过ecplise导入到项目里面
导入之后可能会出现库路径引用错误
在项目右键,依次对库进行修正
运行主Activity
这时就可以看到效果了!接下来我们自己创建一个项目来使用这个控件
新建一个项目UsingPullToRefresh
创建之后要对库进行引用,这有点像C#工程一样,要对程序集进行引用(3个库都要引用)
引用完之后会报出一个jar版本不同的错误
所以我们要让库和我们的工程的版本一致
拷贝libs下的android-support-v4.jar
分别复制到刚引用的3个库中替换libs下的android-support-v4.jar
修改一下MianActivity.java和activity_main.xml的代码
package com.example.usingpulltorefresh;import java.util.ArrayList;
import java.util.List;import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.os.Build;public class MainActivity extends Activity {
private PullToRefreshListView lv;
private ArrayAdapter<String> adapter;
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查找到控件
lv = (PullToRefreshListView) findViewById(R.id.lv);
list= new ArrayList<String>();
list.add("香蕉");
list.add("苹果");adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
lv.setAdapter(adapter);lv.setOnRefreshListener(new OnRefreshListener<ListView>(){
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
new AsyncTask<Void,Void,Void>(){
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}protected void onPostExecute(Void result){
adapter.addAll("西瓜","橙子","火龙果");
lv.onRefreshComplete();
}}.execute();
}
});
}@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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
MainActivity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.usingpulltorefresh.MainActivity"
tools:ignore="MergeRootFrame" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>
activity_main
OK,可以运行了!
直接下载源码:http://pan.baidu.com/s/1mgKBkrI
.Net 转战 Android 4.4 日常笔记(10)--PullToRefresh下拉刷新使用,布布扣,bubuko.com