类似SlidingMenu也是一个开源组件,集成的方式也类似,不赘述,可以参考
https://blog.csdn.net/nishigesb123/article/details/90261849
使用 PullToRefresh
可以实现对多种控件进行下拉刷新、上拉加载操作,还可以实现自定义刷新和加载部分视图等操作。
项目地址:https://github.com/chrisbanes/Android-PullToRefresh
Pull To Refresh Views for Android
This project aims to provide a reusable Pull to Refresh widget for Android. It was originally based on Johan Nilsson's library(mainly for graphics, strings and animations), but these have been replaced since.
Features
- Supports both Pulling Down from the top, and Pulling Up from the bottom (or even both).
- Animated Scrolling for all devices.
- Over Scroll supports for devices on Android v2.3+.
- Currently works with:
- ListView
- ExpandableListView
- GridView
- WebView
- ScrollView
- HorizontalScrollView
- ViewPager
- Integrated End of List Listener for use of detecting when the user has scrolled to the bottom.
- Maven Support.
- Indicators to show the user when a Pull-to-Refresh is available.
- Support for ListFragment!
- Lots of Customisation options!
Repository at https://github.com/chrisbanes/Android-PullToRefresh.
首先有一条添加“命名空间”的语句需要设置一下,图示处找个位置加进去就好~
xmlns:ptr="http://schemas.android.com/apk/res-auto"
然后是组件
一般情况下,下拉刷新的操作肯定是对列表而言的,我们这里也不例外。所以既然是对列表而言,所以我们还得先准备一个列表。
首先是列表数据对象
package com.example.a5_16pulltorefresh;
public class Music {
//标题
private String title;
//歌手
private String singer;
public Music() {
}
public Music(String title, String singer) {
this.title = title;
this.singer = singer;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
}
列表项布局文件
package com.example.a5_16pulltorefresh;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private PullToRefreshListView lv;
//音乐列表
private ArrayList musics = new ArrayList<>();
private DataAdapter dataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = findViewById(R.id.pull_to_refresh_listView);
//不带2的只用实现一个刷新方法
// lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener() {
// @Override
// public void onRefresh(PullToRefreshBase refreshView) {
//
// }
// });
//带2的是上下刷新可以单独实现方法
lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
new LoadDataAsyncTask(MainActivity.this).execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase refreshView) {
new LoadDataAsyncTask(MainActivity.this).execute();
}
});
//设置模式——BOTH表示上下都可以
lv.setMode(PullToRefreshBase.Mode.BOTH);
//设置刷新时显示的文本
//下拉
ILoadingLayout startLayout = lv.getLoadingLayoutProxy(true, false);
startLayout.setPullLabel("正在下拉刷新...");
startLayout.setRefreshingLabel("正在玩命加载中...");
startLayout.setReleaseLabel("放开刷新...");
//上拉
ILoadingLayout endLayout = lv.getLoadingLayoutProxy(false, true);
endLayout.setPullLabel("正在上拉刷新...");
endLayout.setRefreshingLabel("正在玩命加载中...");
endLayout.setReleaseLabel("放开刷新...");
//调用一次
loadData();
dataAdapter = new DataAdapter(this, musics);
lv.setAdapter(dataAdapter);
}
//异步类
static class LoadDataAsyncTask extends AsyncTask {
private MainActivity mainActivity;
//静态方法,需要传...
public LoadDataAsyncTask(MainActivity mainActivity) {
this.mainActivity = mainActivity;
}
@Override
protected String doInBackground(Void... voids) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mainActivity.loadData();
return "success";
}
//完成数据的话...
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if ("success".equals(s)) {
mainActivity.dataAdapter.notifyDataSetChanged();//通知数据集发生改变
mainActivity.lv.onRefreshComplete();//表示刷新完成
}
}
}
//模拟一组数据
private int count = 1;
private void loadData() {
for (int i = 0; i < 10; i++) {
musics.add(new Music("歌曲-" + count, "歌手-" + count));
count++;
}
}
static class DataAdapter extends BaseAdapter {
private Context ctx;
private ArrayList musics;
//构造器传上下文
public DataAdapter(Context context, ArrayList musics) {
this.ctx = context;
this.musics = musics;
}
@Override
public int getCount() {
return musics.size();
}
@Override
public Object getItem(int position) {
return musics.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
//码一下
if (convertView == null) {
convertView = LayoutInflater.from(ctx).inflate(R.layout.list_item, null);
vh = new ViewHolder();
vh.tv_title = convertView.findViewById(R.id.title);
vh.tv_singer = convertView.findViewById(R.id.singer);
convertView.setTag(vh);
}
vh = (ViewHolder) convertView.getTag();
Music m = musics.get(position);
vh.tv_title.setText(m.getTitle());
vh.tv_singer.setText(m.getSinger());
return convertView;
}
static class ViewHolder {
TextView tv_title;
TextView tv_singer;
}
}
}