ListView与折叠工具栏重叠

时间:2018-12-23 11:52:44

标签: java android android-layout listview

我曾经用Android Studio中的Project-Template设置了一个折叠工具栏,并试图在NestedScrollView中实现ListView(请参见下面的代码)

我拥有最新的Android Studio版本,并且AVD在Android 9上运行

我已经尝试过

  1. 更改NestedScrollView的package com.erhein.cdroid.classdroid; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.erhein.cdroid.classdroid.models.G11S1; import java.util.ArrayList; public class StudentAdapter extends BaseAdapter { Activity context; ArrayList<G11S1> students; private static LayoutInflater inflater = null; public StudentAdapter(Activity context, ArrayList<G11S1> students) { this.context = context; this.students = students; inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return students.size(); } @Override public G11S1 getItem(int position) { return students.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View itemView = convertView; itemView = (itemView == null) ? inflater.inflate(R.layout.list_item, null): itemView; TextView textViewName = (TextView) itemView.findViewById(R.id.textViewName); TextView textViewAge = (TextView) itemView.findViewById(R.id.textViewAge); G11S1 selectedStudent = students.get(position); textViewName.setText(selectedStudent.Name); textViewAge.setText(selectedStudent.Age); return itemView; } }
  2. 实现我自己的ListView(Collapsing Toolbar with ListView
  3. 将ListView包装到LinearLayout中

我发现了一些使用android:fillViewport="true"的解决方案,我不想使用这种方法-如果这是最后一种解决方法,我将;)。 (这是针对学校项目的,我们应该只使用在学校 face palm 中学到的组件)

我将在下面跳过一些XML导入行:

RecyclerView

NestedListView

<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:fitsSystemWindows="true"
    tools:context=".MainScreen">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <fragment
        android:id="@+id/fragment1"
        android:name="net.mountdev.alcohollevel.EntryList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@drawable/ic_add_white_24dp" />

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

当前结果是,ListView位于ActionBar中,不应位于该位置(请参见屏幕截图)

ActionBar overlaps with List

2 个答案:

答案 0 :(得分:0)

android.support.design.widget.CoordinatorLayout更改为RelativeLayout

在您的AppBarLayout内部使用android:layout_alignParentTop="true"

为您的fragment添加android:layout_below="@+id/app_bar"

答案 1 :(得分:0)

问题出在fragment上。它占用了整个空间。尝试类似

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

        <android.support.v7.widget.Toolbar
                android:id="@+id/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>

<fragment
        android:layout_below="@+id/app_bar"
        android:id="@+id/fragment1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/app_bar"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="16dp"
        app:layout_anchorGravity="bottom|end"/>