Android在ListView中滚动时隐藏ActionBar

时间:2015-07-08 00:24:34

标签: android listview android-actionbar

我正在寻找一种隐藏动作栏的方法,例如GooglePlay,向下滚动时让ActionBar在向上滚动时重新出现。我找到了很多指南,但它们是关于 recyclerview 的,我正在使用 listview

我找到的最佳指南是:https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling%28part3%29/

这是我的布局:

<?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"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">


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

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

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

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

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

这是我的错误:

Process: com.mycompany.test, PID: 6172
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.test/com.mycompany.test.activities.Friends_List_Activity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
                at android.app.ActivityThread.access$900(ActivityThread.java:147)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5254)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
                at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)
         Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
                at android.view.LayoutInflater.createView(LayoutInflater.java:633)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
                at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
                at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:684)
                at android.view.LayoutInflater.inflate(Unknown Source)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)
                at android.app.Activity.setContentView(Activity.java:2144)
                at com.mycompany.test.activities.Friends_List_Activity.onCreate(Friends_List_Activity.java:26)
                at android.app.Activity.performCreate(Activity.java:5933)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
                at android.app.ActivityThread.access$900(ActivityThread.java:147)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5254)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
                at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)

修改 Hiding the ActionBar on RecyclerView/ListView onScroll 在上面的指南我不能使用代码,我需要扩展Activity所以我无法使用getSupportActionBar()

EDIT 2.0

我的活动:

public class Contacts_List_Activity extends Activity {
    private SearchView mSearchView;
    private Contacts_List_Adapter_OBJ ContactsAdapter;
    private Menu menu;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_Contacts_list);

        ContactsAdapter = new Contacts_List_Adapter_OBJ(this);
        final ListView listView = (ListView) findViewById(R.id.Contacts);
        listView.setAdapter(ContactsAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        this.menu = menu;
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_Contact_list_screen, menu);

        mSearchView = (SearchView) menu.findItem(R.id.search).getActionView();

        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {

                Contacts_List_Adapter_OBJ FilterAdapter = ContactsAdapter;
                if (TextUtils.isEmpty(s)) {
                    FilterAdapter.getFilter().filter("");
                } else {
                    FilterAdapter.getFilter().filter(s.toLowerCase());
                }
                return true;
            }
        });
        mSearchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Set_MenuItem_Visible(false);
            }
        });
        mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                Set_MenuItem_Visible(true);
                return false;
            }
        });

        final MenuItem searchMI = menu.findItem(R.id.search);

        mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean queryTextFocused) {
                if (!queryTextFocused) {
                    searchMI.collapseActionView();
                }
            }
        });
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

这是我尝试使用xml布局并且它可以工作:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

您的案例中的问题似乎是基本活动。使用AppCompatActivity扩展您的Contacts_List_Activity

更新了布局 - 将app:layout_scrollFlags =“scroll | enterAlways”添加到工具栏和 app:layout_behavior =“@ string / appbar_scrolling_view_behavior”到listview

<?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"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">


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

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        app:layout_scrollFlags="scroll|enterAlways"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

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

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

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