简体   繁体   English

Android 长按标签

[英]Android long click on tab

I have a TabHost with some tabs, and after doing a long press on a tab, I want to get the position or the tag of the tab which was long pressed, and not the current tab that is showed.我有一个带有一些选项卡的 TabHost,在长按选项卡后,我想获取 position 或长按选项卡的标签,而不是显示的当前选项卡。 Below there is some code in which I create the long press listener for the TabHost:下面是一些代码,我在其中为 TabHost 创建了长按监听器:

myTabHost.getTabWidget().getChildAt(i).setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        return false;
    }
});

Any solution?任何解决方案? Is correct to apply the listener at TabHost in my case?在我的案例中,在 TabHost 应用监听器是否正确?

I resolved my problem adding to the view of the tab a tag information , and then I attached at the view a listener that gets and prints this tag: 我解决了在标签视图中添加标签信息的问题,然后在该视图上附加了获取并打印此标签的侦听器:

View tabView= mTabHost.getTabWidget().getChildAt(i);
// set the tag information at the view of the tab (the tag contains the position number of the tab)
tabView.setTag( Integer.valueOf(i));
tabView.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub
                // I print the number position of the tab
                Log.d("tab number", ((Integer)view.getTag()).toString() );
                return false;
            }
        });

The identifier of the tab that was long-clicked resides in the View v parameter of the onLongClick function. 长时间单击的选项卡的标识符位于onLongClick函数的View v参数中。 Call v.getId() and the rest are details 调用v.getId() ,剩下的就是细节

val tabCount = binding.tabLayout.tabCount
for (index in 0 until tabCount) {
   binding.tabLayout.getTabAt(index)?.apply {
   view.isLongClickable = false
   if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
         view.tooltipText = ""
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM