2012-12-04 101 views
4

我有一個包含14 fields.Something像這樣的自定義列表視圖:滾動整個列表查看水平

字段1場2場3字段4字段5字段6字段7字段8字段9位10 field11 field12 field13 field14。 現在顯而易見的問題是,我無法適應屏幕上的所有字段,所以我想讓整個列表視圖水平滾動,所以我只能顯示5-6個字段,並且當用戶水平滾動時他將能夠看到其他人。 我可以有一個列表視圖,既可以垂直滾動也可以水平滾動?

回答

8

簡單的橫向卷軸視圖內創建列表視圖,骨架如下

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

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

    </LinearLayout> 
</HorizontalScrollView> 

在這裏,如果你需要證明的非滾動能夠標題列表視圖,然後在不同的佈局中提到像,並將其添加不要忘記設置layout_weight屬性。

0

您應該有一個Horizo​​ntalScrollView,該Horizo​​ntalScrollView應該是此link中給出的VerticalScrollView的子級。

0

實現水平listview的最簡單和容易的方法。

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); 
    } 
1

列表視圖已經支持垂直滾動,所以我們需要支持水平滾動。下面的代碼對我來說工作得非常好。 請注意,即使「weight is 1」,也不要在列表視圖中保留「height as 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>