Android 中多个视图的同时触摸事件处理
Simultaneous touch events handling on multiple views in Android
考虑下图中的示例,
我正在寻找将触摸事件从 canvas View 传递到 Viewpager 的解决方案。这对于同时在两个视图上应用相同数量的缩放是必要的。
这是对正在发生的事情的技术描述,
(让我们将视图视为 A、B & C)
一个。 Parent 查看
乙。查看
C。查看寻呼机
在 dispatchTouchEvent() 被调用之前 children (B & C), A.dispatchTouchEvent()会先调用A.onInterceptTouchEvent()查看视图组是否有兴趣拦截事件
所以如果 A.onTouchEvent() returns false, 那么它会回到 B.onTouchEvent()。如果那个 returns false 那么它会回到 C.onTouchEvent() 并且调度继续照常进行。
返回后 true,
ACTION_CANCEL将派发给所有child人。
所有后续手势事件(直到ACTION_UP/ACTION_CANCEL)将被事件侦听器使用(OnTouchListener.onTouch())如果定义,否则事件处理程序 A.onTouchEvent() 在A级.
此时我可以将事件从 parent 传递到 child 视图但不能让 2 child 视图 B. & C 一起处理事件(在两个视图上应用相同数量的缩放)。
有没有办法将触摸事件从 parent 视图传递到 child 视图,以便它们可以同时处理这些事件?
这是我的布局设置,
<RelativeLayout
android:id="@+id/layoutParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF">
<com.androidapp.NonSwipeableViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/transparent" />
<com.androidapp.DrawingView
android:id="@+id/drawing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent" />
</RelativeLayout>
使用回调实现起来非常简单。只需使用您需要的任何方法创建一个接口,然后在您的 Viewpager 中实现。调用将根据您的触摸事件在您的视图 class 中调用,响应将到达 Viewpager。我在我的 MainActivity 中创建了一个带有 ImageView 的示例,并调用以从不同的片段更改其大小并且它有效。下面是我的代码:
public interface ICallBackForResize {
void resizeme();
void actionUp();
void actionDown();
}
主要活动:
public class MainActivity extends AppCompatActivity implements ICallBackForResize {
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img= (ImageView) findViewById(R.id.image);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void resizeme() {
Toast.makeText(MainActivity.this,"called",Toast.LENGTH_LONG).show();
img.getLayoutParams().height += 20;
img.getLayoutParams().width += 20;
img.requestLayout();
}
@Override
public void actionUp() {
//do whatever
}
@Override
public void actionDown() {
//do whatever
}
}
我的片段 class 带有一个简单的按钮:
public class MyFragmentButton extends Fragment {
View view;
ICallBackForResize callBackForResize;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callBackForResize= (ICallBackForResize) getActivity();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view=inflater.inflate(R.layout.mybutton,container,false);
Button btn= (Button) view.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callBackForResize.resizeme();
}
});
return view;
}
}
我相信您不需要 xml 文件。如果有人需要,我只是分享:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"/>
</android.support.design.widget.CoordinatorLayout>
content_main:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="nanofaroque.com.addlistener.MainActivity">
<ImageView
android:id="@+id/image"
android:text="Hello World!"
android:layout_width="100dp"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher"
android:layout_height="100dp" />
<fragment
android:name="nanofaroque.com.addlistener.MyFragmentButton"
android:id="@+id/headlines_fragment"
android:layout_width="100dp"
android:layout_height="100dp" />
</FrameLayout>
我的按钮:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
如果我没记错的话,您想在顶视图和底视图上处理相同的触摸事件。根据 Android 触摸架构,您无法同时处理来自多个视图的触摸事件。您要么必须在 onTouchEvent()
方法中处理它,要么传递给下一个视图。您还需要等到顶视图处理完 MotionEvent(
) 并让子视图处理。
这是使用 RxAndroid 方法的可能解决方案,
在您的 canvas 视图中 MotionEvent()
、
@Override
public boolean onTouchEvent(MotionEvent event) {
// process all touch events (UP/DOWN/MOVE)
//send events as observable
RxBus.getInstance().sendMotionEvent(event);
return true;
}
这将观察运动事件并通知所有观察者。
现在,在您的 viewpager 中 onResume()
订阅运动事件,这样每当 canvas 发生变化时,它都会立即传递事件。
Subscription RxBus _rxBus = RxBus.getInstance();
subscription = _rxBus.getMotionEvent()
.subscribe(new Action1<MotionEvent>() {
@Override
public void call(MotionEvent event) {
// call the onTouchEvent of your widget e.g. ImageView and pass the received event
// this will apply the same touch event that was applied in the canvas
// .onTouchEvent(event) is important to override the onTouchEvent() of your widget that will use the touch event
imageView.onTouchEvent(event);
}
});
下面给出RxBusclass,
public class RxBus {
private static RxBus instance;
private final PublishSubject<MotionEvent> motion_event = PublishSubject.create();
public static RxBus getInstance() {
if (instance == null) {
instance = new RxBus();
}
return instance;
}
public void sendMotionEvent(MotionEvent motionEvent) {
motion_event.onNext(motionEvent);
}
public Observable<MotionEvent> getMotionEvent() {
return motion_event;
}
}
考虑下图中的示例,
我正在寻找将触摸事件从 canvas View 传递到 Viewpager 的解决方案。这对于同时在两个视图上应用相同数量的缩放是必要的。
这是对正在发生的事情的技术描述,
(让我们将视图视为 A、B & C)
一个。 Parent 查看
乙。查看
C。查看寻呼机
在 dispatchTouchEvent() 被调用之前 children (B & C), A.dispatchTouchEvent()会先调用A.onInterceptTouchEvent()查看视图组是否有兴趣拦截事件
所以如果 A.onTouchEvent() returns false, 那么它会回到 B.onTouchEvent()。如果那个 returns false 那么它会回到 C.onTouchEvent() 并且调度继续照常进行。
返回后 true,
ACTION_CANCEL将派发给所有child人。
所有后续手势事件(直到ACTION_UP/ACTION_CANCEL)将被事件侦听器使用(OnTouchListener.onTouch())如果定义,否则事件处理程序 A.onTouchEvent() 在A级.
此时我可以将事件从 parent 传递到 child 视图但不能让 2 child 视图 B. & C 一起处理事件(在两个视图上应用相同数量的缩放)。
有没有办法将触摸事件从 parent 视图传递到 child 视图,以便它们可以同时处理这些事件?
这是我的布局设置,
<RelativeLayout
android:id="@+id/layoutParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF">
<com.androidapp.NonSwipeableViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/transparent" />
<com.androidapp.DrawingView
android:id="@+id/drawing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent" />
</RelativeLayout>
使用回调实现起来非常简单。只需使用您需要的任何方法创建一个接口,然后在您的 Viewpager 中实现。调用将根据您的触摸事件在您的视图 class 中调用,响应将到达 Viewpager。我在我的 MainActivity 中创建了一个带有 ImageView 的示例,并调用以从不同的片段更改其大小并且它有效。下面是我的代码:
public interface ICallBackForResize {
void resizeme();
void actionUp();
void actionDown();
}
主要活动:
public class MainActivity extends AppCompatActivity implements ICallBackForResize {
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img= (ImageView) findViewById(R.id.image);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void resizeme() {
Toast.makeText(MainActivity.this,"called",Toast.LENGTH_LONG).show();
img.getLayoutParams().height += 20;
img.getLayoutParams().width += 20;
img.requestLayout();
}
@Override
public void actionUp() {
//do whatever
}
@Override
public void actionDown() {
//do whatever
}
}
我的片段 class 带有一个简单的按钮:
public class MyFragmentButton extends Fragment {
View view;
ICallBackForResize callBackForResize;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callBackForResize= (ICallBackForResize) getActivity();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view=inflater.inflate(R.layout.mybutton,container,false);
Button btn= (Button) view.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callBackForResize.resizeme();
}
});
return view;
}
}
我相信您不需要 xml 文件。如果有人需要,我只是分享:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"/>
</android.support.design.widget.CoordinatorLayout>
content_main:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="nanofaroque.com.addlistener.MainActivity">
<ImageView
android:id="@+id/image"
android:text="Hello World!"
android:layout_width="100dp"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher"
android:layout_height="100dp" />
<fragment
android:name="nanofaroque.com.addlistener.MyFragmentButton"
android:id="@+id/headlines_fragment"
android:layout_width="100dp"
android:layout_height="100dp" />
</FrameLayout>
我的按钮:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
如果我没记错的话,您想在顶视图和底视图上处理相同的触摸事件。根据 Android 触摸架构,您无法同时处理来自多个视图的触摸事件。您要么必须在 onTouchEvent()
方法中处理它,要么传递给下一个视图。您还需要等到顶视图处理完 MotionEvent(
) 并让子视图处理。
这是使用 RxAndroid 方法的可能解决方案,
在您的 canvas 视图中 MotionEvent()
、
@Override
public boolean onTouchEvent(MotionEvent event) {
// process all touch events (UP/DOWN/MOVE)
//send events as observable
RxBus.getInstance().sendMotionEvent(event);
return true;
}
这将观察运动事件并通知所有观察者。
现在,在您的 viewpager 中 onResume()
订阅运动事件,这样每当 canvas 发生变化时,它都会立即传递事件。
Subscription RxBus _rxBus = RxBus.getInstance();
subscription = _rxBus.getMotionEvent()
.subscribe(new Action1<MotionEvent>() {
@Override
public void call(MotionEvent event) {
// call the onTouchEvent of your widget e.g. ImageView and pass the received event
// this will apply the same touch event that was applied in the canvas
// .onTouchEvent(event) is important to override the onTouchEvent() of your widget that will use the touch event
imageView.onTouchEvent(event);
}
});
下面给出RxBusclass,
public class RxBus {
private static RxBus instance;
private final PublishSubject<MotionEvent> motion_event = PublishSubject.create();
public static RxBus getInstance() {
if (instance == null) {
instance = new RxBus();
}
return instance;
}
public void sendMotionEvent(MotionEvent motionEvent) {
motion_event.onNext(motionEvent);
}
public Observable<MotionEvent> getMotionEvent() {
return motion_event;
}
}