下拉刷新swipetoloadlayout的使用方法,以及本身定義頭部

《20160930—————————–更新內容java

回過頭看本身曾經寫的這個博客很是多的廢話 和效果並不適合你們去使用這個好用的控件 現在整理刪掉了本身寫的效果, 寫了個最簡單的實例給一塊兒學習的新手。並附送最精簡寫法的demo 直接給你們最想要的東西,如下的廢話 沒空就不用看了哈 android

核心是可以包裹隨意view刷新。

不需要原view能滾動,比谷歌自帶的範圍更廣一些 git

先看效果
這裏寫圖片描寫敘述github

下載實例

相關Demo module免費下載markdown

JAVA

package com.rex;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;

/** * 用於swipetoloadlayout的demo演示 */
public class MainActivity extends AppCompatActivity {

    private SwipeToLoadLayout swipeToLoadLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        swipeToLoadLayout = (SwipeToLoadLayout) findViewById(R.id.swipeToLoadLayout);
        HeaderView swipe_refresh_header = (HeaderView) findViewById(R.id.swipe_refresh_header);
        FooterView swipe_load_more_footer = (FooterView) findViewById(R.id.swipe_load_more_footer);

        swipeToLoadLayout.setRefreshHeaderView(swipe_refresh_header);
        swipeToLoadLayout.setLoadMoreFooterView(swipe_load_more_footer);
        //加入過渡滑動 其它設置 本身依據英文嘗試吧
        swipeToLoadLayout.setRefreshCompleteDelayDuration(2000);


        swipeToLoadLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                Toast.makeText(MainActivity.this, "OnRefreshListener!", Toast.LENGTH_SHORT);
                swipeToLoadLayout.setRefreshing(false);//收頭
            }
        });
        swipeToLoadLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                Toast.makeText(MainActivity.this, "OnLoadMoreListener!", Toast.LENGTH_SHORT);
                swipeToLoadLayout.setLoadingMore(false);
            }
        });
    }
}
package com.rex;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.SwipeRefreshTrigger;
import com.aspsine.swipetoloadlayout.SwipeTrigger;

/** * Created by Rex on 2016/9/30. * 是否是LinearLayout都無所謂 你可以用你喜歡的形式 */
public class HeaderView extends LinearLayout implements SwipeRefreshTrigger, SwipeTrigger {

    private TextView tvStatus;

    public HeaderView(Context context) {
        this(context, null, 0);
    }


    public HeaderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        //這個view隨意定義

        //這裏的原理就是簡單的動態佈局加入
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        View view = View.inflate(getContext(), R.layout.header, null);
        addView(view, lp);


        tvStatus = (TextView) view.findViewById(R.id.tvTest);
    }

    @Override
    public void onRefresh() {
        tvStatus.setText("onRefresh");
    }

    @Override
    public void onPrepare() {
        tvStatus.setText("onPrepare");
    }

    @Override
    public void onSwipe(int i, boolean b) {
        tvStatus.setText("onSwipe" + i);
    }

    @Override
    public void onRelease() {
        tvStatus.setText("onRelease");
    }

    @Override
    public void complete() {
        tvStatus.setText("complete");
    }

    @Override
    public void onReset() {
        tvStatus.setText("測試測試");
    }


}

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" 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.rex.MainActivity">

    <com.aspsine.swipetoloadlayout.SwipeToLoadLayout  android:id="@+id/swipeToLoadLayout" android:layout_width="match_parent" android:layout_height="match_parent" >

        <com.rex.HeaderView  android:id="@+id/swipe_refresh_header" android:layout_width="match_parent" android:layout_height="wrap_content"/>

        <TextView  android:id="@+id/swipe_target" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#c3c9" android:gravity="center" android:text="包裹想被刷新的隨意View"/>

        <com.rex.FooterView  android:id="@+id/swipe_load_more_footer" android:layout_width="match_parent" android:layout_height="wrap_content"/>


    </com.aspsine.swipetoloadlayout.SwipeToLoadLayout>
</RelativeLayout>

20160930—————————–更新內容》app

//舊博客

開始鼠標兩次點到標題欄了。框架

