Disable vertical scroll when user scrolling horizontally in android

 ̄綄美尐妖づ 提交于 2019-12-11 03:17:28

问题


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


回答1:


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




回答2:


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);
}


来源:https://stackoverflow.com/questions/24734226/disable-vertical-scroll-when-user-scrolling-horizontally-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!