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

最近项目中想做一个新手提示的功能,就是在指定的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;
 }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,008评论 25 708
  • 班级情况 校区:科学创想机器人茂业店 时间:周日4:30-5:30 学员:孟渲淇,王子瑞 教师:李飞 教学目标 1...
    A越单纯越幸福阅读 675评论 0 1
  • 周末极好的。譬如现在,六月的第四天,江边。街灯从桥头开始亮起来,从汉口老的租借区到武昌,也是老旧的区。那里曾有个小...
    愁雨斋阅读 300评论 0 0
  • 今天喵爸很早就回来了,因为早上去的挺早,所以在时隔快半年后再次享受到五点半下班的快感。 还是挺不容易的,喵爸吃完饭...
    肥木有阅读 292评论 1 2
  • 隔壁的大姐是一个悲观型的人,为什么这么说呢,因为她说的每一句话的前面总是喜欢加个“不”字,虽然她可能是好心,可总让...
    李在在阅读 298评论 0 3