swipeReferenceLayout 无法完成上来加载数据所以自定义了一个类

Activity.java

package qianfeng.com.swipelayoutdemo;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity { private SwipeRefreshLayout swipeRefreshLayout;
private List<String> list = new ArrayList<>();
private ListView listView;
private ReferenceLayout referenceLayout;
private ListView referenceListView;
private ArrayAdapter<String> adapter;
private List<String> total = new ArrayList<>();
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 100:
adapter.notifyDataSetChanged();
referenceLayout.setRefreshing(false);
break;
case 200:
adapter.notifyDataSetChanged();
referenceLayout.setLoadingState(false);
break;
}
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); referenceListView = (ListView) findViewById(R.id.referenceListView);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, total);
referenceListView.setAdapter(adapter); referenceLayout = (ReferenceLayout) findViewById(R.id.referenceLayout);
referenceLayout.setColorSchemeColors(Color.RED, Color.BLACK);
referenceLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// 下拉刷新的监听器
total.clear();
initData(100); }
});
referenceLayout.setOnLoadingListener(new ReferenceLayout.OnLoadingListener() {
@Override
public void onLoading() {
//上拉加载更多的监听器 initData(200); }
}); initData(100);
adapter.notifyDataSetChanged(); } private void initData(final int what) {
new Thread() {
@Override
public void run() {
super.run();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 20; i++) {
total.add("景田" + i);
}
handler.sendEmptyMessage(what);
}
}.start(); } // 默认 swipeReferenceLayout 的用法
public void defaultView() { swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
listView = (ListView) findViewById(R.id.listView);
// 设置颜色
swipeRefreshLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW);
for (int i = 0; i < 5; i++) {
list.add("xixi" + i);
}
ArrayAdapter adapte = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapte); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Toast.makeText(MainActivity.this, "hahahah", Toast.LENGTH_SHORT).show(); // 刷新完成后 将刷新的View 隐藏掉
// swipeRefreshLayout.setRefreshing(false); }
}); }
}

ReferenceLayout.java 自定义

package qianfeng.com.swipelayoutdemo;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView; /**
* Created by ${Mr.Zhao} on 2016/10/18.
*/
public class ReferenceLayout extends SwipeRefreshLayout implements AbsListView.OnScrollListener { //2. 实例化接口对象
private OnLoadingListener listener;
private View footerView;
private ListView mListView;
private boolean isLoading = false; //1. 定义 一个接口
public interface OnLoadingListener {
public void onLoading();
} //3. 提供设置监听器的方法
public void setOnLoadingListener(OnLoadingListener listener) {
this.listener = listener;
} // 4. 构造布局
public ReferenceLayout(Context context) {
super(context);
initFooterView(context);
} public ReferenceLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initFooterView(context);
} // 实例化 一个 加载更多的时候的 底布局
private void initFooterView(Context context) {
footerView = LayoutInflater.from(context).inflate(R.layout.footer_layout, null);
} // 6 创建一个方法 用来 添加或者移除 footerView
public void setLoadingState(boolean state) {
isLoading = state;
if (state) {
mListView.addFooterView(footerView);
} else {
mListView.removeFooterView(footerView);
}
} @Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
// 5. 获取 ListView
int childCount = getChildCount();
if (childCount > 0) {
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
if (view instanceof ListView) {
mListView = (ListView) view;
mListView.setOnScrollListener(this);
}
}
}
} @Override
public void onScrollStateChanged(AbsListView view, int scrollState) { } // 7 判断ListView 滑动到了 底部
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount == totalItemCount && !isLoading) {
setLoadingState(true);
listener.onLoading();
}
}
}

布局文件:

<?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" tools:context="qianfeng.com.swipelayoutdemo.MainActivity"> <android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"> <ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</android.support.v4.widget.SwipeRefreshLayout> <qianfeng.com.swipelayoutdemo.ReferenceLayout
android:id="@+id/referenceLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ListView
android:id="@+id/referenceListView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView> </qianfeng.com.swipelayoutdemo.ReferenceLayout>
</RelativeLayout>

其上拉布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/footer_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="加载更多"
android:textSize="20sp" />
</LinearLayout>

