Android,Kotlin:如何仅在需要时才允许我的视图寻呼机向左滑动

问题描述 投票:0回答:2

你好,亲爱的观众。

我的情况:

我有一个viewpager,可让我的用户左右滑动。我有一个屏幕(一个片段),我想由用户完成才能刷卡。

我做了什么:

我自定义了viewpager以在希望锁定时锁定滑动(请参见我的代码),但是它同时禁用了两个方向。

理想情况下,我只想禁用从左向右滑动:我希望用户可以向后滑动(从右向左)。我只想禁用向前滑动(从左到右)。

[我试图在我的视图寻呼机上实现一个onTouchListener来检测滑动方向,但仅在ViewPager解锁时才有效。

你能帮我吗?

编辑3:新问题

我已经尝试实现viewpager2,但是现在我的子级Layout出现了问题。

当我需要将一个片段显示为另一个片段时,我使用了基于此编写的扩展方法:how-to-add-a-fragment-in-kotlin-way

看起来像这样:

    inline fun FragmentManager.doTransaction(func: FragmentTransaction.() -> Unit) {
        Log.d(TAG, "doTransaction")
        val fragmentTransaction = beginTransaction()
        fragmentTransaction.func()
        fragmentTransaction.commit()
    }

    fun AppCompatActivity.replaceFragment(frameId: Int, fragment: Fragment) {
        Log.d(TAG, "replaceFragment")
            supportFragmentManager.doTransaction { replace(frameId, fragment) }
    }

所以我有几个片段,像这样:

    companion object : CustomCompanion() {
            @JvmStatic
            override fun newInstance(activity: Activity): IdealsFragment {
                val fragment =
                    IdealsFragment(
                        activity
                    )

                (activity as NewCharacterActivity).replaceFragment(
                    R.id.fragmentIdeals_container_ideals,
                    IdealsRecyclerViewFragment.newInstance(
                        activity
                    )
                )

                return fragment
            }
        }

事实是,使用ViewPager2时,出现“找不到ID的视图”异常。

[我知道这是因为我使用了片段管理器而不是ChildSupportFragmentManager,所以我用好的片段管理器代替了片段管理器,但是现在我崩溃了:“片段尚未连接。”]]

再次,您能帮我吗?

非常感谢!

代码:

 /**
 *   Class "LockableViewPager" :
 *   Custom viewpager that allows or not to swipe between fragments.
 **/
 class LockableViewPager : ViewPager {
    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)


    companion object {
        private const val TAG = "LockableViewPager"
    }

    /**
     * Is swiping enabled ?
     */
    private var swipeEnabled = true


    /**
     * Intercept all touch screen motion events.
     * Allows to watch events as they are dispatched, and
     * take ownership of the current gesture at any point.
     * 
     * @param event : The motion event being dispatched down the hierarchy.
     * @return Return true to steal motion events from the children and have
     * them dispatched to this ViewGroup through onTouchEvent().
     * The current target will receive an ACTION_CANCEL event, and no further
     * messages will be delivered here.
     */
    override fun onTouchEvent(event: MotionEvent): Boolean {
        return when (swipeEnabled) {
            true -> super.onTouchEvent(event)
            false -> //  Do nothing.
                return false
        }
    }


    /**
     * Implement this method to intercept all touch screen motion events.  This
     * allows you to watch events as they are dispatched to your children, and
     * take ownership of the current gesture at any point.
     *
     * @param event : The motion event being dispatched down the hierarchy.
     * @return Return true to steal motion events from the children and have
     * them dispatched to this ViewGroup through onTouchEvent().
     * The current target will receive an ACTION_CANCEL event, and no further
     * messages will be delivered here.
     */
    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        return when (swipeEnabled) {
            true -> super.onInterceptTouchEvent(event)
            false -> swipeEnabled
        }
    }

    /**
     *  Sets the swipeEnabled value to lock or unlock swiping
     */
    fun setSwipeEnabled(swipeEnabled: Boolean) {
        this.swipeEnabled = swipeEnabled
    }    
}

编辑2:也许是解决方案?

我在包含视图寻呼机的活动布局的底部放了一个原始视图:

<View
    android:id="@+id/touch_layer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true" />

我创建了一个实现View.OnTouchListener的抽象类:

abstract class OverlayTouchListener : View.OnTouchListener {
    companion object{
        private const val TAG = "OverlayTouchListener"
    }
}

反序列化我活动中的叠加层视图:

var touchLayer = this.findViewById<View>(R.id.touch_layer)

将OverlayTouchListener添加到touchLayer:

touchLayer?.setOnTouchListener(object : OverlayTouchListener() {
            /**
             * Called when a touch event is dispatched to a view. This allows listeners to
             * get a chance to respond before the target view.
             *
             * @param v The view the touch event has been dispatched to.
             * @param event The MotionEvent object containing full information about
             * the event.
             * @return True if the listener has consumed the event, false otherwise.
             */
            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
                Log.d(TAG, "onTouch")
                if(event != null){
                    //  Check the swiping direction and ViewPager.isSwipeEnabled 
                    viewPager?.setSwipeEnabled(true)
                    viewPager?.onTouchEvent(event)
                }

                return true
            }

        })

现在,我只需要检查是否启用了滑动。如果不是,则仅在从右向左滑动时才调用viewPager.onTouchEvent。

有点棘手,所以如果您有任何建议...

您好,亲爱的观众。我的情况:我有一个viewpager,可以让我的用户左右滑动。我有一个屏幕(一个片段),我想由用户完成才能刷卡。 ...

android android-fragments kotlin android-viewpager android-swipe
2个回答
0
投票

首先,我建议使用ViewPager2,因为它通过RecyclerView实现进行了大幅优化,可让您优雅地删除页面而没有帧滞后。


0
投票

解决方案是使用physicsNeverScrollableScrollPhysics()

© www.soinside.com 2019 - 2024. All rights reserved.