V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hfl1995
V2EX  ›  Android

Android 开发之获得 Tablayout 中子 Tab 所在的 View

  •  
  •   hfl1995 · 2017-04-26 15:37:04 +08:00 · 6278 次点击
    这是一个创建于 2528 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近项目中想做一个新手提示的功能,就是在指定的 View 上弹出一个类似 PopupView 的气泡提示框。

    效果见下图: Screenshot_20170426-151801_01.png

    预想在 TabLayout 的某个子 View 上弹出这个提示框,但是发现在现有公开的方法中取不到相应的 View 对象,研究了一下源码后,发现可以使用反射取到相应的 View 对象。

    但是怎么获取下图中红圈中的 View 对象呢?

    ZZZZZZ.png

    
    public voidaddTab(@NonNullTabtab, intposition, booleansetSelected) {
    
      if(tab.mParent!=this) {
    
      throw newIllegalArgumentException("Tab belongs to a different TabLayout.");
    
      }
    
      configureTab(tab,position);
    
      addTabView(tab);//这个是增加子 Tab 的方法,我们重点看这个方法
    
      if(setSelected) {
    
        tab.select();
    
      }
    
    }
    
    

    我们接下来重点研究那个 addTabView 方法。

    private void addTabView(Tab tab) {
            final TabView tabView = tab.mView;
            mTabStrip.addView(tabView, tab.getPosition(), createLayoutParamsForTabs());
        }
    

    发现了一个 TabView 对象,跟这个方法点进去看看。 TabView 源码:

    class TabView extends LinearLayout implements OnLongClickListener {
            private Tab mTab;
            private TextView mTextView;
            private ImageView mIconView;
    
            private View mCustomView;
            private TextView mCustomTextView;
            private ImageView mCustomIconView;
    
            private int mDefaultMaxLines = 2;
    
            public TabView(Context context) {
                super(context);
                if (mTabBackgroundResId != 0) {
                    ViewCompat.setBackground(
                            this, AppCompatResources.getDrawable(context, mTabBackgroundResId));
                }
                ViewCompat.setPaddingRelative(this, mTabPaddingStart, mTabPaddingTop,
                        mTabPaddingEnd, mTabPaddingBottom);
                setGravity(Gravity.CENTER);
                setOrientation(VERTICAL);
                setClickable(true);
                ViewCompat.setPointerIcon(this,
                        PointerIconCompat.getSystemIcon(getContext(), PointerIconCompat.TYPE_HAND));
            }
    
           //...此处省略若干行代码
        }
    

    有 TextView 对象也有 ImageView 对象,还有各种布局的方法,这时候有个猜测,是不是这个 Tabview 就是我们想要得到的 View 对象呢? 那怎么得到这个 TabView 对象呢?注意下面 addTab 中的这个方法

     final TabView tabView = tab.mView;
    

    获取到当前的 Tab 就可了。而获取 tab 是有公开的方法的:

    /**
         * Returns the tab at the specified index.
         */
     @Nullable
     public Tab getTabAt(int index) {
            return (index < 0 || index >= getTabCount()) ? null : mTabs.get(index);
        }
    

    传入子 Tab 的角标就可以了。

    至此,基本上就可以开始写代码了。

    public View getTabView(int index){
      View tabView = null;
      TabLayout.Tab tab = binding.tabs.getTabAt(index);
              Field view = null;
              try {
                  view = TabLayout.Tab.class.getDeclaredField("mView");
              } catch (NoSuchFieldException e) {
                  e.printStackTrace();
              }
              view.setAccessible(true);
              try {
                  tabView = (View) view.get(tab);
              } catch (IllegalAccessException e) {
                  e.printStackTrace();
              }
      return tabView;
     }
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   951 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 541ms · UTC 21:10 · PVG 05:10 · LAX 14:10 · JFK 17:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.