Solve the conflict between the rolling event of horizontalscrollview in Android and the touch of the component.

Source: Internet
Author: User
 

In the previous chapter, we implemented the drawer + scroll function, but a problem left over is that the rolling event conflicts with the touch event of the component. Next, let's take a look at the cause of the conflict.

Public BooleanOnintercepttouchevent(Motionevent eV)

This method can be used to intercept events caused by all touch screen actions. This means you can monitor events assigned to subitems and get ownership of any current gesture.

Exercise caution when using this method. Because it has a very complex interaction impact with view. ontouchevent (motionevent. Both must be correctly implemented at the same time. Events are received by methods in the following order:

1. The down event is received.

2. the event is processed by a subview of the view group or by the ontouchevent () method passed to the view group. This means that you must implement ontouchevent () and return true, in this way, you can continue to receive other gestures (instead of seeking a parent view to process it ). After ontouchevent () returns true, you no longer receive any event of onintercepttouchevent (), and all processing of the touch action must be done in ontouchevent () as usual.

3. If false is returned, each subsequent event (all up events, including the last up event) will be first passed here and then ontouchevent () of the target object view ().

4. If true is returned, you will not receive any of the following events: the target view will receive the same event but carry the action_cancel action. All subsequent events will be passed to your ontouchevent () method and will not appear here.

Parameters

Action events that ev distributes along the tree structure

Return Value

Returns true if action events are intercepted from the subview and assigned to the current viewgroup through ontouchevent. The current target will receive an action_cancel event, and no other messages will be transmitted here.

 

OnintercepttoucheventThis function is available only in the layout. As described above, to solve this problem, we need to implement the touch event of the component.OnintercepttoucheventFalse is returned. Otherwise, the component cannot capture the touch event. After knowing the principle, we can write the code. We use a flag to check whether the child view of the standard horizontalscrollview is implementing the touch event. If yes, set the flag.
= 0; When touch_up, set flag =-1, while inOnintercepttoucheventThe function checks whether the flag is equal to-1. If the flag is equal to-1OnintercepttoucheventReturn return Super. onintercepttouchevent (EV); otherwise, false is returned. The following code is used for implementation:

The layout file needs to be rewritten:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <SlidingDrawer        android:id="@+id/slidingDrawer1"        android:layout_width="wrap_content"        android:layout_height="80dp"android:layout_alignParentBottom="true"        android:content="@+id/content"        android:handle="@+id/handle" >        <Button            android:id="@+id/handle"            android:layout_width="wrap_content"            android:layout_height="20dp"            android:text="Handle" />        <com.example.test.Horizon            android:id="@+id/content"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"            android:background="@android:color/black"            android:scrollbars="none" >            <LinearLayout                android:layout_width="wrap_content"                android:layout_height="50dp"                android:layout_gravity="center_vertical"                android:gravity="center_vertical"                android:id="@+id/content_linear"                android:orientation="horizontal" >                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:id="@+id/btn1"                    android:text="btn1" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn2" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn3" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn4" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn5" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn5" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn5" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn5" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn5" >                </Button>            </LinearLayout>        </com.example.test.Horizon>    </SlidingDrawer></RelativeLayout>

Unlike the layout file in the previous chapter, you can use Com. example. Test. horizon to replace horizontalscrollview. The following is the content of the Horizon class:

Horizon. Java

 

package com.example.test;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.HorizontalScrollView;public class Horizon extends HorizontalScrollView{public Horizon(Context context) {super(context);}public Horizon(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {if (MainActivity.flag == -1) {return super.onInterceptTouchEvent(ev);}else{return false;}}}

The main class content is as follows:

package com.example.test;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.Button;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn1 = (Button)findViewById(R.id.btn1);btn1.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:flag = 0;Log.e("test", "down");break;case MotionEvent.ACTION_MOVE:Log.e("test", "move");break;case MotionEvent.ACTION_UP:flag = -1;Log.e("test", "up");break;}return false;}});}public Button btn1;public static int flag = -1;}

The test results are as follows:

07-06 21:49:04.639: E/test(409): down07-06 21:49:04.659: E/test(409): move07-06 21:49:04.669: E/test(409): move07-06 21:49:04.679: E/test(409): move07-06 21:49:04.699: E/test(409): move07-06 21:49:04.709: E/test(409): move07-06 21:49:04.719: E/test(409): move07-06 21:49:04.739: E/test(409): move07-06 21:49:04.749: E/test(409): move07-06 21:49:04.769: E/test(409): move07-06 21:49:04.769: E/test(409): up

The above component btn1 achieves simultaneous movement of touch events, but can test up events. However, when you move components, you cannot move the scroll bar.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.