Android onInterceptTouchEvent和dispatchTouchEventAndroid有什么区别?
根据android开发者指南,这两种方法都可以用来拦截触摸事件(MotionEvent),但有什么区别?
怎么办onInterceptTouchEvent,dispatchTouchEvent和onTouchEvent视图的层次结构中一起互动(ViewGroup)?
java android event-listener event-handling android-touch-event
我们可以在Android中使用缩放手势检测器进行缩放缩放吗?
我有一个横向RecyclerView带leftPadding = 48dp,topPadding = 24dp和clipToPadding = false.它从左侧的空白区域开始,但是当用户滚动列表时,其项目将在该(先前为空)空间上绘制.顶部空间始终为空.
这RecyclerView是在FrameLayout中foreground = selectableItemBackground.
我的问题来自这样一个事实,即RecyclerView消耗和忽略左侧和顶部空间的触摸,这意味着OnClickListener当连接到FrameLayout或者连接时都不会触发RecyclerView.
我已经与尝试clickable = false,并focusable = false在RecyclerView,这是行不通的.
我在找什么:
编辑:我已经创建了一个简单的项目,显示我正在谈论的问题:https://github.com/dadino/recyclerviewemptyspacestest 有2个提交,在第一个我试图抓住父视图的点击,在第二个我试图抓住RecyclerView本身的点击.它们都不起作用.
android android-custom-view kotlin android-recyclerview android-touch-event
首先,这是一个最初在这里提出的跟进问题,在Android中平移,缩放和缩放自定义的Canvas绘图视图
由于还没有答案,我终于使用android-gesture-detector解决了我的问题
应用缩放/缩放手势后,我发现,画布绘制坐标仍然指向旧位置(在应用缩放之前),而不是绘制完全相同的触摸坐标.基本上,在缩放或拖动画布后,我无法获得正确的画布坐标.
在缩放之前,
缩小后,触摸点将绘制在之前的位置.我希望它能够在当前触摸位置上绘制,
示例代码,
public class DrawingView extends View {
    private void setupDrawing() {
        mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleListener());
        mgd = new MoveGestureDetector(ctx, mgl);
        sgd = new ScaleGestureDetector(ctx, sgl);
        rgd = new RotateGestureDetector(ctx, rgl);
}
class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor();
            // Don't let the object get too small or too large.
            mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
            invalidate();
            return true;
        }
    }
    MoveGestureDetector.SimpleOnMoveGestureListener mgl = new …android android-canvas android-view android-gesture android-touch-event
我SeekBar在我的Android应用程序中使用了一个.当用户单击任意位置时SeekBar,其进度值会发生变化.我只希望在用户滑动SeekBar拇指时改变进度值(就像UISlider在iOS中一样).
我已经尝试将clickable属性设置SeekBar为false但这不起作用.我怎样才能达到理想的行为?
我正在寻找一个适当的触摸事件,当用户触摸地图时会触发该事件(谷歌地图Android API).有没有人知道如何做到这一点?
我使用的是NestedScrollWebView(在很大程度上受到影响NestedScrollView),这可以解决许多已知 问题与使用相关联WebView的CoordinatorLayout.
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="enterAlways|scroll|snap">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:titleTextAppearance="@style/TextAppearance.AppCompat.Subhead"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_gravity="bottom"
                android:visibility="gone"
                style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>
        </FrameLayout>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <com.example.app.widget.NestedScrollWebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
