ListView below scrollview in Android

后端 未结 5 889
予麋鹿
予麋鹿 2021-01-20 03:07

I am trying to put a ListView below a ScrollView in android. I tried putting them inside a LineaLayout like this



        
相关标签:
5条回答
  • 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?

    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>
    
    0 讨论(0)
  • 2021-01-20 03:47

    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. 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>
    
    0 讨论(0)
  • 2021-01-20 03:48

    Normally it is not good to put listview in scrollview. but if in case it is required, you need to set the height of listview. Follow the link below to calculate the height of listview for fitting in scrollview.

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

    Using this way you can achieve what you want

    0 讨论(0)
  • 2021-01-20 03:49

    Fill parent wont work for you, change it to wrap content, and give layout_weight 1 to scroll view also, then it may work for you

    0 讨论(0)
  • 2021-01-20 04:02

    You have to set the the height of the content of scrollview and its element to 'wrap content'. XML will be like this:

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

    0 讨论(0)
提交回复
热议问题