版权声明:本文为xing_star原创文章,转载请注明出处!

本文同步自http://javaexception.com/archives/103

个人详情页滑动到顶部

最近产品提了个新需求,需要实现点击App内的某个按钮跳转到个人详情页并且滑动到顶部,个人详情页的页面交互稍微复杂,技术角度上包含了状态栏颜色变换,view滑动联动等问题,技术实现上采用了Google出的CoordinatorLayout那套组件,由于App的个人详情页跟微博的相似,这里就拿微博为例来描述。微博默认的效果以及手动滑动到顶部的效果图如下。

个人详情页技术实现分析:

先看看xml布局的伪代码:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff6f6f6"> <android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="@color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@+id/toolbar"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:visibility="invisible"
android:background="@color/white"
app:layout_collapseMode="pin"> </android.support.v7.widget.Toolbar> </android.support.design.widget.CollapsingToolbarLayout> <android.support.design.widget.TabLayout
android:id="@+id/gift_tab"
style="@style/CustomerTabLayout"
android:layout_width="match_parent"
android:layout_height="45dp"
app:tabGravity="center"
app:tabMode="scrollable" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f2f2f2" /> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager
android:id="@+id/viewpager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </android.support.design.widget.CoordinatorLayout>

整个结构上分为两部分,AppBarLayout(里面包含TabLayout),ViewPager,根节点是CoordinatorLayout。上下滑动会引起AppBarLayout的联动,悬浮在顶部,或者是跟着viewPager一起滑动以及视差效果之类的。目前我们要实现的是,在进入当前页面时,强制让AppBarLayout滑动到顶部,使toolbar悬浮固定不动。那么该怎么做呢,一种思路是在onCreate()方法中,发post任务,页面渲染结束后,执行post任务,post的任务是调用AppBarLayout的API方法,让AppBarLayout往上滑。

appBarLayout.post(() -> {
//...具体的滑动逻辑
});

操作AppBarLayout滑动的是对应的Behavior。在CoordinatorLayout这套组件里面体现的淋漓尽致。感兴趣的可以好好分析下CoordinatorLayout是如何完成事件分发的,如何让子view相互联动的。

这里具体的滑动逻辑伪代码为:

CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior();
if (behavior instanceof AppBarLayout.Behavior) {
AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout.Behavior) behavior;
if (mCollapsingHeight != 0) {
appBarLayoutBehavior.setTopAndBottomOffset(-Math.abs(mCollapsingHeight));
}
}

我们将appBarLayout向上滑动了mCollapsingHeight的高度,那么这个高度值是如何计算出来的呢。这个值,实际上是在最开始做个人详情页这个需求就已经得出的值。

mCollapsingHeight = appBarLayout.getHeight() - toolbar.getHeight() - DisplayUtils.dip2px(46);

这个值需要结合页面布局来计算,我们的页面布局两部分中,最上面的是appBarLayout,规定的是距离靠近toolbar的高度就产生渐变,toolbar开始固定位置,那么就需要按照这个公式计算mCollapsingHeight。

完整的代码如下:

private void initView() {
appBarLayout.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
mCollapsingHeight = appBarLayout.getHeight() - toolbar.getHeight() - DisplayUtils.dip2px(46);
});
if (scrollType != 0) {
appBarLayout.post(() -> {
CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior();
if (behavior instanceof AppBarLayout.Behavior) {
AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout.Behavior) behavior;
if (mCollapsingHeight != 0) {
appBarLayoutBehavior.setTopAndBottomOffset(-Math.abs(mCollapsingHeight));
}
}
});
}
}

个人详情页相关:

在Github上找到了一个仿微博个人详情页的开源项目,https://github.com/whisper92/WeiboProfile,技术实现上采用的是ScrollView,ListView,部分代码可以看看。

ps:

由于服务器出了问题,这篇文章内容已丢失,这是重新写的

