【问题标题】:ListView below scrollview in AndroidAndroid中滚动视图下方的ListView
【发布时间】:2013-08-20 18:40:20
【问题描述】:

我正在尝试在 android 中将 ListView 放在 ScrollView 下方。我试着把它们放在像这样的LineaLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/frameLayout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <ScrollView 
      android:id="@+id/marketDetailScrollView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
   ...
  </ScrollView>
  <ListView  android:id="@android:id/list" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    android:background="#FFF"/>
</LinearLayout>

并且ListView 没有显示。我还尝试将其放入 RelaviteLayout 中,但仍然没有。我可以以某种方式在ScrollView 下有一个ListView 吗?

只是添加一些东西。我不想分割我的屏幕,这样我就有一半是ScrollView,另一半是ListView。我希望用户向下滚动显然大于屏幕尺寸的ScrollView,然后应该启动ListView

【问题讨论】:

  • 修复滚动视图的高度.. like android:layout_height="100dip"
  • 你可以让listview将scrollview内的视图作为页眉或页脚添加到listview(没有scrollview)或指定一个固定高度的scroll view并在它下面添加listview
  • 在您的滚动视图中,android:layout_height="fill_parent" 表示您的滚动视图会将所有空间都放到底部,因此您的 ListView 没有更多空间了。
  • 您正在使用LinearLayout,并且您将您的ListView 放在ScrollView 下方,另外您将ScrollViewwidthheight 设置为fill_parent 所以@ 987654342@ 将覆盖并获得ListView 的显示,如果您希望您的ListView 出现,例如为您的ScrollView200dp 设置特定大小。希望这会有所帮助。

标签: android android-layout


【解决方案1】:

我建议您将 ScrollView 内容作为 HeaderView 放在 ListView 中,还是您明确希望在屏幕上有两个单独的可滚动区域?

将滚动视图的内容作为标题(单个可滚动区域)放在列表​​视图中的示例:

public void onCreate(Bundle s){
    setContentView(R.id.yourLayout);

    ListView listView = (ListView) findViewById(R.id.listView);

    // Set the adapter before the header, because older 
    // Android version may have problems if not

    listView.setAdapter(new YourAdapter());

    // Add the header
    View header = inflater.inflate(
            R.layout.you_layout_that_was_in_scrollview_before, null, false); 

    listView.addHeaderView(header);

}

活动的布局如下所示:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView  android:id="@+id/listView" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"
    android:background="#FFF"/>
</LinearLayout>

如果你想要两个可滚动的区域,你应该使用布局权重:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView 
  android:id="@+id/marketDetailScrollView"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1" >
  <!-- ScrollViewContent -->
</ScrollView>
<ListView  android:id="@android:id/list" 
android:layout_height="0dp" 
android:layout_width="match_parent"
android:background="#FFF"
android:layout_weight="1"/>
</LinearLayout>

【讨论】:

  • 不,我没有。所以通过把 is 作为一个 headerview 只会创建一个可滚动的视图?
  • 是的......这将创建一个单一的可滚动区域......我将更新我的答案并添加一个示例
  • 我还有两个可滚动区域
  • 在 header 之前设置适配器会导致 android 崩溃。除了那部分,这很棒。
  • 这取决于 Android API。它已经随着 android 4.1 改变(我猜)。我不记得正确的方法是什么,但是如果在设置适配器后崩溃,则在将适配器设置为列表视图之前设置它,反之亦然。我想在将适配器设置为列表视图之前设置标题应该适用于所有 android 平台。
【解决方案2】:

Fill parent 不适合你, 将其更改为包装内容,并将 layout_weight 1 也用于滚动视图,那么它可能对您有用

【讨论】:

    【解决方案3】:

    通常情况下,将listview 放在scrollview 中是不好的。但是如果需要的话,你需要设置listview的高度。按照下面的链接计算listview的高度以适合scrollview。

    http://www.jiramot.info/android-listview-in-scrollview-problem

    使用这种方式你可以实现你想要的

    【讨论】:

    • 它不在滚动视图中,而是在滚动视图下
    • 哦,好吧。在这种情况下,只需在线性布局中使用权重属性并为滚动视图和列表视图设置权​​重。它应该工作
    【解决方案4】:

    您必须将滚动视图及其元素的内容高度设置为“包装内容”。 XML 会是这样的:

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ScrollView 
      android:id="@+id/marketDetailScrollView"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" >
    ...(height will be wrap_content for inner element as well)
    </ScrollView>
    <ListView  android:id="@android:id/list" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    android:background="#FFF"
    android:layout_weight="1"/>
    </LinearLayout>
    

    这可能会对你有所帮助..

    【讨论】:

    • 这可能是因为你的 ScrollView 的内部内容。你能告诉我你的scrollView的内部内容是什么吗?
    • 内部内容与此有什么关系?滚动视图比屏幕大
    • 检查任何内容的高度是否为fill_parent/match parent。我的代码工作正常。只需检查它是否正常工作。
    • 当您发布答案时会通知OP...无需在每次您回答时都在问题下发表评论...
    【解决方案5】:

    我认为这与您正在寻找的内容很接近。我给你这个代码,但我必须告诉你把ListView 放在ScrollView 中是一种不好的做法,你不应该使用这个解决方案。你应该做一些更正确的事情,并且在滚动视图中没有滚动视图。

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:android1="http://schemas.android.com/apk/res/android"
        android:id="@+id/containerScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:id="@+id/scrollViewContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <ScrollView
                android:id="@+id/marketDetailScrollView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
                <LinearLayout
                    android:id="@+id/scrollViewContainer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
    
                    <!-- the views contained in your ScrollView goes here -->
                </LinearLayout>
            </ScrollView>
    
            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FFF" />
        </LinearLayout>
    
    </ScrollView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      相关资源
      最近更新 更多