第三方library--ListView下拉刷新,上拉加载的使用
功能:实现ListView上拉刷新,下拉加载的效果,该工程使用到的library为:(我云盘也有)
https://github.com/chrisbanes/Android-PullToRefresh
这个库里面除了带上拉刷新,下拉加载的listView外,还有网格视图GridView等等等等,如下:
这里以ListView为例,代码如下: 这个库已经把显示的文字国际化了
import android.os.AsyncTask; import android.os.Bundle; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import com.handmark.pulltorefresh.library.ILoadingLayout; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; /** * 2016-9-13 * 带下拉刷新上拉加载的ListView */ public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { /** * 库中带有下拉刷新上拉加载的ListView的布局控件(把它当成ListView使用就行) */ private PullToRefreshListView pullToRefreshListView; /** * 上面控件中加载的整个布局的对象 */ private ILoadingLayout loadingLayoutProxy; /** * 格式化时间的 */ private SimpleDateFormat simpleDateFormat; /** * 作为ListView的数据 */ private ArrayList<String> data; /** * 作为ListView的适配器 */ private ArrayAdapter<String> adapter; /** * 当前为下拉还是上拉的标志,1代表下拉刷新,2代表上拉加载 */ private static final int PULL_DOWN_REFESH = 1; private static final int PULL_UP_REFESH = 2; /** * 加载哪页数据的索引 */ private int index = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //设置格式化时间的样式 simpleDateFormat = new SimpleDateFormat("yyy-MM-dd hh:mm:ss"); //找到带下拉刷新上拉加载的ListView控件 pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listView); //获取这个布局中的ListView,(库中自带的) ListView listView = pullToRefreshListView.getRefreshableView(); //获得整个加载的布局的对象 loadingLayoutProxy = pullToRefreshListView.getLoadingLayoutProxy(); //设置下拉刷新时旋转的图标 loadingLayoutProxy.setLoadingDrawable(getResources().getDrawable(R.mipmap.ic_launcher)); //修改下拉刷新显示的文字 loadingLayoutProxy.setPullLabel("下拉刷新"); //修改释放刷新 loadingLayoutProxy.setReleaseLabel("释放刷新"); //正在刷新 loadingLayoutProxy.setRefreshingLabel("玩命加载中..."); /*设置模式: PULL_FROM_START:只带下拉刷新 * PULL_FROM_END:只带上拉加载 * BOTH:带下拉刷新和上拉加载*/ pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH); //创建适配器,这里使用简单的arrayAdapter,你也可以使用其他的继承BaseAdapter data = new ArrayList<String>(); new MyAsynsTask().execute(0,PULL_DOWN_REFESH); adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data); pullToRefreshListView.setAdapter(adapter);//设置适配器 //设置单击事件,内部其实就是为这个布局中的listView设置了单击事件 pullToRefreshListView.setOnItemClickListener(this); //设置刷新监听(当设置为只有下拉刷新或者是上拉加载的模式的时候就使用这种监听) /* pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { Log.i("tag","正在刷新,需要获取数据,然后设置为完成刷新"); } });*/ //当设置的模式为带上拉和下拉的时候需要设置为这种监听模式 pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { //下拉刷新的时候调用 new MyAsynsTask().execute(0,PULL_DOWN_REFESH);//下载数据第0页数据,下拉模式 } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { //上拉加载的时候调用 index = index + 1;//下次加载就为下一页 new MyAsynsTask().execute(index,PULL_UP_REFESH);//下载第index页数据,上拉加载模式 } }); } /** * ListView的单击事件 */ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String str = (String) parent.getItemAtPosition(position); Toast.makeText(this,"点击了"+str,Toast.LENGTH_SHORT).show(); } /** * 模拟从网络上获取数据 * Integer接收两个参数,参数1 代表请求哪页数据,参数二代表刷新的方式(下拉还是上拉) */ class MyAsynsTask extends AsyncTask<Integer,Void,ArrayList<String>>{ /** * 接收当前刷新的方式(上拉还是下拉) * 如果是下拉刷新,就把加载的所有数据先清空,然后重新请求第一页数据 * 如果是上拉加载,就在原来数据的基础上增加一页数据 */ private int refreshMode; @Override protected ArrayList<String> doInBackground(Integer... params) { int index = params[0];//第几页数据 refreshMode = params[1];//刷新模式 Log.i("tag","index="+index+",refreshMode"+refreshMode); ArrayList<String> list = new ArrayList<String>(); for (int i = index * 20; i < (index+1)*20; i++) { list.add("第 "+i+" 条数据"); } SystemClock.sleep(2000);//睡两秒 return list; } @Override protected void onPostExecute(ArrayList<String> strings) { if(refreshMode == PULL_DOWN_REFESH){//下拉刷新 data.removeAll(data);//删除所有数据 data.addAll(strings);//添加数据 //设置下一次刷新显示的时间,(下拉的时候显示上一次刷新的时间) loadingLayoutProxy.setLastUpdatedLabel(simpleDateFormat.format(new Date())); }else if(refreshMode == PULL_UP_REFESH){//上拉加载 data.addAll(strings);//添加数据 } adapter.notifyDataSetChanged();//通知刷新 //通知组件刷新完成,可以隐藏上拉头或者下拉头了,不然会一直处于刷新状态 pullToRefreshListView.onRefreshComplete();//注意点,关键的地方 } } }
说明:当为pullToRefreshListView设置刷新监听或者是单击事件的时候,其实它内部就为listView做出了相应的设置,所以可以直接当listview使用
布局文件如下:
<?xml version="1.0" encoding="utf-8"?> <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"> <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pull_to_refresh_listView" android:layout_width="match_parent" android:layout_height="match_parent"> </com.handmark.pulltorefresh.library.PullToRefreshListView> </RelativeLayout>
效果图: