5

in my code there is four listview how do i make screen scrollable to show all data of all listview? not this code make listview scrollable i dont want listview scrolling want ScreenScrollable like this image

   <?xml version="1.0" encoding="UTF-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#ffffff"
   android:orientation="vertical" >

    <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/imagelogo2"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/test_button_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="20dp"
        android:src="@drawable/back" >
     </ImageView>

     <ImageView
        android:id="@+id/test_button_image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingLeft="5dp"
        android:paddingRight="15dp"
        android:paddingTop="15dp"
        android:src="@drawable/options1" />
     </RelativeLayout>

  <TextView
    android:id="@+id/ElemenrySchool"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/btnback"
    android:paddingBottom="10dp"
    android:paddingLeft="5dp"
    android:paddingTop="10dp"
    android:text="elementry"
    android:textSize="17dp" >
     </TextView>

  <LinearLayout
    android:id="@+id/lytContent"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@drawable/border2"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <ListView
        android:id="@+id/listMainMenu11"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dip"
        android:fadeScrollbars="true"
        android:listSelector="@drawable/listview_selector" />
       </LinearLayout>

    <TextView
    android:id="@+id/MiddleSchool"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:paddingLeft="5dp"
    android:paddingTop="10dp"
    android:text="Middle"
    android:textSize="17dp" >
   </TextView>

   <LinearLayout
    android:id="@+id/lytContent2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@drawable/border2"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <ListView
        android:id="@+id/listMainMenu22"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dip"
        android:fadeScrollbars="true"
        android:listSelector="@drawable/listview_selector" />
   </LinearLayout>

   <TextView
    android:id="@+id/HighSchool"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/btnback"
    android:paddingBottom="10dp"
    android:paddingLeft="5dp"
    android:paddingTop="10dp"
    android:text="HighSchool"
    android:textSize="17dp" >
   </TextView>

    <LinearLayout
    android:id="@+id/lytContent3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@drawable/border2"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

      <ListView
        android:id="@+id/listMainMenu33"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dip"
        android:fadeScrollbars="true"
        android:listSelector="@drawable/listview_selector" />
   </LinearLayout>

   <TextView
    android:id="@+id/AtipicalSchool"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/btnback"
    android:paddingBottom="10dp"
    android:paddingLeft="5dp"
    android:paddingTop="10dp"
    android:text="Atipical"
    android:textSize="17dp" >
   </TextView>

   <LinearLayout
    android:id="@+id/lytContent4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@drawable/border2"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <ListView
        android:id="@+id/listMainMenu44"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dip"
        android:fadeScrollbars="true"
        android:listSelector="@drawable/listview_selector" />
   </LinearLayout>

  </LinearLayout>
Sanket Kachhela
  • 10,512
  • 7
  • 46
  • 74
user2699824
  • 71
  • 1
  • 1
  • 7
  • 1
    List View is itself scrollable. Putting it in a ScrollView could be a bad idea! http://stackoverflow.com/questions/6210895/listview-inside-scrollview-is-not-scrolling-on-android – Prachi Aug 21 '13 at 09:23
  • i save data in adapter of listview listMainMenu.setAdapter(mma); Middleschoollist.setAdapter(mma); HighSchoollist.setAdapter(mma); Atipicalschoollist.setAdapter(mma); – user2699824 Aug 21 '13 at 09:24
  • data is dynamic get from server so must show inlistview????? – user2699824 Aug 21 '13 at 09:25

3 Answers3

46

use this custom listview component inside scrollview.

package com.app.Settings;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.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;
}
} 

HOW TO USE IN XML?

 <yourPackageName.ExpandableHeightListView
        android:id="@+id/expandableHeightlist1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"   
        android:cacheColorHint="#00000000"
        android:fadingEdge="none"
        android:focusable="false"
        android:scrollbars="none"
        >
    </yourPackageName.ExpandableHeightGridView>

and set

yourListView.setExpanded(true);

By using this custom component you can set multiple listview inside scrollview. i am using this custom component.

Enjoy :)

Sanket Kachhela
  • 10,512
  • 7
  • 46
  • 74
  • wait dont go ill reply u in 5 mins – user2699824 Aug 21 '13 at 09:59
  • where i put this? in main.java file or in xml file?? – user2699824 Aug 21 '13 at 10:16
  • is show error The method setExpanded(boolean) is undefined for the type Object – user2699824 Aug 21 '13 at 10:20
  • im declair istview like this listMainMenu = (ListView) findViewById(R.id.listMainMenu11); it is correct??? – user2699824 Aug 21 '13 at 10:21
  • ExpandableHeightListView listMainMenu = (ExpandableHeightListView) findViewById(R.id.listMainMenu11); – Sanket Kachhela Aug 21 '13 at 10:22
  • 1
    I dug into Android source repo, and found `ScrollView.measureChildWithMargins()` forcing its child views to be minimal in height [**(link)**](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/android/widget/ScrollView.java#ScrollView.measureChildWithMargins%28android.view.View%2Cint%2Cint%2Cint%2Cint%29). So, this hack is really necessary if one want a nested AdapterView (e.g. ListView) to be fully expanded in height within a parent ScrollView. And this might also be true of a nested AdapterView within a parent AdapterView. – Shigerello Feb 16 '14 at 15:20
  • How the AbsListView.OnScrollListener() would work with this code? I see that actually it is just called the first time and there is no way to get more data lazily. – Proverbio Nov 04 '14 at 01:37
  • Yes,it will load all data first not lazily.in this class we are setting height of listview as per (no of items*height of your row item).. – Sanket Kachhela Nov 04 '14 at 05:49
  • Necessary hacking. Thanks for sharing. – james Jul 27 '16 at 05:47
3

Never use two listview inside Scrollview

Don't try to use listview , use a LinearLayout instead , if you have got a fixed number of views. As listview will have its own scroll and this won't be a good user experience too.

Prateek
  • 3,664
  • 5
  • 35
  • 73
  • then how do i show dynamic data in fourlist view like screenshot?? – user2699824 Aug 21 '13 at 09:25
  • but my problemis i wanna make screen same like this image http://imgur.com/kR6vlKX load dynamic data form server showin listview – user2699824 Aug 21 '13 at 09:27
  • From your image it seems that you have 3 dynamic data sets with 3 differnt data that totals to 9 , So I would recomment in this case you should go by using a linearlayout for all the sets and inside these 3 linear layouts create 3 buttons and populate the data you have got. – Prateek Aug 21 '13 at 09:27
  • no data will be dynamic may be is three may be is 4 may b is 5 is fetch from server – user2699824 Aug 21 '13 at 09:29
  • Look you just can't have two scrolls working smoothly on one screen. In such a case you dynamically add Buttons to your layout based on the size of data you get from the server. – Prateek Aug 21 '13 at 09:33
  • i want only screen scroll not listview scroll – user2699824 Aug 21 '13 at 09:36
  • So you should dynamically add Buttons in the three layouts this is the only option in this case. – Prateek Aug 21 '13 at 09:37
0

U can try expandable listview for this issue, try below link http://www.javacodegeeks.com/2013/06/android-expandablelistview-with-custom-adapter-baseexpandablelistadapter.html

shiju B
  • 1,664
  • 9
  • 23