DIsable scrolling for listview and enable for whole layout

11,354

Solution 1

You can try this.

FOR xml PART DO THIS:

Put your entire layout data under one Scroll View, for example:

    <ScrollView
        android:id="@+id/scrollViewId"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >             

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

                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" /> // SAY YOUR FIRST LIST VIEW:

                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" /> // SAY YOUR SECONDLIST VIEW:

                 // Add your other views as per requirement....

        </LinearLayout>

    </ScrollView>

NOW IN JAVA CLASS DO THE FOLLOWING THING...

Just add this custom method to your code after setting adapter to list view:

setListViewHeightBasedOnChildren(listview)

For Example:

      list = (ListView) findViewById(R.id.listview);
      list.setAdapter(new ArrayAdapter<String> 
                      (MainActivity.this,android.R.layout.simple_list_item_1,name));
      setListViewHeightBasedOnChildren(list);

Do it same for second list view too.

Here is body of setListViewHeightBasedOnChildren METHOD

   public static void setListViewHeightBasedOnChildren(ListView listView) 
{
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), 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,  
                                      LayoutParams.MATCH_PARENT));

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();

    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));

    listView.setLayoutParams(params);
    listView.requestLayout();

}

Hope it works for you.

Solution 2

You shouldn't put scrolling container inside other scrolling container (ScrollView containing listView) Even if you manage to make it work it will create problems.

Please consider redesign of your layout or e.g. dynamically add layouts to scroll view or get rid of scroll view and use list view with header and/or footer views

Share:
11,354
user3781907
Author by

user3781907

Updated on June 15, 2022

Comments

  • user3781907
    user3781907 almost 2 years

    Hi iam currently working on an android application it has two list views inside the main activity.What i want is disable the scrolling of two lists and allow the whole page to scroll only,is there any way for that please do help..... my code package com.example.listviewdemo;

     import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.MotionEvent;
     import android.view.View;
     import android.widget.AdapterView;
     import android.widget.ArrayAdapter;
     import android.widget.ListView;
     import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
    ListView list,list2;
    String[] name={"Happy","always","try","hard","you will","get it!","Believe","in","God","everything","will","work well!","Believe","in","God","everything","will","work well!"};
    String[] name2={"Believe","in","God","everything","will","work well!","Believe","in","God","everything","will","work well!"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        list = (ListView) findViewById(R.id.listview);
        list2 = (ListView) findViewById(R.id.listview2);
        list.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,name));
      list2.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,name2));
    
    
    
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                // TODO Auto-generated method stub
    
                Toast.makeText(getBaseContext(), name [position],Toast.LENGTH_SHORT).show();
            }
        });
    
    list2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                // TODO Auto-generated method stub
    
                Toast.makeText(getBaseContext(), name2 [position],Toast.LENGTH_SHORT).show();
            }
        });   
    }   
      }
    

    my xml code is:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
         android:layout_width="match_parent"
        android:layout_height="match_parent"
         android:orientation="vertical"
         tools:context="${relativePackage}.${activityClass}" >
        <TextView
        android:id="@+id/text_id1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
      android:text="@string/str1" />
       <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
         android:layout_height="wrap_content"
        />
        <TextView
          android:id="@+id/text_id2"
         android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/str2" 
    
         />
    
          <ListView
        android:id="@+id/listview2"
        android:layout_width="match_parent"
         android:layout_height="wrap_content"
           />
          </LinearLayout>
    
  • user3781907
    user3781907 over 9 years
    but what i have heard is that you cant put the listview inside scroll view!
  • A.R.
    A.R. over 9 years
    Usually we dont put list view inside scroll view because list view has its own default scrolling functionality that's why we don't put. But its not mandatory that you can't do this thing. You can put as many list views you want in your UI. But you have stop the scrolling functionality of the List View.
  • A.R.
    A.R. over 9 years
    Just try my code. Implement the Custom function that i have send you setListViewHeightBasedOnChildren(). You will definetely get your result. If there is any problem in implementation let me know.
  • Arpan Sharma
    Arpan Sharma almost 8 years
    it works fine. but my first list view starts skipping 1 element above the space.What can be the problem?
  • Selvin
    Selvin almost 7 years
    This solution is terrible ... You've just recreated a very expensive LinearLayout :)Romain Guy - software engineer at Google, on the Android project
  • Adam Ri
    Adam Ri over 3 years
    So, the two listViews now do not scroll, but the scrollView too. I need to disable it for two listViews, but keep it enabled for the scrollView.