zoukankan      html  css  js  c++  java
  • 下拉刷新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试试。

  • 相关阅读:
    第五周总结
    第四周总结
    第三周总结
    开课博客
    学习进度
    个人作业1-数组
    数组
    第一周考试总结
    团队个人冲刺第六天
    团队个人冲刺第五天
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7152594.html
Copyright © 2011-2022 走看看