Android 实现悬浮的几种方式(一)AppBarLayout+PtrFrameLayout

android:text=“今日”

android:textColor=“@color/gray_64”

android:textSize=“13sp” />

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“的生产装箱情况”

android:textColor=“@color/gray_a”

android:textSize=“13sp” />

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:background=“@android:color/white”

android:orientation=“vertical”

android:padding=“@dimen/d_15dp”>

<TextView

android:id=“@+id/tv_product_name”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“@string/common_null”

android:textColor=“@color/gray_32”

android:textSize=“13sp” />

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”

android:paddingTop=“29dp”>

<LinearLayout

android:layout_width=“0dp”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:gravity=“center”

android:orientation=“vertical”>

<TextView

android:id=“@+id/tv_product_boxs”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“@string/common_null”

android:textColor=“@color/gray_32”

android:textSize=“28dp” />

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginTop=“@dimen/d_10dp”

android:text=“箱”

android:textColor=“@color/gray_a”

android:textSize=“13sp” />

<TextView

android:layout_width=“1dp”

android:layout_height=“match_parent”

android:background=“@color/lineColor” />

<LinearLayout

android:layout_width=“0dp”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:gravity=“center”

android:orientation=“vertical”>

<TextView

android:id=“@+id/tv_product_singles”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“@string/common_null”

android:textColor=“@color/gray_32”

android:textSize=“28dp” />

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginTop=“@dimen/d_10dp”

android:text=“单标”

android:textColor=“@color/gray_a”

android:textSize=“13sp” />

</android.support.design.widget.AppBarLayout>

<cn.finalteam.loadingviewfinal.PtrClassicFrameLayout

app:layout_behavior=“@string/appbar_scrolling_view_behavior”

android:id=“@+id/ptr_rv_layout”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

app:ptr_duration_to_close=“300”

app:ptr_duration_to_close_header=“1500”

app:ptr_keep_header_when_refresh=“true”

app:ptr_ratio_of_header_height_to_refresh=“1.2”

app:ptr_resistance=“1.7”>

<cn.finalteam.loadingviewfinal.RecyclerViewFinal

android:id=“@+id/rv_list”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

/>

</cn.finalteam.loadingviewfinal.PtrClassicFrameLayout>

</android.support.design.widget.CoordinatorLayout>

关于AppBarLayout的五种ScrollFlags参考:

http://www.jianshu.com/p/7caa5f4f49bd

总结:

  • 对于需要跟着RecyclerView一起滚动消失的父布局,设置 app:layout_scrollFlags="scroll|enterAlways;

  • 希望悬浮的,放在(滚动布局)LinearLayout的外面AppBarLayout的里面就可以悬浮了。

  • AppBarLayout里面只存在2种布局:滚动的和悬浮的

  • 对于滚动的主布局里面添加 app:layout_behavior="@string/appbar_scrolling_view_behavior"

这样结束了,activity里面正常加载RecyclerView就可以了

mRvList.setLayoutManager(new LinearLayoutManager(this));

mCommonAdapter = new CommonAdapter();

mRvList.setAdapter(mCommonAdapter);

这就简单实现了悬浮的功能了。

但是我这里加入了下拉刷新的功能,出现了下拉刷新先于AppbarLayout的问题,即下拉的时候,页面还没完全拉下来就触发了刷新功能

此问题的解决参照:

http://blog.csdn.net/lvwenbo0107/article/details/51788601

我这里的解决:

第一步:实现OnOffsetChangedListener并重写onOffsetChanged

public class BoxingProductChartActivity extends StateViewActivity implements AppBarLayout.OnOffsetChangedListener

第二步:当verticalOffset == 0,设置允许刷新

@Override

public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

if (verticalOffset == 0) {

setCanRefresh(true);

} else {

setCanRefresh(false);

}

}

private boolean canRefresh;

public void setCanRefresh(boolean canRefresh) {

this.canRefresh = canRefresh;

}

第三步:设置给PtrClassicFrameLayout(下拉刷新)

@Bind(R.id.ptr_rv_layout)

PtrClassicFrameLayout mPtrRvLayout;

mPtrRvLayout.setOnRefreshListener(new OnDefaultRefreshListener() {

@Override

public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {

return canRefresh && OnDefaultRefreshListener.checkContentCanBePulledDown(frame, content, header);

}

@Override

public void onRefreshBegin(PtrFrameLayout frame) {

requestData();

}

});

大功告成!就是图中现在的情况了

附加:由于上方代码用到一些第三方库,下面贴一个简洁的。使用这种方式只要布局调整好了就好了,不需要写任何java代码的。。

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

<android.support.design.widget.AppBarLayout

android:id=“@+id/appbar”

最后

想要了解更多关于大厂面试的同学可以点赞支持一下,除此之外,我也分享一些优质资源,包括:Android学习PDF+架构视频+源码笔记高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 这几块的内容。非常适合近期有面试和想在技术道路上继续精进的朋友。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值