这些NestedScrollWebView作品很好用AppBarLayout和enterAlways|scroll|snap滚动标志,但不是很好SwipeRefreshLayout.
我删除了以下行NestedScrollWebView:
setOverScrollMode(WebView.OVER_SCROLL_NEVER);
这样可以确保视图不会显示在滚动指示器上.我想要显示滚动指示符,但即使启用了过度滚动,也不会出现以下情况.
根据我的理解,SwipeRefreshLayout应该拦截导致刷新指示符被拖动的任何触摸事件(基于其实现onInterceptTouchEvent()).SwipeRefreshLayout也拒绝任何requestDisallowInterceptTouchEvent()已启用嵌套滚动的子项; 这是真的NestedScrollWebView.因此,应按 …
android swiperefreshlayout android-touch-event android-nestedscrollview
我的主布局中有一个用于根容器的motionLayout。在里面,还有其他的景色。其中之一是 frameLayout,包含一个片段。该片段是一个页面,由一个 NestedScrollView 等组成... MotionLayout 具有仅用于水平滑动的 OnSwipe,而 NestedScrollView 应该只能垂直滚动。我不得不扩展 MotionLayout onInterceptTouchEvent 方法(查看下面的代码),并且只将那些非水平触摸事件传递给孩子们。到目前为止非常简单,并且有效。
问题是,当我开始在吸收某种触摸事件(如按钮或 NestedScrollView)的视图上滑动时,它会混淆 motionLayout 转换并立即跳过帧,因此锚点与我的鼠标位置相对应。过渡几乎完全在一个帧中进行,并扼杀了 UI 体验。我的猜测是,如果 motionLayout 将 X1 和 X2 用于转换,则 X1 值是导致问题的值,其值来自不应该出现的位置。但是当然,当我开始在触摸吸收元素上滑动时。
这是我的 customMotionLayout 的 OnInterceptTouchEvent 的覆盖方法:
 override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        super.onInterceptTouchEvent(event)
        return when (event.actionMasked) {
            MotionEvent.ACTION_DOWN -> {
                previousX = event.x.toInt()
                previousY = event.y.toInt()
                mIsScrolling = false
                false
            }
            MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> {
                mIsScrolling = false
                false
            }
            MotionEvent.ACTION_MOVE -> {
                if (mIsScrolling) true
                 else {
                    var xMotion: Int …android android-touch-event android-constraintlayout android-motionlayout
我很高兴谷歌发布了基于 RecyclerView 构建的ViewPager2,解决了旧 ViewPager 存在的很多问题。
我很快将旧的 ViewPager 代码替换为 ViewPager2:
用 xml 中的 ViewPager2 替换 ViewPager
替换FragmentPagerAdapter(FragmentManager)为FragmentStateAdapter(Fragment)
ViewPager 设置如下:
viewPager.adapter = fragmentAdapter
val mediator = TabLayoutMediator(tabLayout, viewPager, true) { tab, position ->
    tab.text = fragmentAdapter.tabNames[position]
}
mediator.attach()
没有进行其他更改。
执行上述更改后,我意识到一个问题——
现在我的 ViewPager 是一个普通的水平寻呼机,我的每个片段fragmentAdapter都有一个垂直的RecyclerView.
我观察到,当 RecyclerView 的滚动位置为 0 时,该 RecyclerView 中的项目无法接收任何单击或长按事件,但可以滚动。一旦滚动,它可以再次收到点击。*
知道这ViewPager2也是一个RecyclerView,是否与嵌套的 RecyclerView 有关系?
android android-viewpager android-recyclerview android-touch-event android-viewpager2
我遇到了一个问题MotionLayout,如果用户在可点击的项目上开始滑动,则无法向上/向下滑动。我希望用户能够在屏幕的任何部分上滑动,并触发滑动过渡。
基本上,该行为与 Android 版 Yelp 应用程序当前的行为非常相似。在屏幕上的任意位置向上滑动会将搜索栏移动到屏幕顶部。请观看有关滑动行为的视频。
我的转换代码如下所示:
<Transition
    app:constraintSetStart="@id/start"
    app:constraintSetEnd="@id/end"
    >
    <OnSwipe
        app:dragDirection="dragUp"
        app:onTouchUp="decelerate"
        />
</Transition>
我的代码看起来MotionLayout像这样:
<androidx.constraintlayout.motion.widget.MotionLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:layoutDescription="@xml/motion_scene">
     >
     <!-- other views -->
     <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/title"
           android:textColor="@color/black"
           android:textSize="14sp"
           android:lineSpacingExtra="7sp"
           android:fontFamily="@font/avenir"
           android:layout_marginBottom="16dp"
           />
       <GridView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numColumns="2"
            android:verticalSpacing="8dp"
            android:horizontalSpacing="12dp"
            android:stretchMode="columnWidth"
            tools:listitem="@layout/griditem" <!-- This layout file contains a single <Button/> -->
            />
    </LinearLayout>
</androidx.constraintlayout.motion.widget.MotionLayout>
以下是我尝试过但无法正常工作的所有内容:
onInterceptTouchEvent父ViewGroup视图(此处仅返回 true 将导致我想要的滑动行为,但随后应该可点击的子视图不再可点击。)override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean { …android android-animation android-touch-event android-swipe android-motionlayout