问题
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