3

I have a vertical custom list and each item of vertical list contains a horizontal list-view. When I scroll horizontally then list also moves little vertically. Which makes it less user friendly. Can I disable vertical scrolling while scrolling horizontally. I am using

<com.devsmart.android.ui.HorizontalListView
        android:id="@+id/hlistview"
        android:layout_width="match_parent"
        android:layout_height="155dp"
        android:layout_margin="6dp"
        android:background="#fff"
        android:fitsSystemWindows="true"
    />

for horizontal list view

Yawar
  • 1,924
  • 3
  • 29
  • 39
  • Hi, try the below answer poted Shivam Verma. Its working but when starting swipe up and down from the area of HorizontalListView , the HLV only scrolling. If you got any other solution means , pls share it. – Karthikeyan Ve Mar 24 '15 at 14:07
  • Why don't you try [TwoWayView](https://github.com/lucasr/twoway-view/) ? – Umesh Aug 10 '15 at 12:25

2 Answers2

6

The idea is to disable the parent listview to intercept touch event. This might work :

HorizontalListView hv = (HorizontalListView)findViewById(R.id.hlistview);  
    hv.setOnTouchListener(new ListView.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                case MotionEvent.ACTION_DOWN:
                    // Disallow ListView to intercept touch events.
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    break;

                case MotionEvent.ACTION_UP:
                    // Allow ListView to intercept touch events.
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                }

                // Handle HorizontalScrollView touch events.
                v.onTouchEvent(event);
                return true;
            }
        });

Reference : https://stackoverflow.com/a/22609646/1239966

Community
  • 1
  • 1
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
  • You don't need to reset the requestDisallowInterceptTouchEvent flag yourself. It will be reset automatically. According to the docs: "This parent must obey this request for the duration of the touch" https://developer.android.com/reference/android/view/ViewParent#requestDisallowInterceptTouchEvent(boolean) – Ed Lee Mar 12 '22 at 08:47
0

Best way to do this.

put given code on your horizontal listview onScroll method it work perfact

ViewParent view_parent = getParent();
if (view_parent != null) 
{
 view_parent.requestDisallowInterceptTouchEvent(true);
}
Ram
  • 1,408
  • 13
  • 29