有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java安卓:固定位置工具栏

这是我的MainActivityxml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".views.activities.MainActivity">

    <include
        安卓:id="@+id/main_toolbar"
        安卓:layout_height="?attr/actionBarSize"
        安卓:layout_width="match_parent"
        安卓:layout_alignParentTop="true"
        layout="@layout/toolbar" />
    <fragment
        安卓:id="@+id/location_fragment"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_below="@id/main_toolbar"
        安卓:name="com.myapp.views.fragments.mainActivity.LocationSearchFragment"
        安卓:tag="fragment_location_search"
        tools:layout="@layout/fragment_location_search"
        class="com.myapp.views.fragments.mainActivity.LocationSearchFragment" />
    <fragment
        安卓:id="@+id/restaurants_list_fragment"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_below="@id/location_fragment"
        安卓:name="com.myapp.views.fragments.mainActivity.RestaurantFragment"
        安卓:tag="fragment_restaurant_list"
        tools:layout="@layout/fragment_restaurant_list"
        class="com.myapp.views.fragments.mainActivity.RestaurantFragment" />

</RelativeLayout>

当某些事件发生时(搜索完成并且有数据要显示),我希望向上滑动location_fragmentrestaurants_list_fragment,但保留自定义工具栏的位置。创建动画的代码如下所示:

public void onRestaurantsSearchCompleted(RestaurantSearchCompleted evt) {   
    View locationView = findViewById(R.id.location_fragment);
    View restaurantsView = findViewById(R.id.restaurants_list_fragment);
    RelativeLayout.LayoutParams restaurantsLayoutParams = new RelativeLayout.LayoutParams(restaurantsView.getWidth(), Defaults.restaurantsFragmentHeightAfterAnimation);
    float slideUp = -Defaults.locationFragmentHiddenPartHeight;

    locationView.animate()
            .translationYBy(slideUp);
    restaurantsView.animate()
            .translationYBy(slideUp);
    restaurantsView.setLayoutParams(restaurantsLayoutParams);
}

我现在正在努力解决的问题是,动画也会滑到我的自定义工具栏上。如何防止这种情况发生


共 (1) 个答案

  1. # 1 楼答案

    首先,我建议您使用CoordinatorLayout并将工具栏放在其中This是我在寻求实现类似目标时发现的最有用的资源之一,我希望它能有所帮助