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);
Doesn't work and does NOT look pretty. It is all very handy except for the longClick I want to implement.
My small snippet of code
pagerAdapter = new CustomFragmentPagerAdapter(getSupportFragmentManager());
pagerView.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(pagerView);
The tablayout has a method setOnLongClickListener()
but what I can tell, it does nothing.
How can I implement a LongClickListener for a tab in a tablayout?
You can do
mTabLayout.getChildAt(0).setOnLongClickListener
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?).
The tabs itself reside in a SlidingTabStrip
which extends LinearLayout
and 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;
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With