Toolbar滚动渐显类似Google+用户界面滑动效果(一)
来源:互联网 发布:淘宝红包怎么设置 编辑:程序博客网 时间:2024/05/16 17:55
Toolbar滚动渐显类似Google+用户界面滑动效果(一)
@(Blog)[马克飞象|Markdown|Android]
- Toolbar滚动渐显类似Google用户界面滑动效果一
- 最终效果
- 实现
- 布局
- 代码
最终效果
先来张效果图,不过还没有实现标题Tab向上滑时可以贴合Toolbar的效果,只是实现了渐显的效果
实现
- 实现这个效果主要需要Toolbar、SlidingTabLayout、ScrollView、ViewPager。
- 如果这些控件之前没接触的话可以看下我之前博客,除了ScrollView另外3个都由介绍。
- 主要会遇到的问题有2个,一个ViewPager在此图的这种效果中可能会出现
Fragment
不显示的问题,还有一个就是ScrollView
竟然没有滚动监听事件。
布局
布局方面不多说了,直接上代码
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.focus.androidnote.toolbar.ToolbarFadeInActivity"> <ScrollView android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:background="@drawable/mat5" android:scaleType="centerCrop"/> <android.support.v7.widget.Toolbar android:id="@+id/toolbar_tab" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#B6B6B6"> <com.example.focus.androidnote.toolbar.SlidingTabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.v7.widget.Toolbar> <com.example.focus.androidnote.viewpager.AutoHeightViewPager android:id="@+id/page" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </com.example.focus.androidnote.toolbar.MyScrollView> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:theme="@style/Base.ThemeOverlay.AppCompat" android:minHeight="?attr/actionBarSize" android:background="@android:color/transparent"> </android.support.v7.widget.Toolbar></FrameLayout>
这里用到两个Toolbar,一个用于替换ActionBar
,还有个是用来绑定Tag
代码
onCreate代码
mToolbar = (Toolbar) findViewById(R.id.toolbar);mToolbarTab = (Toolbar) findViewById(R.id.toolbar_tab);mTabLayout = (SlidingTabLayout) findViewById(R.id.tab_layout);mPager = (ViewPager) findViewById(R.id.page);mMainLayout = (MyScrollView) findViewById(R.id.main_layout);mBackgrounds = new String[] {"#616161", "#9E9E9E", "#F5F5F5"};mFragmentList = new ArrayList<>();setSupportActionBar(mToolbar);//显示回退按钮getSupportActionBar().setDisplayHomeAsUpEnabled(true);for (int i = 0; i < mBackgrounds.length; i++) { Fragment fragment = TextFragment.newInstance("Fragment" + i, mBackgrounds[i]); mFragmentList.add(fragment);}mPager.setAdapter(new TagPageAdapter(getSupportFragmentManager()));mTabLayout.setViewPager(mPager);mMainLayout.setScrollViewListener(new MyScrollView.ScrollViewListener() { @Override public void onScrollChanged(int x, int y, int oldxX, int oldY) { if (y >=0 && y <= 255) { mToolbar.setBackgroundColor(Color.argb(y, 0, 0, 255)); } }});
特别注意Fragment
的高度要设高一点,否则会没有足够的高度让ScrollView滚动。
我之前提到,ScrollView
和ViewPager
使用的时候可能会遇到问题,当在ScrollView
中嵌套ViewPager
会出现ViewPager
无法显示的问题,解决这个问题可以看SrollView 嵌套ViewPager会出现ViewPager不显示的问题 ,还有一个就是ScrollView
没有滚动事件监听的问题,这个其实也可以通过onTouchListener来替代,不过我觉得利用滚动监听事件会更好。
提示
有一个约定:在覆写onMeasure方法的时候,必须调用 setMeasuredDimension(int,int)来存储这个View经过测量得到的measured width and height。
http://www.cnblogs.com/mengdd/p/3332882.html
0 0
- Toolbar滚动渐显类似Google+用户界面滑动效果(一)
- SearchView结合Toolbar 筛选RecyclerView中的内容,类似知乎、google play实现的效果
- SearchView结合Toolbar 筛选RecyclerView中的内容,类似知乎、google play实现的效果
- Toolbar滑动效果实现总结
- ToolBar实现滑动伸缩效果
- Android实现渐显按钮的左右滑动效果
- Android实现渐显按钮的左右滑动效果
- Android实现渐显按钮的左右滑动效果
- Android实现渐显按钮的左右滑动效果
- Android实现渐显按钮的左右滑动效果
- Android实现渐显按钮的左右滑动效果
- Android实现渐显按钮的左右滑动效果
- Android实现渐显按钮的左右滑动效果
- Android实现渐显按钮的左右滑动效果
- Android实现渐显按钮的左右滑动效果
- 类似launcher桌面滑动效果
- ScrollView滑动悬停ToolBar效果实现
- Android Support Library-标题栏(Toolbar)滚动效果
- 浅谈 System.Linq.Enumerable.AsEnumerable 方法
- Qt实战之开发CSDN下载助手 (1)
- Spring讲解------------零配置
- 谈谈 .NET Reflector
- ThinkPHP中RBAC类的四种用法分析
- Toolbar滚动渐显类似Google+用户界面滑动效果(一)
- UI_最基础的登陆界面
- 后台处理前台模板例子(tmp文件)
- 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)
- 并查集详解,不会的同学可以来瞅瞅,转载的,不过加了一些自己的理解。
- 求的数组中不定元素的和为一个定制的所有可能
- WAS_集群部署应用遭遇ADMA0085E和ADMA0109W错误
- arcgis AO 和AE的区别
- 手动编译安装LAMP架构,并且实现nagios图形化监控(四)