【问题标题】:Android ListView inside Fragment doesn't display all items. Last displayed Item is cutted ofFragment 中的 Android ListView 不显示所有项目。最后显示的项目被切割
【发布时间】:2018-10-27 11:25:20
【问题描述】:

我的应用程序目前存在错误。片段内的 ListView 不显示所有项目。截图如下:

Screenshot of the ListView

它应该数到 50。但它只数到 30(被删掉了)。

这是我的 ListView 项目行布局:

<?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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textView_listview_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/_16sdp"
            android:layout_marginTop="@dimen/_4sdp"
            android:text="TextView"
            android:textAlignment="center"
            android:textSize="@dimen/_18ssp"
            android:gravity="center"
            android:layout_gravity="center_vertical"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </RelativeLayout>
</RelativeLayout>

这是我的带有 listView 的片段:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GeneralFragment">

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

谢谢你:)

【问题讨论】:

  • 显示适配器代码
  • 设置外部ReleativeLayout高度为wrap_content

标签: android android-studio listview


【解决方案1】:

首先,你的片段布局应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GeneralFragment">

    <ListView
        android:id="@+id/listViewGeneral"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

你的行布局可以这样简化:

<?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="wrap_content">

    <TextView
        android:id="@+id/textView_listview_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/_16sdp"
        android:layout_marginTop="@dimen/_4sdp"
        android:text="TextView"
        android:textAlignment="center"
        android:textSize="@dimen/_18ssp"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>

但在我看来,最好定义项目行的确切高度,例如 48dp。在这种情况下,您的布局将更加可预测。

【讨论】:

    【解决方案2】:

    你可以修改如下:

    <ListView
            android:id="@+id/listViewGeneral"
            android:layout_width="match_parent"
            android:layout_height="match_parent" /> //notice this line
    

    【讨论】:

      猜你喜欢
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 2021-08-18
      • 1970-01-01
      相关资源
      最近更新 更多