HorizontalScrollView inside ListView: slight vertical scrolling stops horizontal scrolling

In my example of activity I have - a ListView containing - several HorizontalScrollView containing - a set of TextView

Scrolling horizontal scroll is pretty bad. When I start horizontal scrolling (or throw), I have to be very careful that it works.

Once the horizontal scroll contains the (small) vertical component, the vertical scroll of the ListView captures and completely stops the horizontal scroll.

Any suggestion on how to improve this?

Thanks in advance,

Mark

import android.app.Activity; import android.database.DataSetObserver; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; public class Test extends Activity { private final static int N = 20;//number of HorizontalScrollView private final static int M = 20;//number of TextViews inside a single HorizontalScrollView @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = new LinearLayout(this); //create a list of HorizontalScrollViews final HorizontalScrollView[] hors = new HorizontalScrollView[N]; for (int i = 0; i < hors.length; i++) { hors[i] = new HorizontalScrollView(this, null); hors[i].setMinimumHeight(60); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.HORIZONTAL); hors[i].addView(ll); for (int j = 0; j < M; j++) { TextView t = new TextView(Test.this); t.setText("HorizontalScrollView: "+i+"; TextView: "+j); t.setMinimumHeight(40); ll.addView(t); } } //add a ListView ListView list = new ListView(this); layout.addView(list); list.setAdapter(new BaseAdapter() { @Override public View getView(int position, View convertView, ViewGroup parent) { return hors[position]; } @Override public long getItemId(int position) { return 0; } @Override public Object getItem(int position) { return hors[position]; } @Override public int getCount() { return N; } }); setContentView(layout); } } 
+4
source share
4 answers

The problem is that the ListView (or rather its parent AbsListView ) implements onInterceptTouchEvent and therefore the ListView can intercept all touch events as soon as it considers that it is in the best position to handle them.

This can be avoided by calling requestDisallowInterceptTouchEvent , for example. by subclassing HorizontalScrollView and in its dispatchTouchEvent :

 public boolean dispatchTouchEvent(MotionEvent ev) { boolean ret = super.dispatchTouchEvent(ev); if(ret) { requestDisallowInterceptTouchEvent(true); } return ret; } 

It needs some refinement (for example, only calling requestDisallowInterceptTouchEvent if the horizontal component is larger than the vertical one), since now it almost never allows ListView to take control.

+5
source

although I didn’t dig inside your code, but a common mistake people make is to insert listView inside ScrollView.

as the listview scrolls automatically, so keep it out of the scrollView, while other things might be in the scrollView.

check if this is a script, otherwise pass the XML layout

+1
source

Android has a touch touch concept. A similar question has already been asked here: Android ACTION_MOVE Threshold

0
source

The best way to do this.

put the given code in your horizontal list in the Scroll method, it works perfectionism

  ViewParent view_parent = getParent(); if (view_parent != null) { view_parent.requestDisallowInterceptTouchEvent(true); } 
0
source

Source: https://habr.com/ru/post/1379979/


All Articles