3

I have a layout which has two ListViews and they are all inside a scrollview because the contents can exceed the device's height. Here is the layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.galleri5.android.activities.ImageActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:elevation="4dp"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#FAC80A">

        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Image"
                android:textColor="#000000"
                android:textSize="17sp"
                android:layout_centerVertical="true"
                android:id="@+id/textView80" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView134"
                android:src="@drawable/gallery_page_ic_notifications"
                android:layout_alignParentEnd="true"
                android:layout_marginRight="30dp"
                android:layout_centerVertical="true" />

        </RelativeLayout>

    </android.support.v7.widget.Toolbar>

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_below="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/user_profile_bg"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView102"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="25dp"
                android:layout_marginTop="10dp"
                android:adjustViewBounds="true" />

            <ImageView
                android:id="@+id/imageView103"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="25dp"
                android:src="@drawable/user_profile_activity_1_shape_thin_line_separator" />

            <TextView
                android:id="@+id/textView38"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:alpha="0.8"
                android:inputType="textMultiLine"
                android:textColor="#FFFFFF" />

            <TextView
                android:id="@+id/textView39"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="25dp"
                android:layout_marginLeft="30dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#707070" />

            <ImageView
                android:id="@+id/imageView104"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="25dp"
                android:src="@drawable/user_profile_activity_1_shape_thin_line_separator" />

            <LinearLayout
                android:id="@+id/linearLayout3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:orientation="horizontal">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/imageView107"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:paddingRight="5dp" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@+id/imageView107"
                        android:orientation="vertical"
                        android:paddingLeft="5dp">

                        <TextView
                            android:id="@+id/textView45"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:alpha="0.8"
                            android:paddingBottom="2dp"
                            android:textColor="#FFFFFF" />

                        <TextView
                            android:id="@+id/textView46"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:paddingTop="2dp"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:textColor="#707070"
                            android:textSize="12sp" />
                    </LinearLayout>

                    <ImageView
                        android:id="@+id/imageView108"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentEnd="true"
                        android:layout_centerVertical="true"
                        android:src="@drawable/image_page_button_bookmark" />
                </RelativeLayout>
            </LinearLayout>

            <ImageView
                android:id="@+id/imageView109"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="10dp"
                android:src="@drawable/user_profile_activity_1_shape_thin_line_separator" />

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="35dp"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#343031"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/textView47"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="25dp"
                        android:layout_marginTop="15dp"
                        android:text="This Image is Part Of..."
                        android:textColor="#707070" />

                    <ImageView
                        android:id="@+id/imageView110"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginBottom="20dp"
                        android:layout_marginTop="10dp"
                        android:src="@drawable/user_profile_activity_1_shape_thin_line_separator" />

                    <ListView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/listView2" />

                    <TextView
                        android:id="@+id/textView57"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="20dp"
                        android:layout_marginLeft="30dp"
                        android:text="See All Galleries"
                        android:textColor="#707070" />

                </LinearLayout>


            </android.support.v7.widget.CardView>

            <ImageView
                android:id="@+id/imageView125"
                android:layout_width="305dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginBottom="20dp"
                android:src="@drawable/image_page_belongs_to_shape_line_separator_1" />

            <ListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/listView6" />

            <EditText
                android:id="@+id/editText6"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:alpha="0.8"
                android:inputType="text"
                android:elegantTextHeight="true"
                android:enabled="true"
                android:hint="Comment"
                android:textCursorDrawable="@null"
                android:textColor="#FFFFFF"
                android:textColorHint="#FFFFFF"
                android:layout_marginTop="10dp"
                android:backgroundTint="#fac80a" />

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

The two listviews have id listView2 and listView6. I have set up the adapters for both the listview properly. I am getting the data from backend. For both the listviews, I am getting two items from backend but actually it is showing just one item in each listView. I am also trying to add item to second listView, using notifyDataSetChanged() but that is not working too. I am getting only one item. What could be the possible reason and how to fix it?

