整个列表视图横向滚动

5
我有一个包含14个字段的自定义列表视图。就像这样:
field1 field2 field3 field4 field5 field6 field7 field8 field9 field10 field11 field12 field13 field14。
现在显然的问题是我无法在屏幕上放置所有字段,所以我想使整个列表视图水平滚动,这样我只能显示5-6个字段,当用户水平滚动时,他将能够看到其余的字段。是否可以拥有一个既能垂直滚动又能水平滚动的列表视图?
4个回答

10

在水平滚动视图内创建一个列表视图,框架如下所示

<HorizontalScrollView ...........>
    <LinearLayout ......>
        <LinearLayout ......>
        //List View Titles will be here
        </LinearLayout>

        <ListView ........ android:layout_weight="1" />

    </LinearLayout>
</HorizontalScrollView>

如果您需要在列表视图中显示不可滚动的标题,则可以像下面提到的那样将其添加到单独的布局中,并且不要忘记设置 layout_weight 属性。


0

实现水平列表视图的最简单和最容易的方法。

Horizonatalscroll.xml

 <?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:background="@color/list_item_bg_color">

 </HorizontalScrollView>

horizontal_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/horizontal_outer_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:background="@drawable/rect_border_box"
android:orientation="vertical" >



    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
   </Linearlayout>

Java 代码:

      LinearLayout parent = (LinearLayout) getLayoutInflater().inflate(
            R.layout.observation_detail_parent_layout, null);
      Linearlayout child= null;


     //iterate views upto you adapater count.
      for(int i=0;i<14;i++){
      child = (LinearLayout) getLayoutInflater().inflate(
                    R.layout.horizontal_list, null);

        //set your views specified in horizontal_list.xml

      TextView text = (TextView) findViewById(R.id.text);
      text.setText("your text");
      parent.addView(child);
    }

0

你应该拥有一个HorizontalScrollView,它应该是一个VerticalScrollView的子项,就像这个link所示。


0

列表视图已经支持垂直滚动,所以我们需要支持水平滚动。以下代码对我来说完美地运行了。 请注意,即使“权重为1”,也不要将列表视图的“高度设置为0dp”。因为HorizontalScrollView会将高度设置为零,并且不会为列表视图绘制任何项目。

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/table_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="58dp"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="header"
            android:textColor="@android:color/black" />

        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <ListView
                android:id="@+id/mylist"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" >
            </ListView>
        </HorizontalScrollView>
    </LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接