Selectable scrolling for list and include entire layout

Hi iam currently working in android app it has two list views inside main activity. What I want is to disable scrolling of the two lists and allow scrolling of the whole page, is there a way to do this please help ..... my code is 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:

    <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>

      

+3


source to share


3 answers


You can try this.

FOR xml TO DO THIS:

Put all layout data in one scroll view like:

    <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 NEXT THING ...

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

setListViewHeightBasedOnChildren(listview)

      



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 this to display the second list.

Here is the 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.

+11


source


You cannot put a scrollable container in another scrollable container (a ScrollView containing a listView). Even if you can get it to work, it will create problems.



Please consider redesigning your layout, or for example dynamically add layouts for the scroll view, or get rid of the scroll view and use a list view with header and / or footer view

+1


source


scroolview only allows one child. so to solve your problem, you need to create scrollview in xml inside put linearLayout and inside LinearLayout put all your content.

and to disable scrolling of the list, you can use inside MainActivty: list.setVerticalScrollBarEnabled (false);

<ScrollView
       android:id="@+id/scrollView1"
       android:layout_width="match_parent"
       android:layout_height="match_parent" >
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical" >
           <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" >
           </ListView>
           <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" >
           </ListView>
       </LinearLayout>
   </ScrollView>

      

0


source







All Articles