在我的活动中,我有带标记的地图。当用户单击标记时,它将显示该位置的详细信息和底页。
当视图第一次填充了关于place的数据时,它可以正常工作。但在隐藏了底页,然后再次单击后显示它后,它无法正常工作:
textView保存旧高度(view.invalidate()不起作用)。
viewGroup.addView方法不起作用(在添加之前调用viewGroup.removeAllViews(),但它会保持原来的高度,添加视图也不起作用)。
但是,当我在不隐藏/显示底页的情况下更改视图数据时,所有事情都正常工作。就像第一次换衣服一样。
在调试期间,我还看到了其他一些奇怪的行为:我有一个linearlayout,它有4个relativelayout(内部有textview和imageview)。当我改变1/2的可见性(消失/可见)时,它们会相互重叠,即使线性布局具有垂直方向。但并非总是如此,就像上面所说:第一次工作正常,其他时候,如果我在底页不可见时更改视图,则无法正常工作(如前所述),但当底行为可见时,如果我更改视图,则工作正常(如第一次)。
如果我没搞错的话,那是因为床底。因为,我以前在android上看不到这样的视图行为。
我的代码:

mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView);
    mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            switch (newState) {
                case BottomSheetBehavior.STATE_HIDDEN:
                    hidePlaceDetails();
                    break;
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        }
    });

/**
 * Show bottom sheet with given place details and start load route
 *
 * @param place the place
 */
private void showPlaceDetails(Place place) {
    placeTitleView.setText(place.getTitle());
    placeCategoryView.setText(place.getParentTitle());

    placeInfoGroup.removeAllViews();
    addPlaceInfo(R.drawable.ic_info, place.getDescription(), null);
    addPlaceInfo(R.drawable.ic_place, place.getAddress(), null);
    addPlaceInfo(R.drawable.ic_phone, place.getPhoneNumbers(), null);
    addPlaceInfo(R.drawable.ic_language, place.getWebsite(), null);

    Photo photo = place.getPhoto();
    if (U.isEmpty(photo.getImagePath())) {
        U.hideView(placePhotoView);
    } else {
        U.showView(placePhotoView);
        int proportionalHeight = U.calculateProportionalHeight(
            screenSize[0] /*screenWidth*/,
            photo.getWidth(),
            photo.getHeight(),
            800 /*maxHeight*/);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            proportionalHeight
        );

        placePhotoView.setLayoutParams(layoutParams);
        ImageUtils.networkImage(this, photo, placePhotoView, null, U.imagePlaceholder());
    }

    U.view(placeDistanceProgressView, true); //showView
    U.view(placeDistanceView, false); //hideView
    if (mMap.getMyLocation() != null) {
        Waypoint myLocation = new Waypoint(mMap.getMyLocation().getLongitude(), mMap.getMyLocation().getLatitude());
        Waypoint destination = new Waypoint(place.getPosition().getLongitude(), place.getPosition().getLatitude());

        getAndDrawRoute(place, myLocation, destination);
    }

    int thirdScreen = screenSize[1] / 3;
    mMap.setPadding(0, 0, 0, halfScreen);
    isShowingPlaceDetails = true;
    mBottomSheetBehavior.setPeekHeight(halfScreen);
    mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}

/**
 * Hide bottom sheet and add markers
 */
private void hidePlaceDetails() {
    mMap.setPadding(0, 0, 0, 0);
    mBottomSheetBehavior.setPeekHeight(0);
    mBottomSheetBehavior.setHideable(true);
    mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
    addMarkers();
    isShowingPlaceDetails = false;
}

/**
 * @param icon            icon res
 * @param title           title
 * @param onClickListener nullable
 */
private void addPlaceInfo(
    @DrawableRes int icon, String title, @Nullable View.OnClickListener onClickListener) {
    View root = LayoutInflater.from(this).inflate(R.layout.layout_map_bottom_sheet_row, null);
    ImageView iconView = ButterKnife.findById(root, R.id.place_icon);
    TextView titleView = ButterKnife.findById(root, R.id.place_title);
    iconView.setImageResource(icon);
    titleView.setText(U.notEmpty(title) ? title : Html.fromHtml(String.format("<i>%s</i>", "None")));

    if (onClickListener != null) {
        root.setOnClickListener(onClickListener);
    }
    placeInfoGroup.addView(root);
}

活动*.xml
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.CoordinatorLayout
    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">

    <include layout="@layout/toolbar_appbar"/>

    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        ...
    </RelativeLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/bottomSheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@android:color/white"
        android:elevation="4dp"
        app:behavior_peekHeight="0dp"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <include layout="@layout/layout_map_bottom_sheet"/>

    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

...

layout_map_bottom_sheet.xml布局图
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
android:paddingTop="@dimen/content.margin">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="@dimen/content.padding.big"
    android:paddingStart="@dimen/content.padding.big"
    android:paddingRight="@dimen/content.padding"
    android:paddingEnd="@dimen/content.padding">

    <TextView
        android:id="@+id/place.title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="@dimen/text.line.space"
        android:textColor="@color/text.color"
        android:textSize="@dimen/text.xlarge"
        tools:text="@string/lorem.place.title"/>

    <TextView
        android:id="@+id/place.category"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="@dimen/text.line.space"
        android:paddingBottom="@dimen/content.padding.small"
        android:paddingTop="@dimen/content.padding.small"
        android:textSize="@dimen/text.medium"
        android:textColor="@color/text.secondary.color"
        tools:text="@string/lorem.short"/>

    <include layout="@layout/divider"/>

    <TextView
        android:id="@+id/place.distance"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/content.padding.small"
        android:paddingTop="@dimen/content.padding.small"
        android:textColor="?attr/colorPrimary"
        android:visibility="gone"
        tools:text="@string/lorem.place.distance"/>

    <ProgressBar
        android:id="@+id/place.distance.progress"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginTop="@dimen/content.padding.small"
        android:layout_marginBottom="@dimen/content.padding.small"/>

    <include layout="@layout/divider"/>
</LinearLayout>

<LinearLayout
    android:id="@+id/place.infos"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
</LinearLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/place.photo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        tools:src="@drawable/sample"/>
</FrameLayout>

屏幕记录:https://drive.google.com/file/d/0B5up2CgQawSDbUZtQ2h1VDBWbms/view?usp=drivesdk
我很难解释,但这个问题让我疯狂了好几天。我真的很感谢你能提供任何帮助!

最佳答案

我用下面的解决方法解决了一个类似的问题。
在我的BottomSheetCallback中,我添加了以下内容:

behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
  @Override
  public void onStateChanged(final View bottomSheet, int newState) {
    switch (newState) {
      case BottomSheetBehavior.STATE_COLLAPSED:
      case BottomSheetBehavior.STATE_EXPANDED:
        bottomSheet.post(new Runnable() {
          @Override
          public void run() {
            bottomSheet.requestLayout(); // this seems to fix it.
          }
        });
        break;
      case BottomSheetBehavior.STATE_HIDDEN:
        // remove the views here with removeAllViews();
        break;
    }
  }

  @Override
  public void onSlide(View bottomSheet, float slideOffset) {

  }
});

它仍然不是完美的,因为新的布局需要一些时间来渲染,并且视图弹出时会有一些延迟。但总比什么都没有好。
另外,如果您已经支持target 24,那么在最新的支持库24.0.0中似乎有一个错误修复程序。
https://code.google.com/p/android/issues/detail?id=205226

07-24 09:49
查看更多