DIsable scrolling for listview and enable for whole layout

前端 未结 3 1424
你的背包
你的背包 2021-01-05 13:43

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

相关标签:
3条回答
  • 2021-01-05 14:00

    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.

    0 讨论(0)
  • 2021-01-05 14:02

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

    and for disabling the list scrolling 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 讨论(0)
  • 2021-01-05 14:16

    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

    0 讨论(0)
提交回复
热议问题