Android中SwipeToLoadLayout的使用

SwipeToLoadLayout輕松定制自己的刷新效果,且子布局不在局限只是可以滑動的布局(如ListView, ScrollVie,RecyclerView)。

Android Studio中使用

Gradle中添加 compile 'com.github.Aspsine:SwipeToLoadLayout:1.0.4'
GitHub地址:https://github.com/Aspsine/SwipeToLoadLayout

自定義刷新、加載的效果

SwipeToLoadLayout提供了一套接口,自定義的View只需要實現(xiàn)相應(yīng)的接口就好了,頭實現(xiàn)SwipeRefreshHeaderLayout,尾實現(xiàn)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>

代碼中調(diào)用

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);
    }
}

到此就結(jié)束,想要更多刷新案例可查看http://www.apkbus.com/thread-275506-1-1.html,感覺您的閱讀,期望能給您帶來幫助!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,492評論 2 45
  • 今日無事,無事,說無事,其實是躲事,總想躲著避世,做一枯燈女尼,或是閑云野鶴,甚或是一株植物,長在山野路邊,唯獨不...
    活著不易閱讀 260評論 11 9
  • 8月8日,成都,18:00 該下班了,突然烏云密布,狂風大作,樓下的竹林被吹斷了腰,嚓擦直響,很多體型嬌小的女生大...
    李卿慈閱讀 335評論 0 4
  • 晚上在騰訊空間里看到以前的一位學姐去世的消息,挺震驚的。我雖然跟她不是特別熟,但是當初剛來農(nóng)大沒多久見過面,打過招...
    傲嬌的我很傲嬌閱讀 179評論 0 0
  • 大腦,一個重約1.4KG左右的人體器官,是人體中最復(fù)雜,最精密的組織,對大腦的研究也極具挑戰(zhàn)性。借助科學技術(shù)的發(fā)展...
    rogerzhaz閱讀 1,941評論 0 2