第一次写博客有点紧张,如果有错的话,欢迎您纠正!
大家都知道目前App都有一个常见的功能就是上拉加载、下拉刷新,其实就是分页。
但是今天要用第三方JAR“Android-PullToRefresh-master”,
支持一下控件:
ListView
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager
来、走起!(或者直接下载Demo和jar包就可以了)
jar包和Demo:可以去下载免积分
第一步: 建立项目:“DemoFlush”,导入jar包(Android-PullToRefresh-master.rar包解压后 得到一个文件夹,然后以导入项目的方式,导入到Eclipse中)。
注意:导入之后有可能报错哦,解决方案:在刚导入到项目(Android-PullToRefresh-master)中右键“Properties”选择“Android”然后看到“Project Build Taget”勾选你的SDK(看懂没! 相信你可以搞定的!)
第二步: 选中项目(Demo上面写了就是刚建立的)右键“Properties” 选择“Android”—>Libray–>add
好在这一步Demo项目 已经完成了一半了!
第三步: Demo那个项目中 我是在activity_main.xml 写了一下代码(其实就是自定义控件,但是这个控件是来自刚才的jar包的 所以用完整类名),记得命名空间!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<!-- xmlns:ptr = "http://schemas.android.com/apk/res-auto" 为我们要使用PullToRefresh 里面一些属性需要引的命名空间 -->
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true"
ptr:ptrMode="both" />
</LinearLayout>
第四步: findviewbyid 找到myPull_refresh_list 然后实现监听接口 。运行一下项目先来让你爽一把,满足吗? 继续向下看!
注意运行项目可能报错哦,将“Android-PullToRefresh-master ”下的 /bin/res文件夹删除自动重新生成 就可以解决了!
package com.vip.flushdemo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
public class MainActivity extends Activity implements
OnRefreshListener2<ListView> {
private PullToRefreshListView listView1;
private List<String> lists = new ArrayList<String>();// 模拟数据
private MyArrayAdapter myArrayAdapter;// 数据Adapter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView1 = (PullToRefreshListView) findViewById(R.id.myPull_refresh_list);
// 绑定Adapter
myArrayAdapter = new MyArrayAdapter(this,
android.R.layout.simple_list_item_1, getData());
listView1.setAdapter(myArrayAdapter);
listView1.setOnRefreshListener(this);// 记得绑定一下 监听类
}
// 模拟数据库查询
int index = 1;
private List<String> getData() {
for (int i = 0; i < 10; i++) {
lists.add("第" + index + "条");
index++;
}
return lists;
}
// Adapter 你也懂的
private class MyArrayAdapter extends ArrayAdapter<String> {
public MyArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
}
}
// 异步方式模拟请求数据
private class GetDataTask extends AsyncTask<Integer, Integer, Integer> {
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
myArrayAdapter.notifyDataSetChanged();// 刷新Adapter
listView1.onRefreshComplete();// 告诉它 我们已经在后台数据请求完毕
Toast.makeText(MainActivity.this, "完成了", Toast.LENGTH_SHORT).show();
}
@Override
protected Integer doInBackground(Integer... params) {
getData();// 继续刷数据
try {
Thread.sleep(2000);//暂停一下 只是为了效果更加明显
} catch (Exception e) {
e.printStackTrace();
}
publishProgress(0);//通知前台线程
return 0;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
Toast.makeText(this, "下拉", Toast.LENGTH_SHORT).show();
index = 0;// 刷新数据
lists.clear();
new GetDataTask().execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
Toast.makeText(this, "上拉", Toast.LENGTH_SHORT).show();
new GetDataTask().execute();
}
}
在此恭喜你学会了,有空要多学习一下 没有jar包要怎么写哦,
举一反三:
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager
实现方式差不多!
最后:谢谢您的阅读,您的支持就是我最大的动力!