Whatsapp 喜欢折叠工具栏
Posted
技术标签:
【中文标题】Whatsapp 喜欢折叠工具栏【英文标题】:Whatsapp like Collapsing Toolbar 【发布时间】:2016-06-28 08:01:47 【问题描述】:在我的应用程序中,我想实现类似可折叠工具栏的 Whatsapp 主页。也就是说,向下滚动列表时,工具栏应该向上,并且选项卡应该固定在顶部。我怎样才能做到这一点?
这是我的应用栏布局
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_
android:id="@+id/appbarLayout"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_
android:layout_
android:id="@+id/collapsibleToolBarLayout"
android:fitsSystemWindows="true"
app:titleEnabled="false"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_
android:layout_
android:background="@color/white"
app:tabTextColor="@color/product_page_btn_grey"
app:tabSelectedTextColor="@color/upcomer_background_red"
app:tabIndicatorColor="@color/upcomer_background_red"
android:layout_gravity="bottom"
app:layout_scrollFlags="scroll|enterAlways"
app:tabContentStart="72dp" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.9"
android:background="@color/upcomer_background_red"
app:popupTheme="@style/AppTheme.PopupOverlay" >
<ImageView
android:layout_
android:layout_
android:id="@+id/title_image_view"
android:contentDescription="@null"
android:layout_gravity="start|center_vertical"
android:visibility="gone"
android:src="@drawable/title"/>
<RelativeLayout
android:layout_
android:layout_
android:id="@+id/title_layout"
android:visibility="gone">
<TextView
android:layout_
android:layout_
android:layout_centerVertical="true"
android:id="@+id/toolbarText"
style="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title"/>
<TextView
android:layout_
android:layout_
android:id="@+id/send_button"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:text="@string/send_text"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold"
android:visibility="gone"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
现在TabLayout
不可见并且Toolbar
保持在那里,即使下面的ViewPager
中的列表已滚动。请帮忙。
【问题讨论】:
【参考方案1】:为了实现这个功能实际上不需要CollapsingToolbarLayout,你可以折叠设置为ActionBar的Toolbar。
这里是使用工具栏的示例代码,用于将折叠的 ActionBar,以及带有 ViewPager 的 TabLayout。
首先确保 MainActivity 使用的样式是没有 ActionBar 的样式,例如:
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<!-- ....... -->
</style>
MainActivity.java,具有 FragmentPagerAdapter 并设置选项卡:
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
PagerAdapter pagerAdapter =
new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < tabLayout.getTabCount(); i++)
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
@Override
public void onResume()
super.onResume();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
int id = item.getItemId();
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
class PagerAdapter extends FragmentPagerAdapter
String tabTitles[] = new String[] "Tab One", "Tab Two", "Tab Three" ;
Context context;
public PagerAdapter(FragmentManager fm, Context context)
super(fm);
this.context = context;
@Override
public int getCount()
return tabTitles.length;
@Override
public Fragment getItem(int position)
switch (position)
case 0:
return new BlankFragment();
case 1:
return new BlankFragment();
case 2:
return new BlankFragment();
return null;
@Override
public CharSequence getPageTitle(int position)
// Generate title based on item position
return tabTitles[position];
public View getTabView(int position)
View tab = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_tab, null);
TextView tv = (TextView) tab.findViewById(R.id.custom_text);
tv.setText(tabTitles[position]);
return tab;
activity_main.xml
重要部分:
使用 CoordinatorLayout 在工具栏中使用app:layout_scrollFlags="scroll|enterAlways|snap"
属性
在中使用app:layout_behavior="@string/appbar_scrolling_view_behavior"
ViewPager 属性
这是 activity_main.xml 文件:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_layout"
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_
android:layout_
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_
android:layout_
app:elevation="6dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:elevation="0dp"
app:layout_scrollFlags="scroll|enterAlways|snap"
/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
app:tabMode="fixed"
android:layout_below="@+id/toolbar"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:elevation="0dp"
app:tabTextColor="#d3d3d3"
app:tabSelectedTextColor="#ffffff"
app:tabIndicatorColor="#ff00ff"
android:minHeight="?attr/actionBarSize"
/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_below="@+id/tab_layout"
android:layout_
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>
custom_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:layout_>
<TextView
android:id="@+id/custom_text"
android:layout_
android:layout_
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:textSize="16dip"
android:textColor="#ffffff"
android:singleLine="true"
/>
</LinearLayout>
BlankFragment.java,这只是添加了足够的项目以使其滚动:
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;;
public class BlankFragment extends Fragment
public BlankFragment()
// Required empty public constructor
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
rv.setHasFixedSize(true);
MyAdapter adapter = new MyAdapter(new String[]"test one", "test two", "test three", "test four", "test five" , "test six" , "test seven", "test eight" , "test nine");
rv.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
return rootView;
fragment_blank.xml,请务必使用 RecyclerView 或任何其他支持嵌套滚动的 View,例如 NestedScrollView
(旁注:您可以在 api-21 及更高版本上调用 setNestedScrollingEnabled(true)
以使其与 ListView 一起使用):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<android.support.v7.widget.SearchView
android:id="@+id/sv_search"
android:layout_
android:layout_
android:hint="Search!"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:layout_gravity="start"
android:layout_marginRight="18dp"
android:ems="10" >
</android.support.v7.widget.SearchView>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_recycler_view"
android:layout_below="@+id/sv_search"
android:layout_
android:layout_ >
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
MyAdapter.java,RecyclerView 适配器:
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
private String[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder
public CardView mCardView;
public TextView mTextView;
public MyViewHolder(View v)
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset)
mDataset = myDataset;
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType)
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_item, parent, false);
// set the view's size, margins, paddings and layout parameters
MyViewHolder vh = new MyViewHolder(v);
return vh;
@Override
public void onBindViewHolder(MyViewHolder holder, int position)
holder.mTextView.setText(mDataset[position]);
@Override
public int getItemCount()
return mDataset.length;
card_item.xml,圆形图片和“blah blah blah”字符串都是静态内容,对于这个简单的例子,只有tv_text
TextView是从数据源更新的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >
<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_margin="10dp"
android:layout_
card_view:cardCornerRadius="4dp"
card_view:elevation="14dp">
<RelativeLayout
android:layout_
android:layout_>
<ImageView
android:id="@+id/iv_image"
android:layout_
android:layout_
android:src="@drawable/abc_btn_radio_material">
</ImageView>
<TextView
android:id="@+id/tv_text"
android:layout_toRightOf ="@+id/iv_image"
android:layout_
android:layout_
android:gravity="center" >
</TextView>
<TextView
android:id="@+id/tv_blah"
android:layout_
android:layout_
android:text="blah blah blah......"
android:layout_below="@+id/tv_text"
android:layout_toRightOf="@+id/iv_image"
android:layout_toEndOf="@+id/iv_image">
</TextView>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
build.gradle 依赖项:
dependencies
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:cardview-v7:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
结果:
【讨论】:
我的问题是我在活动中没有标签。我在每个片段中有标签。那么我怎样才能达到这样的效果呢? 这是绝妙的解决方案。我有一点不同的要求,我想使用与您提供的相同布局的底部导航。每个底部导航片段都需要有这样的带有选项卡的视图分页器,但主操作栏应保持在根级别。有实现它的想法吗? 解决方案非常好。但它错过了工具栏快照(我不确定在创建这篇文章后是否在 WhatsApp 中添加了该特定功能),这可以通过在工具栏滚动标志中添加snap
标志来实现。
@Abhi 谢谢,我看了一下编辑,看起来不错,我继续批准它。
@DanielNugent 请也回答这个问题***.com/q/61105979/8663316【参考方案2】:
好的,为了将listview/recyclerview
的滚动事件传递给appbarlayout
,您需要在包含滚动视图的framelayout/relativelayout/linearlayout
中放置以下标志,即listview/recyclerview
:
app:layout_behavior="@string/appbar_scrolling_view_behavior"
例如,在你的情况下应该是这样的:
<android.support.design.widget.CoordinatorLayout ....>
<android.support.design.widget.AppBarLayout ...>
<android.support.design.widget.CollapsingToolbarLayout
app:layout_scrollFlags="scroll|exitUntilCollapsed" ..>
<android.support.design.widget.TabLayout
app:layout_scrollFlags="scroll|enterAlways" ../>
<android.support.v7.widget.Toolbar >
..........
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
app:layout_behavior="@string/appbar_scrolling_view_behavior"
....>
// Frame containing your listview/recyclerview/ scrollingview
<FrameLayout>
</android.support.design.widget.CoordinatorLayout >
【讨论】:
【参考方案3】:This library will help you
这是一个非常简单的 Android 库,允许您将标题粘贴到可滚动视图并轻松对其应用动画
编辑: 要使用 StikkyHeader 库,您只需要 3 行:
StikkyHeaderBuilder.stickTo(mListView)
.setHeader(R.id.header, containerLayout)
.minHeightHeader(250)
.build();
例子:
public class IconAnimator extends HeaderStikkyAnimator
@Override
public AnimatorBuilder getAnimatorBuilder()
View viewToAnimate = getHeader().findViewById(R.id.icon);
Point point = new Point(50,100) // translate to the point with coordinate (50,100);
float scaleX = 0.5f //scale to the 50%
float scaleY = 0.5f //scale to the 50%
float fade = 0.2f // 20% fade
AnimatorBuilder animatorBuilder = AnimatorBuilder.create()
.applyScale(viewToAnimate, scaleX, scaleY)
.applyTranslation(viewToAnimate, point)
.applyFade(viewToAnimate, fade);
return animatorBuilder;
然后将动画器设置为StikkyHeader:
StikkyHeaderBuilder.stickTo(mListView)
.setHeader(R.id.header, containerLayout)
.minHeightHeader(250)
.animator(new IconAnimator())
.build();
【讨论】:
【参考方案4】:正是你想要的(复制粘贴即可)
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:id="@+id/root_layout"
android:layout_
android:layout_
tools:context="com.arkatechnolabs.designpractice.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_
android:id="@+id/appbarLayout"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsibleToolBarLayout"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:expandedTitleMarginStart="10dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_
android:layout_
android:layout_gravity="bottom"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.design.widget.TabLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_
android:layout_
android:id="@+id/title_layout"
android:visibility="gone">
<TextView
android:layout_
android:layout_
android:layout_centerVertical="true"
android:id="@+id/toolbarText"
/>
<TextView
android:layout_
android:layout_
android:id="@+id/send_button"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:text="Send Text"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold"
android:visibility="gone"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_btn"
android:layout_gravity="right|bottom"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_
android:layout_
android:src="@drawable/ic_fab"
app:fabSize="normal">
</android.support.design.widget.FloatingActionButton>
</android.support.design.widget.CoordinatorLayout>
【讨论】:
以上是关于Whatsapp 喜欢折叠工具栏的主要内容,如果未能解决你的问题,请参考以下文章
为啥 chrome 开发者工具不显示 WhatsApp 网络应用请求?
java 折叠工具栏在“折叠”工具栏中添加viewFlipper
工具栏折叠时如何使用动画调整搜索视图宽度 [ANDROID]