Amit Tiwari
  • 3,684
  • 6
  • 33
  • 75
  • listview can scroll itself , when you use it in scrollview it cause problems – Pirisok Oct 15 '15 at 06:58
  • A choice is to use auto list view expand as its child element height. So you will be able use scrollview, and list view height will be equal to its child height so no scroll will be needed. Here is link 2nd answer by DougW [link](http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing)....other option is to use NestedScroll with Recyclerview. Example i will have to find or write it and let you know. – shreyash mashru Oct 15 '15 at 07:30
  • My suggestion is to use [RecyclerView](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html) with multiple view types. Have a look at the following [gist](https://gist.github.com/hister/d56c00fb5fd2dfaf279b). – Mahendran Sakkarai Oct 15 '15 at 07:33
  • @shreyashmashru Thanks. That worked. – Amit Tiwari Oct 15 '15 at 07:54

4 Answers4

1

Here is what I did, in case someone else is having similar problem:

Note: This solution is very inefficient. I am using it because I have at max 2 items in my listView. I would not recommend it to others if you have a listView that has some significant number of items.

public static void setListViewHeightBasedOnChildren(ListView listView) {
              ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
            // pre-condition
                  return;
            }

            int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
            for (int i = 0; i < listAdapter.getCount(); i++) {
                 View listItem = listAdapter.getView(i, null, listView);
                 if (listItem instanceof ViewGroup) {
                    listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                 }
                 listItem.measure(0, 0);
                 totalHeight += listItem.getMeasuredHeight();
            }

            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
                      listView.setLayoutParams(params);
        }

I called this function on both listViews after setting up the adapter. Hope it helps.

Amit Tiwari
  • 3,684
  • 6
  • 33
  • 75
  • you have no idea how wrong your solution is.. it can easily cause so many problems regarding cpu, memory etc.. what if you have 1000 items in that listview? you load the list to load 1000 items? that is so wrong.. – JozeRi Oct 15 '15 at 08:02
  • I know this method is not very efficient. But I am showing only 2 items in the list at max. Therefore I went ahead with this method. – Amit Tiwari Oct 15 '15 at 08:14
  • maybe you personally is using 2 items at max.. but you wrote it yourself - "Here is what I did, in case someone else is having similar problem".. others might use this code, and rely on it. and those others might have more then 2 items(perhaps a lot more then 2), and they may think that this code is a good solution, but i don't think it is. please, at least edit your answer so other coders won't get confused. ty :) – JozeRi Oct 15 '15 at 08:21
1

As i said in comment instead of using listview inside scrollview use recyclerview. I just created a demo project to make you understood about the recyclerview with multiple layouts. Import the project and have a look it to that.

Same way you crop the current layout into multiple layouts and inflate them based on your needs and implement it in recyclerview.

Mahendran Sakkarai
  • 8,381
  • 6
  • 44
  • 66
1

Another possibility would be to use one ListView and 2-.. different adapters and many different Views. And for ListViews there is https://github.com/commonsguy/cwac-merge. Adds many adapters and views in one big adapter, that you can add to 1 ListView.

Text from the page:

Simply create a MergeAdapter and call addAdapter(), addView(), or addViews() (latter accepting a List), then attach your adapter to the ListView.

Gradle:

repositories {
    maven {
        url "https://s3.amazonaws.com/repo.commonsware.com"
    }
}

dependencies {
    compile 'com.commonsware.cwac:merge:1.1.+'
}
Mikelis Kaneps
  • 4,576
  • 2
  • 34
  • 48
0

It is not recommended have a listview inside a scrollview. Although the list shows all the items, scroll experience to the list would not be good. You should restructure your layout to avoid this situation.

Hope it helps you!!

  • Right now, the contents of my listviews occupy around 30% of bottom screen height. If I don't put scrollview, I will only be able to do scroll in the bottom 30% of the screen. I want the contents above the listview to scroll too, if the contents have combined height greater than that of the device. – Amit Tiwari Oct 15 '15 at 07:10
  • I understand you, Can you change your screen logic? For example show the list in a dialog or a new screen when you pressed a button like "show the list"... – Alejandro Martínez Martínez Oct 15 '15 at 07:15
  • I can't. It has been made by a designer and I have to implement that. I also tried nested scroll view, but that is not working too. – Amit Tiwari Oct 15 '15 at 07:17