Android AppBarLayout overlaps listview

China☆狼群 提交于 2019-12-17 23:07:24

问题


I am writing a simple app to play with ContentProvider, I have a db, a ContentProvider, a main activity, a class that forwards commands to the ContentProvider using ContentResolver. On the gui I just want to display all items stored in the db. I created this project from scratch and when creating the Activity, the main layout had a CoordinatorLayout, with a AppBarLayout, and that is fine, I created a ListView and everything work except that the AppBarLayout overlaps the ListView, below the first item of the listview is hiding by the AppBarLayout.

I tried to use android:layout_below for my ListView but it does not work, if I use android:layout_marginTop then my ListView is under the AppBarLayout but I do not find this solution nice.

Is there no clean easy way to have ListView under the AppBarLayout?

below my activity_main layout:

<?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"
    android:background="@color/holo_light_background"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>


    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/bar"/>


    <android.support.design.widget.FloatingActionButton ...>

    <android.support.design.widget.FloatingActionButton ...>

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

回答1:


Simple add

app:layout_behavior="@string/appbar_scrolling_view_behavior"

to your ListView




回答2:


an alternative way is to use RecyclerView instead of ListView,it will work for sure........

OR

use:

      if  (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
          {
               listView.setNestedScrollingEnabled(true);
          }

It will obviously only work on Lollipop.



来源:https://stackoverflow.com/questions/32653767/android-appbarlayout-overlaps-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!