Reputation: 7928
I have a ViewPager. In each pager, it display a listview. Each row in the Listview contains a HorizontalScrollView element. Here is the code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white" >
<LinearLayout
android:id="@+id/survey_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/store_name"/>
<TextView
android:id="@+id/txt_store_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<HorizontalScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_below="@id/survey_header"
android:scrollbars="none">
<TableLayout
android:id="@+id/survey_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow
android:id="@+id/survey_table_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txt_status"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:textColor="@color/red_text"/>
<TextView
android:id="@+id/txt_product"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:textColor="@color/red_text"/>
<TextView
android:id="@+id/txt_stock"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:textColor="@color/red_text"/>
</TableRow>
<TableRow
android:id="@+id/menu_table_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:textColor="@color/red_text"
android:text="@string/survey_status"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:textColor="@color/red_text"
android:text="@string/product_info"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:textColor="@color/red_text"
android:text="@string/stock_count"/>
</TableRow>
</TableLayout>
</HorizontalScrollView>
</RelativeLayout>
Everytime I try to scroll the horizontal Scroll View, the ViewPager receives touch event first and change the page.
How can I disable the changing page of ViewPager when I touch Horizontal Scorll View.
Thanks in advance.
Upvotes: 0
Views: 1044
Reputation: 4448
HorizontalScrollView scrollView=(HorizontalScrollView)findViewById(R.id.scrollView);
scrollView.setOnTouchListener(new View.OnTouchListener() {
float rawX;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
scrollView.getParent().requestDisallowInterceptTouchEvent(true);
rawX = event.getRawX();
return false;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
scrollView.getParent().requestDisallowInterceptTouchEvent(false);
rawX = 0f;
return false;
case MotionEvent.ACTION_MOVE:
if (Math.abs(rawX - event.getRawX()) > ViewConfiguration.get(getActivity()).getScaledTouchSlop())
scrollView.getParent().requestDisallowInterceptTouchEvent(true);
break;
}
return false;
}
});
Upvotes: 1