【问题标题】:Scroll entire list View horizontally水平滚动整个列表视图
【发布时间】:2012-12-04 09:21:53
【问题描述】:

我有一个包含 14 个字段的自定义列表视图。像这样:

字段1字段2字段3字段4字段5字段6字段7字段8字段9字段10字段11字段12字段13字段14。 现在明显的问题是,我不可能将所有字段都放在屏幕上,所以我想让整个列表视图水平滚动,所以我只能显示 5-6 个字段,当用户水平滚动时,他将能够看到他们的其余部分。 我可以有一个垂直和水平滚动的列表视图吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    只需在水平滚动视图内创建列表视图,骨架如下

    <HorizontalScrollView ...........>
        <LinearLayout ......>
            <LinearLayout ......>
            //List View Titles will be here
            </LinearLayout>
    
            <ListView ........ android:layout_weight="1" />
    
        </LinearLayout>
    </HorizontalScrollView>
    

    在这里,如果您需要在列表视图中显示不可滚动的标题,请按照上述方法将其添加到单独的布局中,并且不要忘记设置 layout_weight 属性。

    【讨论】:

      【解决方案2】:

      您应该有一个 Horizo​​ntalScrollView,它应该是该 link 中给出的 VerticalScrollView 的子级。

      【讨论】:

        【解决方案3】:

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

        Horizo​​natalscroll.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>
        

        horizo​​ntal_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);
            }
        

        【讨论】:

          【解决方案4】:

          列表视图已经支持垂直滚动,所以我们需要支持水平滚动。下面的代码对我来说工作得很好。 请注意,即使“权重为 1”,也不要为列表视图保留“高度为 0dp”。因为 Horizo​​ntalScrollView 会将高度设为零,并且不会为列表视图绘制任何项目。

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-05-12
            • 1970-01-01
            • 2019-05-17
            • 2020-05-17
            • 2021-11-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多