选项卡布局在页面更改后滑动,并在动态选项卡布局中滑动

时间:2018-05-25 05:23:48

标签: android android-tablayout

Tablayout在页面滚动后滑动并获得中心。我想在页面更改后解决此问题。带有viewpager的动态选项卡布局。当Tab指示符大小按照选项卡设置时,它的工作正常,但当我设置标签指示符时,它会对标题产生问题。我感谢您的支持和帮助。 在我的应用中检查问题链接https://i.stack.imgur.com/jZYe6.gif

<android.support.design.widget.TabLayout
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/tab_layout"
                style="@style/MyCustomTabLayout"
                android:layout_width="wrap_content"
                android:layout_height="34.3dp"
                android:layout_below="@+id/iv_logo"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5.5dp"
                app:tabBackground="@android:color/transparent"
                app:tabContentStart="0dp"
                app:tabIndicatorColor="@color/colorGreen"
                app:tabIndicatorHeight="1.4dp"
                app:tabSelectedTextColor="@color/colorGreen"
                app:tabTextColor="#e13d3833"
                app:tabMode="scrollable"
                app:tabGravity="center"/>

            <View
                android:id="@+id/top_line"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@+id/tab_layout"
                android:background="@color/colorEditLine" />


            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />



 <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
            <item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
        </style>

        <style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
            <item name="android:textSize">11.5sp</item>
            <item name="textAllCaps">true</item>
        </style>






     private void setupTabs(final List<Categories> list) {
                tabLayout.addTab(tabLayout.newTab().setText(Constant.POPULAR));
                tabLayout.addTab(tabLayout.newTab().setText(Constant.SPECIAL));
         ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager(), getActivity());
                for (int i = 0; i < tabLayout.getTabCount(); i++) {
                    Bundle bundle = new Bundle();
                    bundle.putString("name", tabLayout.getTabAt(i).getText().toString());
                    if (i > 1) {
                        bundle.putSerializable("category", list.get(i - 2));
                    }
                    Fragment fragment = new Home();
                    fragment.setArguments(bundle);
                    adapter.addFrag(fragment, tabLayout.getTabAt(i).getText().toString());
                }

                viewpager.setOffscreenPageLimit(3);
                viewpager.setAdapter(adapter);
                tabLayout.setupWithViewPager(viewpager);

                wrapTabIndicatorToTitle(tabLayout, 30, 30);
                }


                class ViewPagerAdapter extends FragmentPagerAdapter {
                private final List<Fragment> mFragmentList = new ArrayList<>();
                private final List<String> mFragmentTitleList = new ArrayList<>();

                private Context context;
                private LayoutInflater layoutInflater;
                public ViewPagerAdapter(FragmentManager manager, Context context) {
                    super(manager);
                    this.context = context;
                }
                @Override
                public Fragment getItem(int position) {
                    return mFragmentList.get(position);
                }

                @Override
                public int getCount() {
                    return mFragmentList.size();
                }

                public void addFrag(Fragment fragment, String title) {
                    mFragmentList.add(fragment);
                    mFragmentTitleList.add(title);
                }

                @Override
                public CharSequence getPageTitle(int position) {
                    return mFragmentTitleList.get(position);
                }
            }



public void wrapTabIndicatorToTitle(TabLayout tabLayout, int externalMargin, int internalMargin) {
        View tabStrip = tabLayout.getChildAt(0);
        if (tabStrip instanceof ViewGroup) {
            ViewGroup tabStripGroup = (ViewGroup) tabStrip;
            int childCount = ((ViewGroup) tabStrip).getChildCount();
            for (int i = 0; i < childCount; i++) {
                View tabView = tabStripGroup.getChildAt(i);
                //set minimum width to 0 for instead for small texts, indicator is not wrapped as expected
                tabView.setMinimumWidth(0);
                // set padding to 0 for wrapping indicator as title
                tabView.setPadding(0, tabView.getPaddingTop(), 0, tabView.getPaddingBottom());
                // setting custom margin between tabs
                if (tabView.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
                    ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) tabView.getLayoutParams();
                    if (i == 0) {
                        // left
                        setMargin(layoutParams, externalMargin, internalMargin);
                    } else if (i == childCount - 1) {
                        // right
                        setMargin(layoutParams, internalMargin, externalMargin);
                    } else {
                        // internal
                        setMargin(layoutParams, internalMargin, internalMargin);
                    }
                }
            }

            tabLayout.requestLayout();
        }
    }

1 个答案:

答案 0 :(得分:1)

如下所示是为标签更改指示符的代码,您需要根据您的要求更改它,您可以在其中给出不同的(对于您需要在方法参数和内部方法中更改的不同)或每个标签的相同填充。因此,您可以为每个指针制作不同的尺寸指示器。如果您需要更改特定或每个指定尺寸的指标宽度,那么此示例方法将对您有所帮助。

public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }
相关问题