2016-05-17 52 views
-1

我有一个片段布局,也有一个ListView。我使用定制ArrayAdapter填充此ListView。现在,如果我插入更多项目,我无法滚动此列表。如何插入滚动条到列表视图?

所以我想知道如何插入一个垂直滚动条到ListView。

这是布局:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ScrollView01" 
    android:layout_width="800dp" 
    android:layout_height="fill_parent" > 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="830dp" 
    android:layout_height="fill_parent" 
    android:focusable="true" 
    android:focusableInTouchMode="true"> 


    <LinearLayout 
     android:id="@+id/layoutText" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:layout_below="@id/layoutDate"> 

     <View 
      android:layout_width="wrap_content" 
      android:layout_height="2dp" 
      android:background="@color/green" 
      android:layout_marginTop="15dp"/> 

     <!--LIST OF AGENT--> 
     <LinearLayout 
      android:id="@+id/layoutAgent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:layout_below="@id/layoutDate"> 

      <TextView 
       android:id="@+id/labelListAgent" 
       android:layout_width="150dp" 
       android:layout_height="wrap_content" 
       android:gravity="left|center" 
       android:paddingLeft="15dp" 
       android:paddingTop="15dp" 
       android:text="@string/agent"/> 

      <ListView 
       android:id="@+id/listOfAgent" 
       android:layout_width="240dp" 
       android:layout_height="120dp" 
       android:scrollbars="vertical" 
       android:layout_gravity="fill_vertical" 
       android:layout_alignParentTop="true"> 
      </ListView> 



     </LinearLayout> 



     <TextView android:id="@+id/labelAgent" 
      android:layout_width="350dp" 
      android:layout_height="wrap_content" 
      android:paddingTop="10dp" 
      android:gravity="left|center" 
      android:paddingLeft="15dp" 
      android:hint="Insert agent" 
      android:clickable="true" 
      /> 



    </LinearLayout> 

</RelativeLayout> 

</ScrollView> 

回答

0

添加到您的ListView的XML:

android:scrollbars="vertical" 

刚才检查你的代码。我认为你的问题可能是因为你给你的listview一个固定的高度。将android:layout_height="120dp"更改为

android:layout_height="wrap_content" 
+0

如果您阅读我的代码,此代码已存在 – bircastri

+0

我已尝试编写wrap_content而不是120dp但问题是相同 – bircastri

+0

ScrollView有什么特别的原因吗? – NezSpencer

0

默认情况下,listView上有一个scroollbar。 应设置高度为机器人:layout_height =“WRAP_CONTENT”,并删除了android:layout_gravity =“fill_vertical”

+0

我也删除这个标签,但问题是一样的:( – bircastri

+0

尝试下面的新的xml布局,我认为你的xml布局可能是这个问题 – Franklyn

0
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ScrollView01" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" > 
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:focusable="true" 
     android:focusableInTouchMode="true"> 


     <LinearLayout 
      android:id="@+id/layoutText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <View 
       android:layout_width="wrap_content" 
       android:layout_height="2dp" 
       android:background="@android:color/holo_green_dark" 
       android:layout_marginTop="15dp"/> 

      <!--LIST OF AGENT--> 
      <LinearLayout 
       android:id="@+id/layoutAgent" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:weightSum="2" 
       android:orientation="horizontal"> 

       <TextView 
        android:id="@+id/labelListAgent" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.5" 
        android:gravity="left|center" 
        android:paddingLeft="15dp" 
        android:paddingStart="15dp" 
        android:paddingEnd="15dp" 
        android:paddingTop="15dp" 
        android:text="agent"/> 

       <ListView 
        android:id="@+id/listOfAgent" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1.5"> 
       </ListView> 



      </LinearLayout> 



      <TextView android:id="@+id/labelAgent" 
       android:layout_width="350dp" 
       android:layout_height="wrap_content" 
       android:paddingTop="10dp" 
       android:paddingStart="15dp" 
       android:paddingEnd="15dp" 
       android:gravity="left|center" 
       android:paddingLeft="15dp" 
       android:hint="Insert agent" 
       android:clickable="true" 
       /> 

     </LinearLayout> 

    </RelativeLayout> 

</ScrollView> 
+0

惠特这个代码,不工作 – bircastri

0

当主活动中,有一个滚动型,其他Scrollar不工作。然后解决方案是删除我的布局文件顶部的滚动视图,并且ListView滚动条,工作