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


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

回過頭看自己曾經寫的這個博客非常多的廢話 和效果並不適合大家去使用這個好用的控件 如今整理刪掉了自己寫的效果, 寫了個最簡單的實例給一起學習的新手。並附送最精簡寫法的demo 直接給大家最想要的東西,以下的廢話 沒空就不用看了哈

核心是能夠包裹隨意view刷新。

不須要原view能滾動,比谷歌自帶的范圍更廣一些

先看效果
這里寫圖片描寫敘述

下載實例

相關Demo module免費下載

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—————————–更新內容》

//舊博客

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

哈哈 刷新本身還是非常流暢的
這里寫圖片描寫敘述

引言:

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

由於自己水平非常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什么的。結果我試了半天。

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

1、怎樣集成

注意 都是寫在你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試試。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM