6

考虑下图中的示例,

在此处输入图像描述

我正在寻找一种将触摸事件从画布View传递到Viewpager的解决方案。这对于同时在两个视图上应用相同数量的缩放是必要的。

这是正在发生的事情的技术描述,

(让我们将视图视为ABC

A. 父视图

B. 查看

C. 查看寻呼机

在子节点(B & C)上调用dispatchTouchEvent()之前, A.dispatchTouchEvent()将首先调用A.onInterceptTouchEvent()以查看视图组是否有兴趣拦截事件。

因此,如果A.onTouchEvent()返回false,那么它会返回到B.onTouchEvent()。如果返回false则返回到C.onTouchEvent()并且调度照常继续。

在返回true时,

  1. ACTION_CANCEL将发送给所有的孩子。

  2. 如果已定义,所有后续手势事件(直到ACTION_UP / ACTION_CANCEL)将由事件侦听器(OnTouchListener.onTouch())使用,否则事件处理程序A.onTouchEvent()在 A 级别。

此时我可以将事件从父视图传递给子视图,但不能让 2 个子视图B。& C一起处理事件(在两个视图上应用相同数量的缩放)。

有没有办法将触摸事件从父视图传递给子视图,以便他们可以同时处理事件?

这是我的布局设置,

<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>
4

2 回答 2

5

如果我没记错的话,您想在顶部和底部视图上处理相同的触摸事件。根据 Android 触摸架构,您不能同时处理来自多个视图的触摸事件。您必须在方法内处理它onTouchEvent()或传递到下一个视图。您还需要等到顶视图完成处理MotionEvent() 并让子视图处理。

这是使用RxAndroid方法的可能解决方案,

在您的画布视图中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()订阅运动事件,这样无论何时从画布上进行更改,它都会立即传递事件。

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);
                    }
            });

RxBus类如下所示

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;
    }
}
于 2016-12-13T05:46:41.853 回答
2

使用回调很容易实现。只需使用您需要的任何方法创建一个接口,然后在您的 Viewpager 中实现。将根据您的触摸事件在您的 View 类中调用调用,并且响应将来自 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

}
 }

我的 Fragment 类带有一个简单的按钮:

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 文件。如果有人需要,我只是分享:

活动主:

<?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>

内容主:

<?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>
于 2016-12-08T04:28:04.813 回答