美文网首页
android TabLayout 添加item点击事件的帮助类

android TabLayout 添加item点击事件的帮助类

作者: 风___________ | 来源:发表于2018-08-16 13:52 被阅读29次

    使用:

    /* 拦截tablayout点击事件 */
            View.OnTouchListener tabOnClickListener = new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    int pos = (int) view.getTag();
                    if (pos==0 && StringDetectionHelper.isBlackString(userDetail.getMobile())) {
    // 拦截第一个item点击添加自定义逻辑
                        return true;
                    }
                    if (pos==1 && StringDetectionHelper.isBlackString(userDetail.getEmail())) {
    // 拦截第二个item点击
                        return true;
                    }
                    return false;
                }
            };
            TabLayoutAddOnClickHelper.AddOnClick(tabLayout,tabOnClickListener);
    

    工具类:

    
    import android.support.design.widget.TabLayout;
    import android.view.View;
    
    import java.lang.reflect.Field;
    /**
     * TabLayout 添加点击事件的帮助类
     * */
    
    public class TabLayoutAddOnClickHelper {
        public static void AddOnClick(TabLayout tabLayout, View.OnTouchListener listener){
            for (int i=0;i<tabLayout.getTabCount();i++) {
                View view = getTabView(tabLayout,i);
                if (view == null) continue;
                view.setTag(i);
                view.setOnTouchListener(listener);
            }
        }
        // 获取tabview
        private static View getTabView( TabLayout tabLayout,int index){
            TabLayout.Tab tab = tabLayout.getTabAt(index);
            if (tab == null) return null;
            View tabView = null;
            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 TabLayout 添加item点击事件的帮助类

          本文链接:https://www.haomeiwen.com/subject/ufwwbftx.html