如何让Android微博个人详情页滚动到顶部的更多相关文章

  1. iOS开发——UI进阶篇(十)导航控制器、微博详情页、控制器的View的生命周期

    一.导航控制器出栈 1.initWithRootViewController本质 UIViewController *vc = [[OneViewController alloc] init]; // ...

  2. iOS之UI--微博个人详情页

    前言:微博个人详情页,和我常用的的QQ空间的详情页是同样的.要求能够融会贯通,做这一类的界面能够快速上手实现. 动态图效果展示: 直接使用UINavigationBar->UITableView ...

  3. Android 自定义控件 轻松实现360软件详情页

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43649913,本文出自:[张鸿洋的博客] 1.概述 最近有不少朋友私聊问应用宝. ...

  4. Android仿淘宝继续上拉进入商品详情页的效果,使用双Fragment动画切换;

    仿淘宝继续上拉进入商品详情页的效果,双Fragment实现: 动画效果: slide_above_in.xml <?xml version="1.0" encoding=&q ...

  5. Android跳转淘宝、京东APP商品详情页

    import Android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; imp ...

  6. Android点击跳转到淘宝的某一商品详情页或者某一店铺页面

    最近项目的有个需求是点击购买资料按钮进入淘宝界面,简单分析一下,如果用户手机有淘宝就打开淘宝的页面,没有的话也可以选择使用webView进行展示,还是使用手机浏览器进行展示. 判断有无淘宝的代码就不贴 ...

  7. react 从商品详情页返回到商品列表页,列表自动滚动上次浏览的位置

    现状:目前从商品详情页返回到商品列表页,还需要再去请求服务数据,还需要用户再去等待获取数据的过程,这样用户体验非常不好, 遇到的问题: 1:如何将数据缓存, 2:如何获取和保存列表滑动的高度, 3:判 ...

  8. 高仿淘宝和聚美优品商城详情页实现《IT蓝豹》

    高仿淘宝和聚美优品商城详情页实现 android-vertical-slide-view高仿淘宝和聚美优品商城详情页实现,在商品详情页,向上拖动时,可以加载下一页. 使用ViewDragHelper, ...

  9. 自己定义ViewGroup实现仿淘宝的商品详情页

    近期公司在新版本号上有一个须要. 要在首页加入一个滑动效果, 详细就是仿照X宝的商品详情页, 拉到页面底部时有一个粘滞效果, 例如以下图 X东的商品详情页,假设用户继续向上拉的话就进入商品图文描写叙述 ...

随机推荐

  1. mysql 存储过程(支持事务管理)

    CREATE DEFINER=`root`@`localhost` PROCEDURE `createBusiness`(parameter1 int) BEGIN #Routine body goe ...

  2. python xmlrpc

    rpc 协议 RPC = Remote Procedure Call Protocol,即远程过程调用协议. xml rpc 协议 使用http协议作为传输协议,使用xml文本传输命令和数据的一种协议 ...

  3. PHP中的多行字符串传递给JavaScript方法两则

    PHP和JavaScript都是初学.近期有这么个需求: 例如说有一个PHP的多行字符串: $a = <<<EOF thy38 csdn blog EOF; 传递给JavaScrip ...

  4. 【网络协议】IP协议、ARP协议、RARP协议

    IP数据报 IP是TCP/IP协议族中最核心的协议,全部的TCP.UDP.ICMP.IGMP数据都以IP数据报的格式传输.IP仅提供尽力而为的传输服务.假设发生某种错误.IP会丢失该数据.然后发送IC ...

  5. 流媒体开发之开源项目live555---live555 server 编译 包括更改帧率大小

    由于要测试8148解码器的性能,需要搭建不同帧率25fps - >30fps,宏块大小defualt 100 000 -> 200 000不同大小的h264码流,所以就需要编译改动的liv ...

  6. LeetCode(28)题解:Implement strStr()

    https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the fir ...

  7. 例子:两个表根据productID合并

  8. UI标签库专题三:JEECG智能开发平台 FormValidation(表单提交及验证标签)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/28484209  自己定义弹出框提示 ...

  9. Spring的声明式事务

    1.与hibernate集成 <bean id="sessionFactory" class="org.springframework.orm.hibernate3 ...

  10. Android「后台下载」Feb.24小记

    参考了CSDN上的这个文章(HERE),之前只是新开一个线程: public class DownloadThread implements Runnable{ String tarFile ; pu ...