安卓高级2 swipeReferenceLayout 使用案例 和完善其自定义上拉的更多相关文章

  1. Easyui入门视频教程 第09集---登录完善 图标自定义

    目录 ----------------------- Easyui入门视频教程 第09集---登录完善 图标自定义   Easyui入门视频教程 第08集---登录实现 ajax button的使用  ...

  2. 安卓高级3 RecyclerView 和cardView使用案例

    cardView: 添加依赖:在Studio搜索cardview即可 在V7包中 或者直接在gradle中添加 compile 'com.android.support:cardview-v7:24. ...

  3. 安卓高级8 SurfaceView案例三 结合mediaplay播放视频

    我们知道mediaplay无法直接播放视频所以我们结合Surface package qianfeng.com.mediaplayerdemo; import android.media.MediaP ...

  4. 安卓高级8 SurfaceView案例二 自定义相机

    效果:(由于不好录屏所以文字描述) 定一个SurfaceView 下方有几个按钮,点击确定可以拍照保存取消. 并且SurfaceView实时显示相机内容 package qianfeng.com.cu ...

  5. 安卓高级2 Volley框架的使用案例

    初始化类: MyApp.java package qianfeng.com.day37_volley_pull.app; import android.app.Application; import ...

  6. 安卓高级 WebView的使用到 js交互

    我们先来学习 怎么使用再到用js和安卓源生方法交互 WebView简单使用 此部分转载并做了补充 原博客 原因:比较简单不是很想在写,我只要写js交互部分 WebView可以使得网页轻松的内嵌到app ...

  7. 安卓高级9 用原生intent分享

    大家都用过安卓app时发现有个分享按钮如下: 所以今天特此分享用用原生完成: package qianfeng.com.simplesharedemo; import android.content. ...

  8. 安卓高级1 -----Xutil3 和Picasso使用

    Xutils3 Xutils由于内部使用httpclient然而在安卓5.0谷歌发现httpclient出现不稳定的情况.于6.0完全弃用,所以作者升级到Xutils3替换原本网络模块 配置环境(St ...

  9. 某PCBA企业应用易普优APS实现高级计划排程案例

    一.项目介绍 1.生产计划现状 某PCBA企业(以下简称A企业)的产品生产是典型的多品种.小批量.多变化的生产模式.其中产品种类有1000多种,主流的200多种,每个月数百个生产订单,分解到工序以后的 ...

随机推荐

  1. [LeetCode] Non-decreasing Array 非递减数列

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

  2. promise 的基本概念 和如何解决js中的异步编程问题 对 promis 的 then all ctch 的分析 和 await async 的理解

    * promise承诺 * 解决js中异步编程的问题 * * 异步-同步 * 阻塞-无阻塞 * * 同步和异步的区别? 异步;同步 指的是被请求者 解析:被请求者(该事情的处理者)在处理完事情的时候的 ...

  3. 【基础】EM 还是 REM?这是一个问题!

    简言 应用象EM 和 REM这种相对长度单位进行页面排版是WEB开发中的最佳实践.在页面排版中较好应用EM 和 REM,根据设备尺寸缩放显示元素的大小.这就使得组件在不同设备上都达到最佳的显示效果成为 ...

  4. 词向量:part 1 WordNet、SoW、BoW、TF-IDF、Hash Trick、共现矩阵、SVD

    1.基于知识的表征 如WordNet(图1-1),包含同义词集(synonym sets)和上位词(hypernyms,is a关系). 存在的问题: 作为资源来说是好的,但是它失去了词间的细微差别, ...

  5. [POI 2004]SZP

    Description Byteotian 中央情报局 (BIA) 雇佣了许多特工. 他们每个人的工作就是监视另一名特工.Byteasar 国王需要进行一次秘密行动,所以他要挑选尽量多的信得过的特工. ...

  6. [HEOI2014]大工程

    题目描述 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道. 我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上. 在 2 个国家 a,b 之间建一条新通道需要的代价为树上 ...

  7. POJ 3590 The shuffle Problem

    Any case of shuffling of n cards can be described with a permutation of 1 to n. Thus there are total ...

  8. LGTB 与序列

    LGTB 有一个长度为N 的序列A,现在他想构造一个新的长度为N 的序列B,使得B 中的任意两个数都 互质. 并且他要使ai与bi对应项之差最小 请输出最小值 输入 第一行包含一个数N 代表序列初始长 ...

  9. 计蒜客NOIP模拟赛5 D1T1 机智的 AmyZhi

    那年一个雨季,AmyZhi 在校门外弯身买参考书. 这时 SiriusRen 走过来,一言不合甩给她一道“自认为”很难的题: --------------- 给你一个数字 NN(NN 的范围是 11  ...

  10. 51 nod 1766 树上的最远点对(线段树+lca)

    1766 树上的最远点对 基准时间限制:3 秒 空间限制:524288 KB 分值: 80 难度:5级算法题   n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个 ...