您的位置:首页 > 其它

下拉刷新swipetoloadlayout的用法,以及自定义头部

2016-03-09 23:31 351 查看
《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 SwipeRefreshLayoutliaohuqiu android-Ultra-Pull-To-RefreshYalantis 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 formdependencies {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 lineswipe_target” type=”id” 刷新目标swipe_refresh_header” type=”id” 刷新头部swipe_load_more_footer” type=”id” 刷新尾部

3.1首先 xml文件中`

3.2 Java代码中

“`第一次写博客 比较啰嗦,但这刷新控件确实不错,建议大家先改成简单的头布局试一下,大家可以看此链接SwipeToLoadLayout–小白也能轻松定制自己的刷新效果,我也是看到了这个,才开始去使用,如果想着自定义稍微复杂的控件和使用可以跟我探讨更好的方法。脚布局也是一个逻辑哦。认真看的说明是绝对可以写出自己对应的。需要的demo的留言我以后传。我这里面加了几个自定义控件。大家可以用一个简单的textview试试。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: