I am adding touch listener to parent view for to detect swipe gestures. My layout has a nested scrollview and three recycleviews, but my touchevent does not get triggered, i think the parent doesn't get the touchIntercept. In this case i want the constraintLayout to handle the touch event.
XML
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/table_view_main_constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ui.table_view.TableViewActivity"
tools:showIn="@layout/activity_table_view">
<ProgressBar
android:id="@+id/progress_table_view"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="@color/colorFontWhite"
android:translationZ="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.core.widget.NestedScrollView
android:id="@+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/content_table_view_layout_bottom"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint_layout2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint_layout_spinners"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Spinner
android:id="@+id/spinner_table_view_country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/nav_header_vertical_spacing"
android:background="@drawable/shape_button_round_home"
android:gravity="center_vertical"
android:orientation="vertical"
android:popupBackground="@drawable/shape_spinner_gradient"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/spinner_table_view_league"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/nav_header_vertical_spacing"
android:background="@drawable/shape_button_round_home"
android:gravity="center_vertical"
style="spinner"
android:orientation="vertical"
android:popupBackground="@drawable/shape_spinner_gradient"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spinner_table_view_country" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/table_view_recycle_view_games"
android:layout_width="0dp"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
app:layout_constraintEnd_toEndOf="@id/guideline5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/constraint_layout_spinners" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline5"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.53"
app:layout_constraintTop_toBottomOf="@id/constraint_layout_spinners" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/table_view_recycle_view_odds_1"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/guideline6"
app:layout_constraintStart_toStartOf="@id/guideline5"
app:layout_constraintTop_toBottomOf="@id/constraint_layout_spinners"
android:nestedScrollingEnabled="false" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/table_view_recycle_view_odds_2"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/guideline6"
app:layout_constraintTop_toBottomOf="@id/constraint_layout_spinners"
android:nestedScrollingEnabled="false" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline6"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.77"
app:layout_constraintTop_toBottomOf="@id/constraint_layout_spinners" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Activity
public class TableViewActivity extends BaseActivity implements CustomAdapterGames.OnGameClickListener {
private ConstraintLayout constraintLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_view);
constraintLayout = findViewById(R.id.table_view_main_constraint_layout);
constraintLayout.requestDisallowInterceptTouchEvent(true);
recyclerViewGames = findViewById(R.id.table_view_recycle_view_games);
adapterOdds1 = new CustomAdapterOdds(this, Games, this, GamesHashMap.get(viewPagerColFirst), SitesList, viewPagerColFirst);
configRecyclerViewGames(recyclerViewGames, adapterGames);
}
}
Ontouch Method
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
// return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// swipe horizontal?
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
Toast.makeText(TableViewActivity.this, "prev", Toast.LENGTH_SHORT).show();
return true;
}
if (deltaX > 0) {
Toast.makeText(TableViewActivity.this, "next", Toast.LENGTH_SHORT).show();
return true;
}
} else {
Toast.makeText(TableViewActivity.this, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE, Toast.LENGTH_SHORT).show();
}
// swipe vertical?
if (Math.abs(deltaY) > MIN_DISTANCE) {
// top or down
if (deltaY < 0) {
return true;
}
if (deltaY > 0) {
return true;
}
} else {
Toast.makeText(TableViewActivity.this, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE, Toast.LENGTH_SHORT).show();
}
// return true;
}
}
return false;
}