使Linearlayout可滚动而不使用滚动视图

问题描述:

我有一个Linearlayout,我想让它滚动而不使用ScrollView。可能吗。任何建议将不胜感激。这里是详细信息: 如果我使用ScrollView封装LinearLayout,但是当我在LinearLayout中使用ListView(因为它是我的客户端要求)时,它表示不在ScrollView中使用ListView。我必须使用ListView显示50个产品列表,并且必须将此ListView放入LinearLayout中,同时整个布局都可以滚动。可能吗。这里是骨架:使Linearlayout可滚动而不使用滚动视图

<LinearLayout> 
    <RelativeLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <TextView> 
    </LinearLayout> 
    <LinearLayout> 
     <ListView> 
    </LinearLayout> 
    </RelativeLayout> 
</LinearLayout> 

重要提示:请参阅列表视图,我想添加50列表项目。那么我怎样才能让这个总的LinearLayout可以滚动。

+1

为什么你不想使用ScrollView? – Daniel 2013-03-05 14:58:06

+0

我在Linearlayout周围使用了ScrollView,但是当我在LinearLayout中添加一个ListView时,ListView不起作用。这是真正的原因。所以我想在不使用ScrollView的情况下制作LinearLayout。可能吗。 – androidcodehunter 2013-03-05 15:04:34

+0

我会建议你编辑你的代码,所以堆栈溢出可以帮助你修复ListView停止使用ScrollView这是最好的选择。 – Daniel 2013-03-05 15:11:33

其实做一些研究后,我想出了一个解决这个问题:

起初我想在一个非常简单的方式来解释这个问题。

  1. LinearLayout将可滚动。要做到这一点,我们可以使用ScrollView,但有时我们需要在LinearLayout中使用ListView。
  2. 我们知道,里面滚动型,我们不能用另一个滚动视图像的ListView

如何解决呢?

ListView是固有的可滚动的,所以我们可以在ListView中添加页眉和页脚。作为一个结论:

  1. 创建布局header.xml和footer.xml和list.xml
  2. 查找list.xml的主要活动ListView控件参考,并动态地在ListView引用添加页眉和页脚。

如果你只在线性布局内使用listview,那么你不需要使用scrollview。因为ListView默认是可滚动的。但是如果你还有其他组件,那么你可以在另一个滚动视图中分开它们。确保ScrollView只使用一个直接的子布局。 下面是一个示例代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#ffffff"> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:divider="#b5b5b5" 
     android:dividerHeight="1dp" 
     android:cacheColorHint="#00000000"/> 

</LinearLayout> 
+0

其实我必须在ListView中添加50个产品列表,并且listView将放入LinearLayout中。我做到了,但问题是当项目列表超过设备大小时,LinearLayout没有展开,但我需要展开LinearLayout作为ListView展开。 – androidcodehunter 2013-03-05 15:33:56

+0

你能否在这里粘贴你的xml文件和代码。 – manishpro 2013-03-05 15:36:31

+0

我添加了我的xml代码。 – androidcodehunter 2013-03-06 08:33:18