工具栏未按预期响应滚动事件

时间:2017-03-13 11:59:57

标签: android android-layout listview android-toolbar android-coordinatorlayout

我有一个包含自定义工具栏和简单列表视图的活动。滚动列表视图时,我希望工具栏响应。但是,它不起作用。 我还注意到,当我触摸工具栏并向上/向下移动时,工具栏会移动。

这是.xml文件

<input type="text"  percentage-formatter ng-model="tea">

这是.java文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.abdralabs.talksee.HistoryActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/history_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/history_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/title"
            app:layout_scrollFlags="scroll|enterAlways">

        </android.support.v7.widget.Toolbar>

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


    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv_history"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </ListView>

styles.xml文件

public class HistoryActivity extends AppCompatActivity {
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        toolbar = (Toolbar)findViewById(R.id.history_toolbar);
        /*
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        */
        toolbar.setTitle("History");
        String[] rcArray = {"A","B","C","D","A","B","C","D","A","B","C","D","A","B","C","D","A","B","C","D","A","B","C","D","A","B","C","D"};



        ArrayAdapter adapter = new ArrayAdapter<String>(this,
                R.layout.list_history, rcArray);

        ListView listView = (ListView) findViewById(R.id.lv_history);
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        /*
        switch (item.getItemId()){
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                break;
        }
        */
        return super.onOptionsItemSelected(item);
    }
}

v21 \ styles.xml文件

<resources>

<!-- Base application theme. -->
<style name="AppTheme"
    parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay"
    parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay"
    parent="ThemeOverlay.AppCompat.Light" />

</resources>

这是清单

<resources>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    </style>
</resources>

编辑: 为了回应CodePlay的回答,我已按照您的建议应用了更改,结果是只有第一个listview项目正在显示且不可滚动。我甚至将NestedScrollView的android:layout_height =“match_parent”更改为android:layout_height =“wrap_content”,但它无效。

I have attached the picture showing how the ui looks after applying the changes

1 个答案:

答案 0 :(得分:0)

更新:您还需要将ListView包装在android.support.v4.widget.NestedScrollView中。

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv_history"/>

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

您需要将 CollapsingToolbarLayout 包装在AppBarLayout中的工具栏中,以响应ListView的滚动。

  

CollapsingToolbarLayout是工具栏的包装器,它实现了折叠应用栏。它被设计为用作AppBarLayout的直接子项。

xml中的AppBarLayout示例应该类似于以下代码:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Light"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"/>

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

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