ListView under scrollview in Android

I am trying to put a ListView below a ScrollView in android. I tried putting them inside LineaLayout 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="fill_parent" > ... </ScrollView> <ListView android:id="@android:id/list" android:layout_height="fill_parent" android:layout_width="fill_parent" android:background="#FFF"/> </LinearLayout> 

and ListView not displayed. I also tried putting it in RelaviteLayout and still nothing. Can I somehow have a ListView under ScrollView ?

Just to add something. I don’t want to split the screen so that I have half with ScrollView and the other half with ListView . I want the user to scroll down the ScrollView , which is apparently larger than the screen size, and then the ListView should start

+4
source share
5 answers

Would I recommend you put the contents of the ScrollView as a HeaderView in the ListView, or do you obviously want to have two separate scrollable areas on the screen?

An example for placing scroll content in a list as a title (one 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 action layout will look 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="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 scrolling areas, you must work with the layout:

 <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> 
+10
source

Fill parent will not work for you, change it to wrap the content, and let layout_weight 1 also scroll the view, then it may work for you

0
source

It's usually not good to put a listview in a scrollview. but if necessary, you need to set the height of the list. Follow the link below to calculate the height of the list to set in scrollview.

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

Using this method, you can achieve what you want.

0
source

You must set the height of the scrollview content and its element for 'wrap content'. XML will look 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> 

It can help you.

0
source

I think this is close to what you are looking for. I give you this code, but I have to say that setting ListView to ScrollView is bad practice and you should not use this solution. You have to work on something more correct and without scrolling in scrolling.

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

Source: https://habr.com/ru/post/1497962/


All Articles