Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add two listview in scrollview

I am adding two list view in one scroll view, as my both listview(s) will contains data, but the problem I am facing is that when I add listview(S) in scroll view, it only shows one item of each list view.

Here is the image,

enter image description here

The XML code is :

<ScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/layoutFooter7"
        android:layout_below="@+id/linArrange" >

        <RelativeLayout
            android:id="@+id/linListViews"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <ListView
                android:id="@+id/lstviewUnBlockVenues"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:cacheColorHint="#00000000"
                android:divider="#00000000"
                android:dividerHeight="0dp"
                android:fadeScrollbars="false" >

            </ListView>

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/lstviewUnBlockVenues"
                android:background="#ABCDEF"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Blocked Venues"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="#061218" />
            </LinearLayout>



            <ListView
                android:id="@+id/lstviewBlockedVenues"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/linearLayout1"
                android:cacheColorHint="#00000000"
                android:divider="#00000000"
                android:dividerHeight="0dp"
                android:fadeScrollbars="false" >

            </ListView>
        </RelativeLayout>
    </ScrollView>

All I want is that scroll view should scroll down if the list view contains more items, as at this moment both list views are displaying one item only :(

like image 769
Adil Bhatty Avatar asked Feb 21 '23 05:02

Adil Bhatty


2 Answers

I used "arnp" 's answer and made and example with 3 listView in scrollview. It works fine.

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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

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

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

            <View
                android:layout_width="fill_parent"
                android:layout_height="5dp"
                android:background="#ff0000" />

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

            <View
                android:layout_width="fill_parent"
                android:layout_height="5dp"
                android:background="#ff0000" />

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

            <View
                android:layout_width="fill_parent"
                android:layout_height="5dp"
                android:background="#ff0000" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

Activity :

package com.example.listviewtest;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView list1 = (ListView) findViewById(R.id.list1);
        ListView list2 = (ListView) findViewById(R.id.list2);
        ListView list3 = (ListView) findViewById(R.id.list3);


        String[] items1 = { "Alfa1", "Alfa2", "Alfa3", "Alfa4", "Alfa5", "Alfa6", "Alfa7", "Alfa8", "Alfa9", "Alfa10", "Alfa11", "Alfa12", "Alfa13", "Alfa14", "Alfa15", "Alfa16", "Alfa17"  };
        String[] items2 = { "Beta1", "Beta2", "Beta3", "Beta4", "Beta5", "Beta6", "Beta7", "Beta8", "Beta9", "Beta10", "Beta11", "Beta12", "Beta13", "Beta14", "Beta15", "Beta16", "Beta17" };
        String[] items3 = { "Teta1", "Teta2", "Teta3", "Teta4", "Teta5", "Teta6", "Teta7", "Teta8", "Teta9", "Teta10", "Teta11", "Teta12", "Teta13", "Teta14", "Teta15", "Teta16", "Teta17" };

        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items1);
        list1.setAdapter(adapter1);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items2);
        list2.setAdapter(adapter2);
        ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items3);
        list3.setAdapter(adapter3);

        setListViewHeightBasedOnChildren(list1);
        setListViewHeightBasedOnChildren(list2);
        setListViewHeightBasedOnChildren(list3);

    }

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

            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }

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


}
like image 104
Engin OZTURK Avatar answered Mar 02 '23 17:03

Engin OZTURK


It's best you modify your UI.

Placing one scrolling widget in another in Android really creates lot of problems. There is a workaround but that makes scrolling a pain for the user.

For more information you may click on any of the results that show up on the google search

like image 20
Ravi Vyas Avatar answered Mar 02 '23 15:03

Ravi Vyas