【问题标题】:How to pretend a click on tabLayout in android kotlin?如何假装在android kotlin中点击tabLayout?
【发布时间】:2020-01-30 15:11:24
【问题描述】:

是否可以使用非“实际操作”的代码在 tanLayout 上单击子级? 像tab[position].performClick() 这样的东西? 有这样的东西吗?

问候

【问题讨论】:

    标签: android kotlin android-tablayout


    【解决方案1】:

    你可以使用这样的东西。

    public static void onClickTab(TabLayout tab_layout) {
            ViewGroup vg = (ViewGroup) tab_layout.getChildAt(0);
            int tabsCount = vg.getChildCount();
            for (int j = 0; j < tabsCount; j++) {
                ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
                int tabChildesCount = vgTab.getChildCount();
                for (int i = 0; i < tabChildesCount; i++) {
                    View tabViewChild = vgTab.getChildAt(i);
                    if (tabViewChild instanceof TextView) {
                        ((TextView) tabViewChild).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                // Perfor your click acrtion
                            }
                        });
                    }
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      您可以使用以下代码选择标签:

       tabs.getTabAt(1)?.select()
      

      希望对你有帮助!!

      【讨论】:

        【解决方案3】:

        如果您正在使用带有选项卡布局的视图寻呼机,那么只需使用

           mViewPager.setCurrentItem(position)
        

        或者如果不使用 viewpager 则使用

          mTabLayout.getTabAt(position).select();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多