TabLayout 的 PressedListener 事件

PressedListener event for TabLayout

我想为我的 tabLayout 设置 Press 事件侦听器。

基于按下的 tabLayout 项,我想在我的布局中突出显示另一个视图。

我知道我们有 addOnTabSelectedListener 但我想要 PressedListener

有什么解决办法吗?

获取每个选项卡视图并在每个选项卡上设置触摸侦听器,我自己没有尝试过请尝试让我知道

val tabStrip = tab.getChildAt(0)
    if (tabStrip is ViewGroup) {
        val childCount = tabStrip.childCount
        for (i in 0 until childCount) {
            val tabView = tabStrip.getChildAt(i)
            tabView.setOnTouchListener { v, event ->
                if (event.action == MotionEvent.ACTION_DOWN){
                    Log.d("touchEvent","touched $i")
                }
                return@setOnTouchListener true
            }
        }
    }

在java

   final View tabStrip = ((TabLayout) findViewById(R.id.tab)).getChildAt(0);
    int childCount = ((ViewGroup) tabStrip).getChildCount();
    for (int i = 0; i < childCount; i++) {
        View tabView = ((ViewGroup) tabStrip).getChildAt(i);
        final int finalI = i;
        tabView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Log.d("onTouch", "touched " + finalI);
                }
                return false;
            }
        });