1

I dont know to add more listview inside one page layout (attach picture) listview inside one page layout

I want Each ListView is not scrolling. Page layout is scrolling only. Help me!! Build same home layout app: https://play.google.com/store/apps/details?id=com.google.android.youtube

Answer I had resolve this problem Step1: Make new file java:

public class ExpandableHeightListview extends ListView {
    boolean expanded = false;

    public ExpandableHeightListview(Context context)
    {
        super(context);
    }

    public ExpandableHeightListview(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ExpandableHeightListview(Context context, AttributeSet attrs,int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

Step2: Java: Replace all ListView ->ExpandableHeightListview, and set listview.setExpanded(true);

Step3: layout xml: Replace

<yourpackage.ExpandableHeightListview
                android:id="@+id/etResponse"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="300dp"
                android:scrollbars="none"
                android:orientation="vertical"
                android:fadingEdge="none">
            </yourpackage.ExpandableHeightListview>

Step4: Done!

Sỹ Phạm
  • 530
  • 6
  • 16
  • Here you don't need multiple list views. You need to implement a single `ListView` handling multiple view types. Your adapter should implement method `getViewTypeCount`. – andrea.petreri Jan 27 '16 at 08:18
  • No need to put 3 ListViews, user RecyclerView and add different kind of items depending on your needs. – hardartcore Jan 27 '16 at 08:18
  • Possible duplicate of [Android how to display 2 listviews in one activity one after the other](http://stackoverflow.com/questions/17693578/android-how-to-display-2-listviews-in-one-activity-one-after-the-other) – Nicolas Cortell Jan 27 '16 at 08:18

3 Answers3

3

You can calculate height of listview belong to number item in this. Here is example :

public class ExpandedListView extends ListView {
private android.view.ViewGroup.LayoutParams params;
private int old_count = 0;

public ExpandedListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
    if (getCount() != old_count) {
        old_count = getCount();
        params = getLayoutParams();

        if (getChildAt(0)!= null){
            params.height = getCount() * (old_count > 0 ? (getChildAt(0).getHeight()) : 0) ;
        }else
            params.height = 0;

        setLayoutParams(params);
        requestLayout();
    }

    super.onDraw(canvas);
}
}

And use it in xml like this :

.......
<your package.ExpandedListView
    android:id="@+id/lv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none" />

<your package.ExpandedListView
    android:id="@+id/lv2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none" />
....
Danh DC
  • 1,246
  • 7
  • 17
2

Note This solution will fix the height of listView.

You need to set the height of each ListView. Lets say 30dp also give layout_weight="1" Here is example LinearLayout nested inside ScrollView

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
<LinearLayout 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"
    android:orientation="vertical">

    <ListView
        android:id="@+id/lastest"
        android:layout_marginTop="10sp"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="30dp" />

    <ListView
        android:id="@+id/featured"
        android:layout_marginTop="10sp"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="30dp" />

    <ListView
        android:id="@+id/startup"
        android:layout_marginTop="10sp"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="30dp" />
</LinearLayout>
</ScrollView>

Hope it helps

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
2

Please use custom Listview:

public class ExpandableHeightListview extends ListView
  {

    boolean expanded = false;

    public ExpandableHeightListview(Context context)
    {
        super(context);
    }

    public ExpandableHeightListview(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ExpandableHeightListview(Context context, AttributeSet attrs,int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

in .xml file:

<yourpackagename.ExpandableHeightListview
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/White"
            android:scrollbars="none"
            android:orientation="vertical"
            android:fadingEdge="none">
</cyourpackagename.ExpandableHeightListview>
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
Manish
  • 1,071
  • 2
  • 10
  • 27
  • thank you. but listview hide scrollbar only, when height screen < height listview, listview will scrolling. I want listview not scroll, as view difference – Sỹ Phạm Jan 27 '16 at 08:37
  • i want build layout same youtube app google https://play.google.com/store/apps/details?id=com.google.android.youtube [br]Build layout design home. Thanks – Sỹ Phạm Jan 27 '16 at 08:40
  • there is property `expandable`in custom list view. try setExpanded(true) in custom listView – Manish Jan 27 '16 at 09:20
  • is that fix the issue?? – Manish Jan 27 '16 at 09:24