安卓学习-PullToRefresh-下拉动画效果

第三方开源组件 更新数据时使用

安卓学习-PullToRefresh-下拉动画效果_第1张图片

安卓学习-PullToRefresh-下拉动画效果_第2张图片

安卓学习-PullToRefresh-下拉动画效果_第3张图片

安卓学习-PullToRefresh-下拉动画效果_第4张图片

安卓学习-PullToRefresh-下拉动画效果_第5张图片

 

安卓学习-PullToRefresh-下拉动画效果_第6张图片安卓学习-PullToRefresh-下拉动画效果_第7张图片

 

安卓学习-PullToRefresh-下拉动画效果_第8张图片

Music.java

package com.xiaofei.app.pulltorefresh;

/**
* Created by Goddess on 2016/4/24.
*/
public class Music {
private String title;
private String 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;
}

public Music() {
}

public Music(String title, String singer) {
this.title = title;
this.singer = singer;
}
}

MainActivity.java

package com.xiaofei.app.pulltorefresh;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 = (PullToRefreshListView) 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 activity;

public LoadDataAsyncTask(MainActivity mainActivity) {
this.activity = mainActivity;
}

@Override
protected String doInBackground(Void... params) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
activity.loadData();
return "success";
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if ("success".equals(s)) {
activity.dataAdapter.notifyDataSetChanged();//通知数据集发生改变
activity.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 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 = (TextView) convertView.findViewById(R.id.title);
vh.tv_singer = (TextView) 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;
}
}
}

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>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.xiaofei.app.pulltorefresh.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>

你可能感兴趣的:(安卓学习-PullToRefresh-下拉动画效果)