-2

I am trying to add list view under scroll view so that i can scroll whole page as well as my list view but my list view is not scrolling while my whole page is scrolling.

This is my XML :

          <?xml version="1.0" encoding="utf-8"?>
          <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:background="@color/gray">
             <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/toolbar"
                android:background="@color/white">


             <LinearLayout
               android:id="@+id/layout_form"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:orientation="vertical">

                 <android.support.v4.view.ViewPager
                    android:id="@+id/viewPager"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_marginBottom="5dp"
                    android:background="@color/trans_white"/>

                 <ListView
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:id="@+id/list_prdt_detail"
                   android:divider="@color/white"
                   android:layout_marginBottom="50dp"
                   android:scrollbars="vertical"/>

                 </LinearLayout>

                 </ScrollView>


                  <LinearLayout
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     android:orientation="horizontal"
                     android:weightSum="2"
                     android:layout_gravity="center"
                     android:gravity="center"
                     android:layout_alignParentBottom="true">

                  <TextView
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="ADD TO CART"
                   android:background="@color/orange"
                   android:layout_weight="1"
                   android:layout_gravity="center"
                   android:gravity="center"
                   android:padding="20dp"
                   android:textColor="@color/white"
                   android:fontFamily="sans-serif"/>

                 <TextView
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="BUY NOW"
                   android:background="@color/red"
                   android:layout_weight="1"
                   android:layout_gravity="center"
                    android:gravity="center"
                   android:padding="20dp"
                   android:textColor="@color/white"
                   android:fontFamily="sans-serif"/>

                 </LinearLayout>

               </RelativeLayout>

This is my java :

              listAdapter = new 
                 AdapterProddetail(Productdetail.this,al_prod_id,
                   al_prod_image,al_prod_name,al_prod_price,
                   al_prod_disc,al_prod_discount,al_prod_stock,al_prod_cc);
                   list_prdt_detail.setAdapter(listAdapter);
                   list_prdt_detail.setOnItemClickListener(this);
                   list_prdt_detail.setOnTouchListener(new 
                     ListView.OnTouchListener() {

                       public boolean onTouch(View v, MotionEvent event) {
                          int action = event.getAction();
                           switch (action) {
                                    case MotionEvent.ACTION_DOWN:
                                  // Disallow ScrollView to intercept touch 
                                     events.
                     v.getParent().requestDisallowInterceptTouchEvent(true);
                      break;
                     case MotionEvent.ACTION_UP:
                    // Allow ScrollView to intercept touch events.
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                    } // Handle ListView touch events.
                       v.onTouchEvent(event);
                      return true;
                    }
                 });

I have add java code that will disallow scroll intercept but still list view not scrolling.Please help me to overcome this situation.

Abhijeet
  • 481
  • 1
  • 6
  • 14

2 Answers2

2

Do not use ListView inside ScrollView. Use NestedScrollView instead of ScrollView.

OR

you can try it with setting android:nestedScrollingEnabled="true" to ListView

Try this:

<?xml version="1.0" encoding="utf-8"?>
<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:background="@android:color/darker_gray">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:background="@android:color/white">

        <LinearLayout
            android:id="@+id/layout_form"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v4.view.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginBottom="5dp"
                android:background="#88ffffff"/>

            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/list_prdt_detail"
                android:divider="@android:color/white"
                android:layout_marginBottom="50dp"
                android:scrollbars="vertical"
                android:nestedScrollingEnabled="true"/>

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_alignParentBottom="true">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ADD TO CART"
            android:background="@android:color/holo_orange_light"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:gravity="center"
            android:padding="20dp"
            android:textColor="@android:color/white"
            android:fontFamily="sans-serif"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="BUY NOW"
            android:background="@android:color/holo_red_dark"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:gravity="center"
            android:padding="20dp"
            android:textColor="@android:color/white"
            android:fontFamily="sans-serif"/>

    </LinearLayout>
</RelativeLayout>

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
1

In your code, v.getParent() refers to the LinearLayout and not the ScrollView. Use v.getParent().getParent().

Also, as #Ferdous Ahamed mentioned, you should use NestedScrollView instead of ScrollView

Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103