Android底部表单行为无法正常工作。视图在首次运行时不显示

时间:2016-06-14 21:16:51

标签: android

美好的一天,我有一个支持底部表格行为的应用程序。无论我尝试过什么,底部工作表都不会在第一次初始显示时显示其中的元素。第二次相同的代码触发,底部工作表显示元素。这两种情况都发生了:动态添加视图和列表视图。这是我为底部工作表构建的xml。

public void onSensorChanged(SensorEvent se) {

float degPitch = 0;
float degRoll = 0;
float degYaw = 0;
float radPitch = 0;
float radRoll = 0;
float radYaw = 0;

if (se.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
    mAccelerometerResult = se.values;
    Log.d("onSensorChanged", "Accelerometer: " + mAccelerometerResult.length);
}

if (se.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
    mMagneticFieldResult = se.values;
    Log.d("onSensorChanged", "Magnetic Field: " + mMagneticFieldResult.length);
}

if (mAccelerometerResult != null && mMagneticFieldResult != null) {

//~~~This is where the 3x3 matrix has changed to a 4x4.
    float[] rotation = new float[16];
    float[] inclination = new float[16];

    boolean rotationMatrixCheck = mSensorManager.getRotationMatrix(rotation, inclination, mAccelerometerResult, mMagneticFieldResult);

    if (rotationMatrixCheck) {

        float[] orientation = new float[3];

//~~~This is where the rotational matrix is remapped to be used in portrait mode.
        float[] remappedRotation = new float[16];
        SensorManager.remapCoordinateSystem(rotation, SensorManager.AXIS_X, SensorManager.AXIS_Z, remappedRotation);

        mSensorManager.getOrientation(rotation, orientation);


        radYaw = orientation[0];      //Yaw = Z axis
        radPitch = orientation[1];      //Pitch = X axis
        radRoll = orientation[2];      //Roll = Y axis

        degYaw = round((float) Math.toDegrees(radYaw), 2);
        degPitch = round((float)Math.toDegrees(radPitch), 2);
        degRoll = round((float)Math.toDegrees(radRoll), 2);


        if ((counter % 10) == 0) {
            //mYawTextView.setText(degYaw + "°");
            mPitchTextView.setText(degPitch + "°");
            mRollTextView.setText(degRoll + "°");
            counter = 0;
        } else {
            counter++;
        }
    }
}

正如您所看到的那样,listview被提交,所以我尝试了各种可能的方法,甚至用RELATIVELAYOUT或者什么来替换NESTEDSCROLLVIEW。这里是实际为linearLayout添加视图的代码。

<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">

<RelativeLayout 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">


    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="brijhelpline.testproject.MapsActivity" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:alpha="0.9"
        android:background="@drawable/gradient">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/settings"
                android:indeterminate="false"
                android:visibility="gone" />

            <ImageView
                android:id="@+id/userIcon"
                android:layout_width="38dp"
                android:layout_height="38dp"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="13dp"
                android:background="@drawable/user" />

            <ImageView
                android:id="@+id/settings"
                android:layout_width="38dp"
                android:layout_height="38dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="3dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/settings_icon" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="20dp"
                android:text="Fuel Buddy"
                android:textColor="#ffffff"
                android:textSize="20sp" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:background="@drawable/searc_layout">

        <ImageView
            android:id="@+id/marker"
            android:layout_width="20dp"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp"
            android:background="@drawable/marker_icon" />

        <ImageView
            android:id="@+id/add"
            android:layout_width="25dp"
            android:layout_height="26dp"
            android:layout_alignParentRight="true"
            android:layout_margin="10dp"
            android:background="@drawable/add_icon" />

        <EditText
            android:id="@+id/inputStation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_toLeftOf="@+id/add"
            android:layout_toRightOf="@+id/marker"
            android:hint="Поиск Заправки"
            android:maxLines="1" />
    </RelativeLayout>


</RelativeLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@android:color/white"
    android:clipToPadding="true"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <LinearLayout
        android:id="@+id/layoutToAdd"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    </LinearLayout>
    <!--<ListView-->
    <!--android:id="@+id/listView"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="match_parent"></ListView>-->
</android.support.v4.widget.NestedScrollView>

我完全不知道什么是问题以及该怎么做...我真的很讨厌我,所以如果有人可以帮助我,我会非常感激。

4 个答案:

答案 0 :(得分:12)

到目前为止,我发现这是谷歌的一个错误,同时我找到了一个解决方法。无论何时你想进行实际的节目设置,无论是折叠还是扩展,只需发布​​一个新的runnable并进行如下设置:

  bottomSheet.post(new Runnable() {
                    @Override
                    public void run() {
                        mBottomSheetBehavior.setPeekHeight(200);
                        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                    }
                });

答案 1 :(得分:1)

这对我有用。

而不是设置

mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);

这样做

mBottomSheetBehavior.setState(mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN ? BottomSheetBehavior.STATE_COLLAPSED : BottomSheetBehavior.STATE_HIDDEN);

答案 2 :(得分:1)

我在崩溃底层时遇到了问题。我通过设置峰高来解决。

mBottomSheetBehavior.setPeekHeight(0);

如果你想要折叠和隐藏BottomSheet,你可以在初始化BottomSheetBehavior后添加这样的代码:

答案 3 :(得分:0)

此代码适用于我,就像在一些Pre-Lollipop设备上一样,底页不起作用

  ViewCompat.postOnAnimation(coordinator, new Runnable() {
            @Override
            public void run() {
                ViewCompat.postInvalidateOnAnimation(coordinator);
            }
        });

希望这有帮助