在Android上添加OnLongClickListener支持TabLayout | TabLayout.Tab [英] Add OnLongClickListener on android support TabLayout | TabLayout.Tab

查看:387
本文介绍了在Android上添加OnLongClickListener支持TabLayout | TabLayout.Tab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用服务库中的TabLayout.长按选项卡时,我尝试添加OnLongClickListener.对我来说这是不可能的.我什至试图通过使用childViews来破解"它:

I am using the TabLayout found in the Service library. I have tried to add an OnLongClickListener when long-clicking on a tab. It has been impossible for me. I have even been trying to "hack" it by using childViews:

View tabLayoutChild = tabLayout.getChildAt(0);
    ViewGroup group = (ViewGroup) tabLayoutChild;
    group.getChildAt(0).setOnLongClickListener(this);

不起作用,看起来也不漂亮.除了我要实现的longClick之外,一切都非常方便.

Doesn't work and does NOT look pretty. It is all very handy except for the longClick I want to implement.

我的一小段代码

pagerAdapter = new CustomFragmentPagerAdapter(getSupportFragmentManager());
    pagerView.setAdapter(pagerAdapter);
    tabLayout.setupWithViewPager(pagerView);

tablayout具有方法setOnLongClickListener(),但我可以告诉它,它什么也没做.

The tablayout has a method setOnLongClickListener() but what I can tell, it does nothing.

如何为tablayout中的标签实现LongClickListener?

How can I implement a LongClickListener for a tab in a tablayout?

推荐答案

您可以

mTabLayout.getChildAt(0).setOnLongClickListener

将其设置在选项卡主机上,但这意味着它仅在您点击TabHost中不包含选项卡的空间(背景?)时才会触发.

to set it on the tab host, but this mean that it only triggers when you tap the space in the TabHost that doesn't contain a tab (background?).

选项卡本身位于扩展LinearLayoutSlidingTabStrip中,我们可以使用它来访问每个选项卡.因此,我们为每个标签设置长按监听器,如下所示:

The tabs itself reside in a SlidingTabStrip which extends LinearLayoutand we can use it to get to each tab. So we set the long press listener per tab like this:

LinearLayout tabStrip = (LinearLayout) mTabLayout.getChildAt(0);
for (int i = 0; i < tabStrip.getChildCount(); i++) {
    tabStrip.getChildAt(i).setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            return false;
        }
    });
}

这篇关于在Android上添加OnLongClickListener支持TabLayout | TabLayout.Tab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