简体   繁体   English

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

[英]Toolbar not responding as expected to scroll events

I have an activity which contains a custom toolbar and a simple listview. 我有一个包含自定义工具栏和简单列表视图的活动。 I want the toolbar respond when the listview is scrolled. 我希望工具栏在列表视图滚动时做出响应。 However, it is not working. 但是,它不起作用。 I have also noticed that when I touch the toolbar and make an upward/downward movement the toolbar moves then. 我还注意到,当我触摸工具栏并向上/向下移动时,工具栏随之移动。

Here is the .xml file 这是.xml文件

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

Here is the .java file 这是.java文件

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);
    }
}

The styles.xml file 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>

The v21\\styles.xml file v21 \\ styles.xml文件

<resources>

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

Here is the manifest 这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abdralabs.talksee">

    <uses-feature
        android:name="android.hardware.microphone"
        android:required="false" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ts_icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />

        <activity android:name=".LauncherActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".RegisterActivity" />
        <activity android:name=".LoginActivity" />
        <activity
            android:name=".SwipeActivity"
            android:label="@string/title_activity_swipe"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".SearchingMatchActivity" />
        <activity android:name=".IMActivity" />

        <service
            android:name=".TalkSeeService"
            android:enabled="true"
            android:exported="true" />

        <activity android:name=".FriendsListActivity" />
        <activity android:name=".VideoCallEstablishedActivity" />
        <activity android:name=".IMListActivity" />
        <activity android:name=".CallEndedActivity" />
        <activity android:name=".HistoryActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".SwipeActivity"/>
        </activity>
        <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings" />
        <activity android:name=".QuickDemoActivity"></activity>
    </application>

</manifest>

EDIT: In response to CodePlay's answer, I have applied the changes as you have suggested and the outcome is such that only the first listview item is displaying and it is unscrollable. 编辑:作为对CodePlay的回答,我已经按照您的建议应用了更改,结果是仅显示第一个listview项,并且无法滚动。 I even changed the NestedScrollView's android:layout_height="match_parent" to android:layout_height="wrap_content" and yet it is not working. 我什至将NestedScrollView的android:layout_height =“ match_parent”更改为android:layout_height =“ wrap_content”,但它无法正常工作。

我已附上图片,显示在应用更改后ui的外观

Updated: you also need to wrap the ListView within a android.support.v4.widget.NestedScrollView. 更新:您还需要将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>

You need to have CollapsingToolbarLayout wrapping the toolbar inside AppBarLayout to respond to the ListView's scroll. 您需要使CollapsingToolbarLayout将工具栏包装在AppBarLayout中,以响应ListView的滚动。

CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. CollapsingToolbarLayout是Toolbar的包装,它实现了折叠的应用程序栏。 It is designed to be used as a direct child of a AppBarLayout. 它旨在用作AppBarLayout的直接子级。

An example of the AppBarLayout in your xml should be like the below code: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM