Android listview not scrolling

2020-02-15 03:59发布

问题:

i have a layout whose starting tag(parent tag is ) is

<scroll view  
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

later i have many images , textView , Edit text and other things, and finally a list view. i have defined the hight of listview as 230dp as prescribed to me. the thing is the whole layout is scrolling nice, but the content inside the listview is not scrolling individually . this is how the xml looks...

    <ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     > 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical" >
    <TextView android:layout_width="fill_parent"
         android:layout_height="50dp"
         android:text="BISCOOT"
         android:textSize="25dp"
         android:background="@drawable/item_bg_light"
         android:textColor="@android:color/white"
         android:gravity="center"/>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" android:layout_width="fill_parent" 
     android:layout_height="200dp" android:background="#ff000000">

      <ImageSwitcher android:id="@+id/switcher1" android:layout_width="fill_parent" 
      android:layout_height="200dp"  
      android:layout_alignParentLeft="true" android:layout_alignParentRight="true" >
      </ImageSwitcher> 
        <ProgressBar
              android:id="@+id/progressBar2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"  android:layout_centerHorizontal="true"
               />
        <ImageButton android:id="@+id/imageButton1" android:layout_width="wrap_content"
       android:layout_height="wrap_content" android:layout_alignParentRight="true" 
       android:layout_centerVertical="true" android:src="@android:drawable/ic_media_ff" /> 

        <ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:layout_alignParentLeft="true" 
      android:layout_alignTop="@+id/imageButton1" android:src="@android:drawable/ic_media_rew" /> 
  </RelativeLayout>
    <TextView android:layout_width="fill_parent"
         android:layout_height="30dp"
         android:text="Latest headLines"
         android:background="@drawable/item_bg_light"
         android:textColor="@android:color/white"
         android:gravity="center"/>
    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
         <LinearLayout android:orientation="vertical"
             android:layout_width="fill_parent"            
             android:layout_height="fill_parent">
      <ListView 
        android:id="@android:id/list" 
        android:layout_width="fill_parent" 
        android:scrollbarFadeDuration="0" 
        android:scrollbarStyle="outsideOverlay" 
        android:smoothScrollbar="true" 
          android:layout_height="260dp">
     </ListView>
    </LinearLayout>



    <!-- 
    <TextView android:layout_width="fill_parent"
         android:layout_height="30dp"
         android:text="Heroin On the Rocks"
         android:background="@drawable/item_bg_light"
         android:textColor="@android:color/white"
         android:gravity="center"/>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" android:layout_width="fill_parent" 
     android:layout_height="200dp" android:background="#ff000000">

      <ImageSwitcher android:id="@+id/switcher2" android:layout_width="fill_parent" 
      android:layout_height="200dp"  
      android:layout_alignParentLeft="true" android:layout_alignParentRight="true" /> 

        <ImageButton android:id="@+id/imageButton3" android:layout_width="wrap_content"
       android:layout_height="wrap_content" android:layout_alignParentRight="true" 
       android:layout_centerVertical="true" android:src="@android:drawable/ic_media_ff" /> 

        <ImageButton android:id="@+id/imageButton4" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:layout_alignParentLeft="true" 
      android:layout_alignTop="@+id/imageButton1" android:src="@android:drawable/ic_media_rew" /> 
  </RelativeLayout>
  -->




<TextView android:layout_width="fill_parent"
         android:layout_height="30dp"
         android:text="Watch Movies"
         android:background="@drawable/item_bg_light"
         android:textColor="@android:color/white"
         android:gravity="center"/>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="100dp" android:orientation="horizontal"
    android:weightSum="6"
    >
        <ImageView 

            android:layout_height="100dp"
            android:layout_width="wrap_content" android:layout_weight="1"
           android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
            android:id="@+id/watchmoviesimg1"
            />
         <ImageView android:layout_height="100dp"
            android:layout_width="wrap_content" android:layout_weight="1"
           android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
            android:id="@+id/watchmoviesimg2"/>
          <ImageView android:layout_height="100dp" android:layout_weight="1"
            android:layout_width="wrap_content"
           android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
            android:id="@+id/watchmoviesimg3"/>

          <ProgressBar
              android:id="@+id/progressBar3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center" />

           <ImageView android:layout_height="100dp" android:layout_weight="1"
            android:layout_width="wrap_content"
           android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
            android:id="@+id/watchmoviesimg4"/>
            <ImageView android:layout_height="100dp" android:layout_weight="1"
            android:layout_width="wrap_content"
           android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
            android:id="@+id/watchmoviesimg5"/>
             <ImageView android:layout_height="100dp" android:layout_weight="1"
            android:layout_width="wrap_content"
           android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
            android:id="@+id/watchmoviesimg6"/>


    </LinearLayout>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="200dp" 
    android:orientation="vertical"
    >
              <TextView android:layout_width="fill_parent"
         android:layout_height="30dp"
         android:text="Photo Stories"
         android:background="@drawable/item_bg_light"
         android:textColor="@android:color/white"
         android:gravity="center"/>

              <ProgressBar
                  android:id="@+id/progressBar4"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center" />

               <ImageView 
                   android:id="@+id/photostoryimageview1"
                   android:layout_height="200dp" android:layout_weight="1"
            android:layout_width="fill_parent"
            />
               </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="200dp" 
    android:orientation="vertical"
    >
                 <TextView android:layout_width="fill_parent"
         android:layout_height="30dp"
         android:text="Star Special"
         android:background="@drawable/item_bg_light"
         android:textColor="@android:color/white"
         android:gravity="center"/>

                 <ProgressBar
                     android:id="@+id/progressBar5"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content" 
                     android:layout_gravity="center"/>

               <ImageView 
                   android:id="@+id/starSpecialimageView1"
                   android:layout_height="200dp" android:layout_weight="1"
            android:layout_width="fill_parent"
            />
               </LinearLayout>

</LinearLayout>
</ScrollView>

回答1:

Simply, remove the ScrollView from your layout. And, make the LinearLayout as parent. And, try to run your app. It will scroll the list.

Because the ListView class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent ScrollView I strongly recommend you to simplify your layout somehow. For example you can add views you want to be scrolled to the ListView as headers or footers.

Have a look at this

Update

Take one parent layout with two child. And, in one layout it should contain ScrollView with your contents. And, on another layout contain your ListView Try like this He has done like that one.



回答2:

just add this, then it will work.

listView.setOnTouchListener(new View.OnTouchListener() {
        // Setting on Touch Listener for handling the touch inside ScrollView
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Disallow the touch request for parent scroll on touch of child view
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });


回答3:

You shouldn't put a ListView inside a ScrollView because the ListView class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent ScrollView.



回答4:

You dont able to do ScrollViw and ListView in one layout. If you have put ScrollView then ListView will not work and if you dont put ScrollView then ListView will work to scroll.

UPDATE

Even if the listview inside scrollview or scrollview inside listview, both will work. You need to handled its touch accordingly.



回答5:

Try below class, it may help you to work with ListView inside ScrollView

Utility.java

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;

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

            int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
            for (int i = 0; i < listAdapter.getCount(); i++) {
                 View listItem = listAdapter.getView(i, null, listView);
                 if (listItem instanceof ViewGroup) {
                    listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                 }
                 listItem.measure(0, 0);
                 totalHeight += listItem.getMeasuredHeight();
            }

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

Finally use this line just after setting adapter to your listview - Utility.setListViewHeightBasedOnChildren(your_listview);



回答6:

You cannot add a ListView in a scroll View, as list view also scrolls and there would be a synchonization problem between listview scroll and scroll view scroll. You can make a CustomList View and add this method into it.

@Override public boolean onInterceptTouchEvent(MotionEvent ev)
{
    /*
     * Prevent parent controls from stealing our events once we've gotten a touch down
     */
    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
        ViewParent p = getParent();
        if (p != null) {
            p.requestDisallowInterceptTouchEvent(true);
        }
    }
    return false;
}


回答7:

I found one more solution to get rid of this scrolling problem. Actually using listview in scrollview sometimes might create problems.

We could remove this scrollview and just take one listview. Now create three layout first main layout with listview (which we want to show above list), second for header and third for footer (which we are to show below list) and finally add header and footer in list. This will add header and footer as a list item.

Note: This solution is for small list like 2 or 3 items in list.