【问题标题】:Vertical scroll for a Xamarin Android list viewXamarin Android 列表视图的垂直滚动
【发布时间】:2019-01-02 14:55:04
【问题描述】:

我正在尝试获取列表视图的垂直滚动,但始终显示所有列表视图项目,即使这意味着它们将离开手机的显示屏。

这是我一直在使用的代码:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px">
<GridLayout
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:rowCount="6"
    android:columnCount="1"
    android:orientation="vertical"
    android:id="@+id/myGridID">

    <!-- other controls like TextView, EditBox, other Grid layouts -->

    <ListView
         android:layout_row="4"
         android:layout_column="0"
         android:isScrollContainer="true"
         android:choiceMode="singleChoice"
         android:scrollbarSize="100px"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:id="@+id/myListId" />

    <!-- other controls like TextView, EditBox, other Grid layouts -->

</GridLayout>
</LinearLayout> 

我预计当列表项的高度超过 100 像素(大于 android:scrollbarSize 的值)时,垂直滚动将变为活动状态。这不会发生。

使用以下代码:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px">
<GridLayout
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:rowCount="6"
    android:columnCount="1"
    android:orientation="vertical"
    android:id="@+id/myGridID">

    <!-- other controls like TextView, EditBox, other Grid layouts -->

    <ScrollView 
      a:visibility="visible"
      xmlns:a="http://schemas.android.com/tools"
      android:scrollbars="vertical"
      android:visibility="visible"
      android:layout_height="150px"
      android:layout_row="4"
      android:layout_column="0">
        <ListView
          android:isScrollContainer="true"
          android:choiceMode="singleChoice"
          android:scrollbarSize="100px"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:id="@+id/myListId" />
    </ScrollView>

    <!-- other controls like TextView, EditBox, other Grid layouts -->

</GridLayout>
</LinearLayout>

因此,将列表视图放在 ScrollView 中会创建垂直滚动,但始终只显示一个列表项,所以在这里我也希望从列表视图中看到至少三个项目,然后滚动浏览其余物品。

如果可能的话,我仍然更喜欢通过仅使用列表视图将垂直滚动功能添加到我的应用程序,因为 ScrollView 在尝试从列表中获取孩子时似乎使事情变得复杂。

有人可以解释一下我在构建 android xamarin 列表视图的垂直滚动时做错了什么吗?

【问题讨论】:

  • 我阅读了建议的可能重复项,我认为我的问题是另外一回事,因为我试图避免在 ScrollView 中添加列表视图。我最好尝试在列表视图的垂直滚动中添加构建。另一个区别可能是我尝试通过使用 Android Xamarin 而不是本机 Java 代码来实现这一点,如果可能的话,只使用 xml 设计器代码。
  • 原生Android和xamarin android在XML方面没有区别

标签: android css xamarin xamarin.android


【解决方案1】:

默认情况下Listview有一个Scroll,将scrollview作为父级添加是一个不好的做法,

为了更好地了解事情是如何工作的,请查看以下帖子here

更新

去掉滚动条,改变listview代码如下:

  <ListView
    android:layout_width="match_parent"
    android:divider="#D3D3D3" //divider color 
    android:dividerHeight="1dip" // Height
    android:layout_height="wrap_content"
    android:id="@+id/myListId" 
    />

如果您也需要以下属性,请一一添加,看看更改这些是否会导致任何问题。

      android:isScrollContainer="true"
      android:choiceMode="singleChoice"
      android:scrollbarSize="100px"

【讨论】:

  • 感谢您的回答!我还认为列表视图的内置滚动应该可以工作并且应该使用,但我仍然看不到它。我可以添加垂直滚动的唯一方法是将列表视图放在滚动视图中。
  • 实际上,我建议您像我在上面发布的那样更改 XML 中的 listview 代码,并删除滚动视图并检查它是否适合您
  • 我尝试使用您的代码示例,但对我来说没有出现滚动。当您尝试时,您的列表视图是否具有相同的父布局,例如 GridLayout 和 LinearLayout?
  • 如果我错了,请纠正我,但您是想在列表的右侧添加滚动条以帮助滚动,还是您的列表视图无法正确滚动
  • 我正在尝试你说的第一件事,所以我需要一个垂直滚动条,它通常出现在列表的右侧,以防列表视图没有足够的“高度”直接显示列表中的所有项目。
猜你喜欢
  • 2012-07-31
  • 1970-01-01
  • 1970-01-01
  • 2013-07-18
  • 1970-01-01
  • 2012-11-19
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多