简体   繁体   English

在Android下面的scrollview下面的ListView

[英]ListView below scrollview in Android

I am trying to put a ListView below a ScrollView in android. 我试图将一个ListView放在Android下的ScrollView下面。 I tried putting them inside a LineaLayout like this 我尝试将它们放在像这样的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>

and the ListView doesn't get shown. 并且ListView不会显示。 I also tried putting it inside a RelaviteLayout and still nothing. 我也尝试将它放在RelaviteLayout ,但仍然没有。 Can I somehow have a ListView under a ScrollView ? 我可以在ScrollView下以某种方式拥有ListView吗?

Just to add something. 只是添加一些东西。 I don't want to split my screen so that I have a half with a ScrollView and another half with a ListView . 我不想分割我的屏幕,以便我有一半使用ScrollView ,另一半使用ListView I want the user to scroll down the ScrollView which apparently is bigger than the screen size and then the ListView should start 我希望用户向下ScrollView显然大于屏幕大小的ScrollView ,然后ListView应该启动

I would recommend you to put the ScrollView Content as HeaderView in the ListView or do you explicitly want to have two separated scrollable areas on screen? 我建议你把ScrollView内容作为HeaderView放在ListView中,或者你明确想要在屏幕上有两个独立的可滚动区域?

Example for putting the content of the scroll view in the list view as header (one single scrollable area): 将滚动视图的内容作为标题放入列表视图的示例(单个可滚动区域):

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

}

The layout of the activity would look like that: 活动的布局如下:

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

If you want two scrollable areas, you should work with layout weight: 如果您想要两个可滚动区域,则应使用布局权重:

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

Fill parent不会为你工作,更改它以包装内容,并给layout_weight 1也滚动视图,然后它可能适合你

Normally it is not good to put listview in scrollview. 通常将listview放在scrollview中是不好的。 but if in case it is required, you need to set the height of listview. 但如果需要,你需要设置listview的高度。 Follow the link below to calculate the height of listview for fitting in scrollview. 按照下面的链接计算listview的高度以适合scrollview。

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

Using this way you can achieve what you want 使用这种方式,您可以实现您想要的

You have to set the the height of the content of scrollview and its element to 'wrap content'. 您必须将scrollview及其元素的内容高度设置为“wrap content”。 XML will be like this: 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>

This will might help you.. 这可能会对你有所帮助..

I think this is near to what you are looking for. 我认为这接近你正在寻找的东西。 I give you this code code but i have to tell that putting a ListView in a ScrollView is a bad-practice and you should not use this solution. 我给你这个代码,但我必须告诉你把一个ListView放在一个ScrollView是一个坏习惯,你不应该使用这个解决方案。 You should work on something more correct and with no scroll view within scroll view. 您应该在滚动视图中处理更正确且没有滚动视图的内容。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM