Fragment中的Android ListView不滚动

时间:2017-06-06 21:16:51

标签: android listview android-fragments

我的MainActivity片段中有一个listView,由自定义ArrayAdapter呈现。问题是列表视图由于某种原因无法滚动。

我的XML MainActivity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:screenOrientation="portrait"
tools:context=".MainActivity">

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#f1f1f1">

</FrameLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    design:menu="@menu/navigation" />
</LinearLayout>

我的XML片段:

<android.support.constraint.ConstraintLayout 
    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:screenOrientation="portrait"
    tools:context=".Friends">


<Button
    android:id="@+id/newfriendbutton"
    android:layout_width="0dp"
    android:layout_height="66dp"
    android:text="@string/new_friend"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="395dp"
    android:layout_height="wrap_content"
    android:text="@string/instruct"
    android:textAlignment="center"
    app:layout_constraintTop_toBottomOf="@+id/newfriendbutton"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="8dp" />

<ListView
    android:id="@+id/friendListView"
    android:layout_width="match_parent"
    android:layout_height="592dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView6"
    tools:ignore="MissingConstraints"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1" />
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:1)

你确定ListView已满吗?如果视图中有更多选项,则只允许滚动,而不是适合给定约束的选项。在这种情况下,最好使用RelativeLayout。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:screenOrientation="portrait"
    tools:context=".Friends">


    <Button
        android:id="@+id/newfriendbutton"
        android:layout_height="66dp"
        android:text="@string/new_friend"
        android:layout_width="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/instruct"
        android:textAlignment="center"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/newfriendbutton"/>

    <ListView
        android:id="@+id/friendListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView6"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"/>
</RelativeLayout>