我有一个ViewPager被添加到标题栏的底部。到达最后一页后,标题栏实际上缩小为Visibility.GONE,因此viewpager页面占据整个页面。这不是我需要帮助的部分,但是在布局文件中使用AnimateLayoutChanges = true,它会故障到另一个页面而不是停留在同一页面上。
切换器代码如下所示(称为onClick of tab,或on PageChange)
private void switchTab(Integer tabNumber){
if (tabNumber != currentTab) {
currentTab = tabNumber;
for (int x=0; x<tab_holder.getChildCount(); x++){
final int pos = x;
FacilityTab tab = tabs.get(pos);
if (x != tabNumber){
tab.setBackgroundColor(getResources().getColor(R.color.text_msg_title));
}else{
tab.setBackgroundColor(getResources().getColor(R.color.tab_bar_pressed));
}
}
try {
tab_controller.smoothScrollTo(tab_holder.getChildAt(tabNumber).getLeft() - 100, 0);
}catch (Exception e){
e.printStackTrace();
}
}
if (tabNumber == (tab_holder.getChildCount() - 1)){
tab_controller.setVisibility(View.GONE);
headerBar.setVisibility(View.GONE);
pager.setCurrentItem(tabNumber);
}else{
tab_controller.setVisibility(View.VISIBLE);
headerBar.setVisibility(View.VISIBLE);
}
}
XML看起来像:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="@color/background_color">
<TableRow
android:layout_width="match_parent"
android:layout_height="52dp"
android:background="@color/blue"
android:elevation="2dp"
android:id="@+id/header_bar"
android:weightSum="1"
android:visibility="visible">
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:src="@drawable/back_arrow"
android:id="@+id/back_button"
android:padding="18dp"
android:tint="@color/white" />
<ImageView
android:layout_width="100dp"
android:layout_height="match_parent"
android:id="@+id/imageView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/chat_small"
android:padding="8dp"
android:layout_marginRight="50dp" />
</TableRow>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/header_bar"
android:id="@+id/tab_controller"
android:gravity="center"
android:scrollbars="none"
android:background="@color/text_msg_title"
android:visibility="visible">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tab_holder">
</LinearLayout>
</HorizontalScrollView>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tab_controller" />
<RelativeLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:id="@+id/notification_view"
android:layout_alignTop="@+id/varbutton"
android:layout_marginTop="32dp"
android:background="@color/notification_green"
android:visibility="visible">
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/notification_bell"
android:padding="12dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Username"
android:id="@+id/notification_title"
android:layout_alignParentTop="true"
android:textColor="@color/white"
android:gravity="center_vertical"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/imageView"
android:textSize="24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="User's new message"
android:id="@+id/notification_detail"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/imageView"
android:layout_marginBottom="2dp"
android:textColor="@color/white"
android:textSize="16sp"
android:layout_below="@+id/notification_title"
android:gravity="center_vertical" />
</RelativeLayout>
<ImageButton
android:id="@+id/varbutton"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_gravity="bottom|right"
android:tint="@android:color/white"
android:elevation="2dp"
android:background="@drawable/floating_button"
android:layout_marginBottom="24dp"
android:layout_marginRight="24dp"
android:src="@drawable/msgicon"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:scaleType="fitCenter"
android:padding="16dp"/>
</RelativeLayout>
(为隐私而编辑的图片)