简体   繁体   English

工具栏无法响应Listview滚动事件

[英]Toolbar not responding to Listview 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. 但是,它不起作用。

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

What have I done wrong? 我做错了什么?

EDIT: In response to rafsanahmad007's answer I have applied the changes as per your suggestion but still there is no change. 编辑: 为了回应rafsanahmad007的回答,我已根据您的建议应用了更改,但仍然没有变化。 When I scroll through the ListView there is no change to the size of the ToolBar. 当我滚动ListView时,ToolBar的大小没有变化。 However, when I click on the ToolBar itself and make an up & down motion the ToolBar moves up & down too. 但是,当我单击ToolBar本身并进行上下运动时,ToolBar也会上下移动。 What I want to achieve is, when I scroll the ListView downwards the ToolBar should collapse and when I scroll the ListView upwards the ToolBar should get to its normal size/position. 我想要实现的是,当我向下滚动ListView时,ToolBar应该崩溃,当我向上滚动ListView时,ToolBar应该达到它的正常大小/位置。

The following pictures depict how the ToolBar is responding currently. 以下图片描述了ToolBar当前的响应方式。 在此输入图像描述

在此输入图像描述

在此输入图像描述

As you can see in the above pics, the ListView is not being scrolled, but the ToolBar itself is being scrolled. 正如您在上面的图片中看到的那样,ListView没有滚动,但ToolBar本身正在滚动。 What I want is, when I scroll the ListView the ToolBar should scroll only during that time. 我想要的是,当我滚动ListView时,ToolBar应该只在那段时间滚动。 I hope I have clarified myself. 我希望我已经澄清了自己。

try this: 尝试这个:

 <android.support.design.widget.AppBarLayout
    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/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:minHeight="80dp"
        app:layout_scrollFlags="scroll|enterAlwaysCollapsed" />

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

you can also get Help from Here 你也可以从这里获得帮助

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

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