Fragment中CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout复杂布局使用Immersionbar无法实现状态栏沉浸式。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:background="@color/black">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</androidx.viewpager.widget.ViewPager>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/center_appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toobar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="@color/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:collapsedTitleGravity="left"
app:expandedTitleMarginTop="185dp"
app:expandedTitleGravity="left"
app:expandedTitleMarginStart="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="@dimen/dp_300"
android:gravity="center"
android:scaleType="fitXY"
app:layout_scrollFlags="scroll"
android:src="@mipmap/icon_default_user"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="@dimen/dp_10">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="阿伟哈皮"
android:textColor="@color/white"
android:textSize="@dimen/sp_18"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1关注"
android:textSize="@dimen/sp_14"
android:textColor="@color/white"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1被关注"
android:layout_marginStart="@dimen/dp_10"
android:textSize="@dimen/sp_14"
android:textColor="@color/white"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="@dimen/dp_10">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+2"
android:layout_marginStart="@dimen/dp_20"
android:textColor="@color/red"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6人气"
android:textSize="@dimen/sp_14"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="21/170/60"
android:textSize="@dimen/sp_14"
android:textColor="@color/light_gray"
android:layout_marginTop="@dimen/dp_10"
android:drawablePadding="@dimen/dp_10"
android:drawableLeft="@mipmap/icon_white_user"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京"
android:textSize="@dimen/sp_14"
android:textColor="@color/light_gray"
android:layout_marginTop="@dimen/dp_10"
android:drawablePadding="@dimen/dp_10"
android:drawableLeft="@mipmap/icon_white_location"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你还没有填写个人介绍,点击添加"
android:textSize="@dimen/sp_14"
android:textColor="@color/light_gray"
android:layout_marginTop="@dimen/dp_10"
android:drawablePadding="@dimen/dp_10" />
</LinearLayout>
</LinearLayout>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_40"
app:layout_collapseMode="pin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/dp_30"
android:gravity="right">
<ImageView
android:id="@+id/img_right"
android:layout_width="@dimen/dp_24"
android:layout_height="@dimen/dp_32"
android:layout_gravity="right"
android:src="@mipmap/icon_list_setting" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
<com.flyco.tablayout.CommonTabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_40"
app:tl_iconVisible="false"
app:tl_textBold="SELECT"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:tl_indicator_width="@dimen/dp_70"
app:tl_indicator_color="@color/white"
app:tl_indicator_height="@dimen/dp_2"
app:tl_textsize="@dimen/sp_16"
app:tl_textSelectColor="@color/white"
app:tl_textUnselectColor="@color/white">
</com.flyco.tablayout.CommonTabLayout>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>
package com.example.gw.xlp.feature.mine.fragment;
import com.example.gw.xlp.R;
import com.example.gw.xlp.component.widget.TabEntity;
import com.example.gw.xlp.component.widget.base.BaseFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import java.util.ArrayList;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
public class MyPageFragment extends BaseFragment {
MyPagerAdapter mAdapter;
private final ArrayList<Fragment> mFragments = new ArrayList<>();
private final String[] mTitles = {
"动态", "私密", "喜欢"
};
@BindView(R.id.tablayout)
CommonTabLayout mTl3;
@BindView(R.id.vp)
ViewPager mVp;
@BindView(R.id.toolbar)
Toolbar mToolbar;
private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
public static MyPageFragment getInstance() {
MyPageFragment sf = new MyPageFragment();
return sf;
}
@Override
protected int getContentViewLayoutID() {
return R.layout.fragment_my;
}
@Override
protected void initViewAndData() {
mFragments.add(new DynamicFragment());
mFragments.add(new DynamicFragment());
mFragments.add(new DynamicFragment());
for (int i = 0; i < mTitles.length; i++) {
mTabEntities.add(new TabEntity(mTitles[i], 0, 0));
}
mTl3.setTabData(mTabEntities);
mTl3.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelect(int position) {
mVp.setCurrentItem(position);
}
@Override
public void onTabReselect(int position) {
}
});
mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mTl3.setCurrentTab(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
mVp.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
mVp.setCurrentItem(0);
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mTitles[position];
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
}
}