哈哈 刷新自己仍是很是流暢的
這裏寫圖片描寫敘述maven

引言:

從開發到現在確定是想弄一套適合本身的刷新的控件,最開始一直用的pulltorefresh。只是 不知道是我使用問題仍是自己有問題。刷新過快就會卡主等一些小bug,而後是每個控件都是本身定義的。漸漸的想用寫的樣式了。ide

因爲本身水平很是low寫出來的也很是low因此仍是想着用框架。
而後轉Android studio後接觸到了Google自帶的SwipeRefreshLayout,包裹需要刷新的view 刷新樣式也很是新穎。但樣式侷限,子佈局還必定得自身是可以滑動的佈局,還沒提供上拉載入的效果,SwipeRefreshLayout居然和自家RecyclerView衝突。
但是這樣的包裹需要刷新的view實在是不錯的設定,因此我就在找有沒有帶着這個長處。又還有其它特點如本身定義頭尾部很是方便的。佈局

最終我找到了[swipetoloadlayout](https://github.com/Aspsine/SwipeToLoadLayout)
他如下三哥們爲基礎

Google SwipeRefreshLayout
liaohuqiu android-Ultra-Pull-To-Refresh
Yalantis Phoenix
因此有SwipeRefreshLayout喜歡的部分。

1.支持上下拉,頭部和尾部也都是用接口實現可塑造性高,多種流行下拉方式
2.包裹其它需要刷新的View就能夠
3.設定了一下刷完完畢本身定義 釋放後的緩緩滑動 更加流暢

說了半天廢話我仍是來說我摸索到的使用方法。
爲何要說摸索。因爲我一開始都不知道怎麼把頭部收回去…一般是complete啊 finish什麼的。結果我試了半天。

直接說我搜出來加摸索的詳細詳盡使用方法了。

一、怎樣集成

注意 都是寫在你module的gradle 裏面
Step 1. Add the JitPack repository in your build.gradle at the end of repositories:

repositories {
maven { url 「https://jitpack.io」 }
}
Step 2. Add the dependency in the form

dependencies {
compile ‘com.github.Aspsine:SwipeToLoadLayout:v1.0.0’
}
SwipeToLoadLayout日後更新了兩個版本號,大家也可以用新的。
這個版本號就是你sdk要更新到23,否則會缺省一些主題等value的值

2.開始本身定義刷新效果

swipeToLoadLayout提供了一套接口。刷新的頭部本身定義一個View實現SwipeTrigger和SwipeRefreshTrigger就能夠了,刷新的尾部本身定義一個View實現SwipeLoadMoreTrigger和SwipeTrigger。頭部實現代碼:

我搜到的是一個Textview做爲頭部 因此遠遠不能知足我要的本身定義。
因此這裏給上個人本身定義頭部,因爲設計到一些效果。你們可以理解了換成一些簡單的控件就能夠

也就是說LinearLayout你可以換成不論什麼View 成爲你的頭佈局。

3.詳細使用方法

按例如如下固定寫法3個id固定就能夠。第一個id不需要是固定的。但寫成了固定方便初始化 見2中方法initSwipe

注意。swipetoloadlayout中佈局包裹的View id是指定的,不能亂改。不然找不到
Add a comment to this line
swipe_target」 type=」id」 刷新目標
swipe_refresh_header」 type=」id」 刷新頭部
swipe_load_more_footer」 type=」id」 刷新尾部

3.1首先 xml文件裏`

 
  

3.2 Java代碼中

「`

第一次寫博客 比較囉嗦,但這刷新控件確實不錯。建議你們先改爲簡單的頭佈局試一下,你們可以看此連接SwipeToLoadLayout–小白也能輕鬆定製本身的刷新效果,我也是看到了這個。才開始去使用,假設想着本身定義略微複雜的控件和使用可以跟我探討更好的方法。

腳佈局也是一個邏輯哦。認真看的說明是絕對可以寫出本身相應的。

需要的demo的留言我之後傳。
我這裏面加了幾個本身定義控件。

你們可以用一個簡單的textview試試。

相關文章
相關標籤/搜索