Gallery View inside scroll view not working.

2019-06-02 05:44发布

问题:

I'm using gallery view inside scoll view but gallery view not working properly.

My custom GalleryView

<com.divum.Adapter.CustomGallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdge="none"
android:spacing="10dp" />

Adapter Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/top_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="2dp"
android:orientation="vertical"
android:weightSum="100">
<TextView 
android:id="@+id/txt_title1"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:padding="5dp"
android:textColor="@color/black"
android:textSize="14dp" />      
<ImageView 
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:layout_marginTop="2dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop" 
android:layout_marginLeft="2dp" />
<ScrollView 
android:scrollHorizontally="false"
android:fadingEdge="none"
android:scrollbars="none"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView 
android:id="@+id/txt_details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:padding="5dp"
android:textColor="@color/black"
android:textSize="14dp" />      
</ScrollView>
</LinearLayout>

My problem is scrollview(vertically) working fine. The galleryview(horizontally) swiping is not working...

回答1:

This is well known bug: ScrollView intercept both horizontal and vertical touch events. You can use this custom scroll view instead of standard one. This intercept only vertical touches:

public class VerticalScrollView extends ScrollView {

private float xDistance, yDistance, lastX, lastY;

public VerticalScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        xDistance = yDistance = 0f;
        lastX = ev.getX();
        lastY = ev.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        final float curX = ev.getX();
        final float curY = ev.getY();
        xDistance += Math.abs(curX - lastX);
        yDistance += Math.abs(curY - lastY);
        lastX = curX;
        lastY = curY;
        if (xDistance > yDistance)
            return false;
    }

    return super.onInterceptTouchEvent(ev);
}

}