PullToRefresh-下拉动画效果
集成PullToRefresh组件
PullToRefresh托管在Github上的开源组件,用于实现下拉刷新功能
https://github.com/chrisbanes/Android-PullToRefresh
在AS中集成PullToRefresh组件:
1.创建项目
2.导入已下载的PullToRefresh中的library
3.修改错误
若报错,把manifest.xml 里的一段代码删除即可
<uses-sdk android:minSdkVersion="15" />
Androidstudio3.0 以后 不能在manifest.xml 设置这些 必须在 gradle里设置
加载完成后导入依赖
使用PullToRefresh实现列表刷新
添加xmlns:ptr="http://schemas.android.com/apk/res-auto"
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_to_refresh_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
ptr:ptrDrawable="@drawable/default_ptr_flip"
ptr:ptrAnimationStyle="flip"
ptr:ptrHeaderBackground="@android:color/transparent"
ptr:ptrHeaderTextColor="#919191"
>
</com.handmark.pulltorefresh.library.PullToRefreshListView>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_to_refresh_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
ptr:ptrDrawable="@drawable/default_ptr_flip"
ptr:ptrAnimationStyle="flip"
ptr:ptrHeaderBackground="@android:color/transparent"
ptr:ptrHeaderTextColor="#919191">
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/title"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/singer"
android:paddingLeft="16dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/title"
android:layout_toEndOf="@+id/title" />
</RelativeLayout>
Music.java
package com.example.pulltorefresh;
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;
}
}
MainActivity.java
package com.example.pulltorefresh;
import android.content.Context;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.AsyncTaskLoader;
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<Music> 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);
// lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
// @Override
// public void onRefresh(PullToRefreshBase<ListView> refreshView) {
//
// }
// });
lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
new LoadDataAsyncTask(MainActivity.this).execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
new LoadDataAsyncTask(MainActivity.this).execute();
}
});
//上下都可以刷新
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<Void,Void,String>{
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 < 15; i++) {
musics.add(new Music("歌曲-" + count, "歌手-" + count));
count++;
}
}
static class DataAdapter extends BaseAdapter{
private Context context;
private ArrayList<Music> musics;
public DataAdapter(Context context, ArrayList<Music> musics) {
this.context = 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(context).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;
}
}
}
其他支持
- ExpandableListView
- GridView
- WebView
- ScrollView
- HorizontalScrollView
- ViewPager