Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i have a ListView inside a NestedScrollView

I have created an app with a page that need to load content dynamically from web service. I want to have listview that can scroll together with a linear layout inside NestedScrollView. But when the contents is loaded to the listview, it doesn't stretch to its full height.

Here is my code.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.myquestionth.myquestionth10.Profile2Activity"
    tools:showIn="@layout/activity_profile2">

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

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

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="#BBBBBB" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Media heading"
                android:id="@+id/textView2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis."
                android:id="@+id/textView8" />

        </LinearLayout>

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#70bcf5" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

I have some searched about scrollview cannot be nested. This is an example i want according to my layout designing from Google play review page. What the method they use? Suggest me if i do something wrong. Many thanks.

enter image description here

Here is what i want.

enter image description here

like image 690
Marcus Avatar asked Feb 25 '16 17:02

Marcus


2 Answers

Well, I would suggest you 2 ways to solve that problem:

1) Try to make LinearLayout a header of your ListView. Note that header should be inflated as it is written here.

2) You mentioned that you use NestedScrollView, so maybe you should also try to replace ListView inside NestedScrollView with LinearLayout, as wise people suggested here, adding row views in loop similar to how your adapter works.

Good luck!

like image 140
Red Hot Chili Coder Avatar answered Sep 19 '22 06:09

Red Hot Chili Coder


on Lollipop onwards you can use

yourtListView.setNestedScrollingEnabled(true);

This enable or disable nested scrolling for this view if you need backwards compatibility with older version of the OS you'll have to use the RecyclerView.

like image 23
JackA Avatar answered Sep 20 '22 06:09

JackA