Scrollview only Works in Listview

I have the following layout defined in my app:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/disco_photo"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/disco_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_below="@id/disco_photo"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left"
            android:layout_margin="8dp"
            android:gravity="end"
            android:textSize="20sp"
            android:textStyle="italic" />

        <TextView
            android:id="@+id/disco_times"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_below="@id/disco_name"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left"
            android:layout_margin="8dp"
            android:gravity="end"
            android:textSize="20sp"
            android:textStyle="italic"
            android:visibility="gone" />

        <ListView
            android:id="@+id/disco_parties"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/disco_times"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left"
            android:layout_margin="8dp"
            android:text="@string/map_view"
            android:gravity="end"
            android:textSize="20sp"
            android:textStyle="italic" />

        <RatingBar
            android:id="@+id/disco_rating"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_below="@id/disco_parties"
            android:numStars="5"
            android:stepSize="1.0"
            android:rating="3.0"
            android:visibility="gone" />

        <TextView
            android:id="@+id/disco_map"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_below="@id/disco_rating"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left"
            android:layout_margin="8dp"
            android:text="@string/map_view"
            android:gravity="end"
            android:textSize="20sp"
            android:textStyle="italic" />

    </RelativeLayout>
</ScrollView>

I want to get scroll in the whole layout but the problems comes when only ListView is getting the scrollbar, so you can only scroll this list. The rest of the layour is fixed to the screen height.

How can I make the whole layout be scrollable? Thanks in advance.

Answers


I am going through your problem. You can not use list view inside the scroll-view directly. If you want to use it, then you have to give height pragmatically to list view and it work for me.

public class test1 extends Activity {
...
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test1);
...

//here 1st set your list view than call  set_List_View_Height_Based_On_Children...

set_List_View_Height_Based_On_Children(your_Listview_name); 
...
}
///method set_List_View_Height_Based_On_Children
public static void set_List_View_Height_Based_On_Children(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}
}

ListView can't be placed inside a ScrollView unless you define that your ListView isn't scrollable. If you wanna do this you should create a custom ListView like the following

    public class NonScrollListView extends ListViewCompat {

//Define a public constructor
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = View.MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

You should not use a ListView in a ScroolView, it has it's own scroll and they interfere with each-other. I would recommend creating a list adapter that creates all of your different views from an array using a switch statement:

    class MyAdapter extends ArrayAdapter{

    public MyAdapter(Context context, int resource, List objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         ViewItem viewType = getItem(position);

        switch(viewType.type){
            case TEXTVIEW: convertView = LayoutInflater.from(getContext()).inflate(R.layout.textView1, parent, false);

                break;
            case LISTITEM: convertView = LayoutInflater.from(getContext()).inflate(R.layout.listItem, parent, false);

                break;
        }


        return convertView;
    }


}

Need Your Help

I can't post data to database with AJAX in Laravel 5.2

ajax database post laravel-5.2

I am trying to send / post data to database with AJAX in laravel 5.2 but I am not able to post data to database with AJAX. what is the problem I can not figure out this.

How can I modify an existing UIViewController to allow scrolling?

iphone uiview uiviewcontroller interface-builder uiscrollview

I have already built a UIViewController subclass with a bunch of controls in it, and just realized that if I rotate the iPhone, half of the controls become invisible. So, I would like to somehow ma...