Android下拉刷新库使用
本文主要介绍第三方下拉刷新库PullToRefresh使用,关于这个下拉刷新库的使用介绍文章很多,本文主要针对初学者并Android开发工具eclipse做一些简单使用介绍。
相信有很多同学下载这个资源很跑不起来,这是因为你要到依赖库包括PullToRefresh和Android兼容v7库这两个看才能正常使用,至于v7库后期我会说明作用是什么。
依赖库导入
下图是导入的库:
有同学一定不会导入,好吧我也简单介绍一下,图文教程我就直接上了,
首先邮件选择import,如图:
接下来选择Android点击Existing…..这个选项
然后点击 Browse…找到依赖库的位置导入即可
记住这里依赖库位置和项目位置要保持一致不然会报错,导入完成可以看到四个文件,下拉刷新库中包括一个demo文件,下图第一个红框就是demo,可以直接运行使用
使用下拉刷新库
一共两个地方,第一个是xml文件,
<LinearLayout 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="com.example.pulltoscollview.MainActivity" >
PullToRefreshListView这个可以换成PullToRefreshGridView,PullToRefreshHorizontalScrollView等都可以
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>
下面是java文件,
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.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
//PullToRefreshListView 这个和xml文件中<com.handmark.pulltorefresh.library.PullToRefreshListView中的PullToRefreshListView保持一直,下面有PullToRefreshListView就要和xml文件保持一致
PullToRefreshListView lv;
ArrayAdapter<String>adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(PullToRefreshListView) findViewById(R.id.listView);
List<String>arr=new ArrayList<String>();
arr.add("sas");
arr.add("ds");
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr);
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(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@SuppressLint("NewApi")
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
adapter.addAll("dsd","dsd");
lv.onRefreshComplete();
};
}.execute();
}
});
}
}
最后加上demo地址Android下拉刷新库使用