SwipeToLoadLayout 上下拉控件 很不错
她:你想做什么?
我:我想环游世界
然后,我围着她转了一圈。
效果图:
开发工具:android studio 2.2.3
测试机型:夜神模拟器
GitHub地址:https://github.com/Aspsine/SwipeToLoadLayout
如何在我们工程中使用:
1、添加依赖:
maven { url "https://jitpack.io" }
compile 'com.github.Aspsine:SwipeToLoadLayout:1.0.4'
RefreshHeaderView(自己创建):
package com.wentong.administrator.swipetoloaddemo;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import com.aspsine.swipetoloadlayout.SwipeRefreshTrigger;
import com.aspsine.swipetoloadlayout.SwipeTrigger;
/**
* Created by Administrator on 2017/3/17.
*/
public class RefreshHeaderView extends TextView implements SwipeRefreshTrigger,SwipeTrigger {
public RefreshHeaderView(Context context) {
super(context);
}
public RefreshHeaderView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onRefresh() {
setText("正在刷新");
}
@Override
public void onPrepare() {
setText("");
}
@Override
public void onMove(int i, boolean b, boolean b1) {
if (!b) {
if (i >= getHeight()) {
setText("松开后刷新");
} else {
setText("下拉刷新");
}
} else {
setText("刷新完毕");
}
}
@Override
public void onRelease() {
}
@Override
public void onComplete() {
setText("刷新完成");
}
@Override
public void onReset() {
setText("");
}
}
LoadMoreFooterView:
package com.wentong.administrator.swipetoloaddemo;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import com.aspsine.swipetoloadlayout.SwipeLoadMoreTrigger;
import com.aspsine.swipetoloadlayout.SwipeTrigger;
/**
* Created by Administrator on 2017/3/17.
*/
public class LoadMoreFooterView extends TextView implements SwipeTrigger, SwipeLoadMoreTrigger {
public LoadMoreFooterView(Context context) {
super(context);
}
public LoadMoreFooterView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onLoadMore() {
setText("正在加载");
}
@Override
public void onPrepare() {
setText("");
}
@Override
public void onMove(int i, boolean b, boolean b1) {
if (!b) {
if (i <= -getHeight()) {
setText("松开后加载");
} else {
setText("上拉加载更多");
}
} else {
setText("4");
}
}
@Override
public void onRelease() {
setText("5");
}
@Override
public void onComplete() {
setText("加载完成");
}
@Override
public void onReset() {
setText("");
}
}
Activity:
package com.wentong.administrator.swipetoloaddemo.activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.wentong.administrator.swipetoloaddemo.R;
import com.wentong.administrator.swipetoloaddemo.adapter.AdapterRecycler;
import java.util.ArrayList;
import java.util.Date;
/**
* Created by Administrator on 2017/3/17.
*/
public class ActivityRecycler extends AppCompatActivity{
SwipeToLoadLayout swipeToLoadLayout ;
RecyclerView recyclerView ;
AdapterRecycler adapterRecycler ;
ArrayList<String> arrayList ;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);
initView() ;
}
private void initView() {
swipeToLoadLayout = (SwipeToLoadLayout) findViewById(R.id.act_recycler_swipe) ;
recyclerView = (RecyclerView) findViewById(R.id.swipe_target) ;
LinearLayoutManager llm = new LinearLayoutManager(this) ;
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
swipeToLoadLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
swipeToLoadLayout.postDelayed(new Runnable() {
@Override
public void run() {
swipeToLoadLayout.setRefreshing(false);
arrayList.add("下拉刷新出来的:\n" + new Date()) ;
}
} , 2000) ;
}
});
swipeToLoadLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
swipeToLoadLayout.postDelayed(new Runnable() {
@Override
public void run() {
swipeToLoadLayout.setLoadingMore(false);
arrayList.add("上拉加载出来的:\n" + new Date() ) ;
}
} , 2000) ;
}
});
adapterRecycler = new AdapterRecycler(this) ;
arrayList = new ArrayList<>() ;
for (int i = 0; i <8 ; i++) {
arrayList.add("模拟的初始数据:" + i) ;
}
adapterRecycler.setArrayList(arrayList);
recyclerView.setAdapter(adapterRecycler);
swipeToLoadLayout.post(new Runnable() {
@Override
public void run() {
swipeToLoadLayout.setRefreshing(true);
}
}) ;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<com.aspsine.swipetoloadlayout.SwipeToLoadLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".activity.ActivityRecycler"
android:id="@+id/act_recycler_swipe"
app:refresh_final_drag_offset="100dp">
<com.wentong.administrator.swipetoloaddemo.RefreshHeaderView
android:id="@+id/swipe_refresh_header"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:background="@color/colorAccent"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/swipe_target"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
<com.wentong.administrator.swipetoloaddemo.LoadMoreFooterView
android:id="@+id/swipe_load_more_footer"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:background="@color/colorPrimaryDark"/>
</com.aspsine.swipetoloadlayout.SwipeToLoadLayout>
Adapter:
package com.wentong.administrator.swipetoloaddemo.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.wentong.administrator.swipetoloaddemo.R;
import java.util.ArrayList;
/**
* Created by Administrator on 2017/3/17.
*/
public class AdapterRecycler extends RecyclerView.Adapter<AdapterRecycler.My>{
Context context ;
ArrayList<String> arrayList ;
public ArrayList<String> getArrayList() {
return arrayList;
}
public void setArrayList(ArrayList<String> arrayList) {
this.arrayList = arrayList;
}
public AdapterRecycler(Context context) {
this.context = context;
arrayList = new ArrayList<>() ;
}
@Override
public My onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.adapter_recycler , parent , false) ;
My my = new My(view) ;
return my;
}
@Override
public void onBindViewHolder(My holder, int position) {
holder.tv_one.setText(arrayList.get(position));
holder.tv_two.setText(arrayList.get(position));
}
@Override
public int getItemCount() {
return arrayList.size();
}
class My extends RecyclerView.ViewHolder{
TextView tv_one , tv_two ;
public My(View itemView) {
super(itemView);
tv_one = (TextView) itemView.findViewById(R.id.adapter_recycler_tv_one) ;
tv_two = (TextView) itemView.findViewById(R.id.adapter_recycler_tv_two) ;
}
}
}
注意一点:我在这里写的是recyclerView ,官网上面的是ListView 我在换成RecyclerView时候出现了空指针的异常。
查看简书作者@lavor 查明原因: 就是我们xml文件里的三个子视图id必须和我一样 不然就会出现空指针异常。
OK!
进阶版,效果图:
demo地址: