设备解锁后无法滚动-CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout

时间:2018-11-09 13:47:07

标签: java android

我的页面带有可折叠的标题。锁定和解锁设备后,无法滚动标题(有时会在我转到另一个片段然后再次返回时发生)。我只能通过触摸正文(而不是标题)来滚动。

感谢您的回答。

这是我的代码:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|enterAlwaysCollapsed">

            <include
                android:id="@+id/event_detail_header"
                layout="@layout/view_event_detail_header"
                app:item="@{item}" />

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            style="@style/AppTabLayout"
            android:layout_width="match_parent" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

1 个答案:

答案 0 :(得分:2)

通过模拟点击来固定。

    View nestedScrollView = view.findViewById(R.id.nested_scroll_view);
    if (nestedScrollView != null) {
        float posX = 0.0f;
        float posY = 0.0f;
        int state = 0;

        // Prepare and perform fake action down
        long downTime = SystemClock.uptimeMillis();
        long eventTime = SystemClock.uptimeMillis() + 100;
        int eventType = MotionEvent.ACTION_DOWN;
        MotionEvent motionEvent = MotionEvent.obtain(downTime, eventTime, eventType, posX, posY, state);
        nestedScrollView.dispatchTouchEvent(motionEvent);

        // Prepare and perform fake action up
        downTime = SystemClock.uptimeMillis();
        eventTime = SystemClock.uptimeMillis() + 100;
        eventType = MotionEvent.ACTION_DOWN;
        motionEvent = MotionEvent.obtain(downTime, eventTime, eventType, posX, posY, state);
        nestedScrollView.dispatchTouchEvent(motionEvent);
    }