SwipeToLoadLayout轻松定制自己的刷新效果,且子布局不在局限只是可以滑动的布局(如ListView, ScrollVie,RecyclerView)。
Android Studio中使用
Gradle中添加 compile 'com.github.Aspsine:SwipeToLoadLayout:1.0.4'
GitHub地址:https://github.com/Aspsine/SwipeToLoadLayout
自定义刷新、加载的效果
SwipeToLoadLayout提供了一套接口,自定义的View只需要实现相应的接口就好了,头实现SwipeRefreshHeaderLayout,尾实现SwipeLoadMoreFooterLayout即可。
public class RefreshHeaderView extends SwipeRefreshHeaderLayout {
private ImageView ivArrow;
private TextView tvRefresh;
private ProgressBar progressBar;
private int mHeaderHeight;
private Animation rotateUp;
private Animation rotateDown;
private boolean rotated = false;
public RefreshHeaderView(Context context) {
this(context, null);
}
public RefreshHeaderView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mHeaderHeight = getResources().getDimensionPixelOffset(R.dimen.refresh_header_height_60);
rotateUp = AnimationUtils.loadAnimation(context, R.anim.rotate_up);
rotateDown = AnimationUtils.loadAnimation(context, R.anim.rotate_down);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
tvRefresh = (TextView) findViewById(R.id.tvRefresh);
ivArrow = (ImageView) findViewById(R.id.ivArrow);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
}
@Override
public void onRefresh() {
ivArrow.clearAnimation();
ivArrow.setVisibility(GONE);
progressBar.setVisibility(VISIBLE);
tvRefresh.setText("正在刷新...");
}
@Override
public void onPrepare() {
Log.d("WeiboRefreshHeaderView", "onPrepare()");
}
@Override
public void onMove(int y, boolean isComplete, boolean automatic) {
if (!isComplete) {
ivArrow.setVisibility(VISIBLE);
progressBar.setVisibility(GONE);
if (y > mHeaderHeight) {
tvRefresh.setText("释放刷新");
if (!rotated) {
ivArrow.clearAnimation();
ivArrow.startAnimation(rotateUp);
rotated = true;
}
} else if (y < mHeaderHeight) {
if (rotated) {
ivArrow.clearAnimation();
ivArrow.startAnimation(rotateDown);
rotated = false;
}
tvRefresh.setText("下拉刷新");
}
}
}
@Override
public void onRelease() {
Log.d("WeiboRefreshHeaderView", "onRelease()");
}
@Override
public void onComplete() {
rotated = false;
ivArrow.clearAnimation();
ivArrow.setVisibility(GONE);
progressBar.setVisibility(GONE);
tvRefresh.setText("刷新完成");
}
@Override
public void onReset() {
rotated = false;
ivArrow.clearAnimation();
ivArrow.setVisibility(GONE);
progressBar.setVisibility(GONE);
tvRefresh.setText("下拉刷新");
}
}
public class LoadMoreFooterView extends SwipeLoadMoreFooterLayout{
private TextView tvLoadMore;
private ProgressBar progressBar;
private int mHeaderHeight;
public LoadMoreFooterView(Context context) {
super(context);
}
public LoadMoreFooterView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LoadMoreFooterView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mHeaderHeight = getResources().getDimensionPixelOffset(R.dimen.refresh_header_height_60);
tvLoadMore = (TextView) findViewById(R.id.tvLoadMore);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
}
@Override
public void onLoadMore() {
progressBar.setVisibility(VISIBLE);
tvLoadMore.setText("正在加载...");
}
@Override
public void onPrepare() {
Log.d("LoadMoreFooterView", "onPrepare()");
}
@Override
public void onMove(int y, boolean isComplete, boolean automatic) {
if (!isComplete) {
if (y <= mHeaderHeight) {
progressBar.setVisibility(VISIBLE);
tvLoadMore.setText("加载更多");
} else if(y > mHeaderHeight) {
progressBar.setVisibility(GONE);
tvLoadMore.setText("上拉加载");
}
}
}
@Override
public void onRelease() {
super.onRelease();
}
//加载完成
@Override
public void onComplete() {
progressBar.setVisibility(GONE);
tvLoadMore.setText("加载完成");
}
@Override
public void onReset() {
Log.d("LoadMoreFooterView", "onReset()");
}
}
header和footer的xml
<?xml version="1.0" encoding="utf-8"?>
<com.help.view.RefreshHeaderView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/refresh_header_height_60"
android:layout_gravity="center">
<TextView
android:id="@+id/tvRefresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:gravity="center"
android:text="下拉刷新"
android:textColor="#FFFFFF"/>
<ImageView
android:id="@+id/ivArrow"
android:layout_width="20dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/tvRefresh"
android:scaleType="centerInside"
android:src="@drawable/icon_weibo_pull_arrow_white"/>
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleSmallInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/tvRefresh"/>
</RelativeLayout>
</com.help.view.RefreshHeaderView>
<?xml version="1.0" encoding="utf-8"?>
<com.help.view.LoadMoreFooterView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/refresh_header_height_60"
android:layout_gravity="center">
<TextView
android:id="@+id/tvLoadMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="10dp"
android:text="加载更多"
android:textColor="#FFFFFF"/>
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleSmallInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/tvLoadMore"/>
</RelativeLayout>
</com.help.view.LoadMoreFooterView>
Activity的xml
注意:刷新目标、头尾的id不能更改只能是swipeToLoadLayout、swipe_refresh_header、swipe_load_more_footer
<?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"
android:background="@color/colorPrimary">
<com.aspsine.swipetoloadlayout.SwipeToLoadLayout
android:id="@+id/swipeToLoadLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@id/swipe_refresh_header"
layout="@layout/layout_weibo_header"/>
<TextView
android:id="@id/swipe_target"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:text="下拉刷新"
android:textSize="24sp"/>
<include
android:id="@id/swipe_load_more_footer"
layout="@layout/layout_load_more_footer"/>
</com.aspsine.swipetoloadlayout.SwipeToLoadLayout>
</RelativeLayout>
代码中调用
public class RefreshActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener {
private SwipeToLoadLayout swipeToLoadLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weibo_refresh);
swipeToLoadLayout = (SwipeToLoadLayout) findViewById(R.id.swipeToLoadLayout);
swipeToLoadLayout.setOnRefreshListener(this);
swipeToLoadLayout.setOnLoadMoreListener(this);
swipeToLoadLayout.postDelayed(new Runnable() {
@Override
public void run() {
swipeToLoadLayout.setRefreshing(true);
}
}, 100);
}
//刷新
@Override
public void onRefresh() {
swipeToLoadLayout.postDelayed(new Runnable() {
@Override
public void run() {
swipeToLoadLayout.setRefreshing(false);
}
}, 3000);
}
//加载更多
@Override
public void onLoadMore() {
swipeToLoadLayout.postDelayed(new Runnable() {
@Override
public void run() {
swipeToLoadLayout.setLoadingMore(false);
}
}, 3000);
}
}
到此就结束,想要更多刷新案例可查看http://www.apkbus.com/thread-275506-1-1.html,感觉您的阅读,期望能给您带来帮助!
网友评论