problems:
- tabs width.
as you see in my design (Figma) i have two buttons have two different fragments , i used tablayout with viewpager and indicator , it works but the design i got isn't as i excepted, the tabs it was too big in width ,i tried to fix it but it got worst
the XML code(house_fragment.XML):
java code(house_fragment.java):
TabLayout mTabs; View mIndicator; ViewPager mViewPager; private int indicatorWidth; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_farm, container, false); //Assign view reference mTabs = view.findViewById(R.id.tab); mIndicator = view.findViewById(R.id.indicator); mViewPager = view.findViewById(R.id.viewPager); //Set up the view pager and fragments TabFragmentAdapter adapter = new TabFragmentAdapter(getChildFragmentManager()); adapter.addFragment(info_fragment.newInstance("param1", "param2"), "Info"); adapter.addFragment(stat_fragment.newInstance("param1", "param2"), "Statistic"); mViewPager.setAdapter(adapter); mTabs.setupWithViewPager(mViewPager); //Determine indicator width at runtime mTabs.post(new Runnable() { @Override public void run() { indicatorWidth = mTabs.getWidth() / mTabs.getTabCount(); //Assign new width FrameLayout.LayoutParams indicatorParams = (FrameLayout.LayoutParams) mIndicator.getLayoutParams(); indicatorParams.width = indicatorWidth; mIndicator.setLayoutParams(indicatorParams); } }); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { //To move the indicator as the user scroll, we will need the scroll offset values //positionOffset is a value from [0..1] which represents how far the page has been scrolled @Override public void onPageScrolled(int i, float positionOffset, int positionOffsetPx) { FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)mIndicator.getLayoutParams(); //Multiply positionOffset with indicatorWidth to get translation float translationOffset = (positionOffset+i) * indicatorWidth ; params.leftMargin = (int) translationOffset; mIndicator.setLayoutParams(params); } @Override public void onPageSelected(int i) { } @Override public void onPageScrollStateChanged(int i) { } }); return view; } and the adapter for fragments(TabFragmentAdapter):
public class TabFragmentAdapter extends FragmentPagerAdapter { private final List fragmentList = new ArrayList(); private final List fragmentTitleList = new ArrayList(); public TabFragmentAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { return fragmentList.get(i); } @Override public int getCount() { return fragmentList.size(); } @Override public CharSequence getPageTitle(int position) { return fragmentTitleList.get(position); } public void addFragment(Fragment fragment, String title) { fragmentList.add(fragment); fragmentTitleList.add(title); } } is there's another solution that allows me to set width that i want
the exception : Statistic selected button Info selected button the result : result i got
Источник: https://stackoverflow.com/questions/780 ... achieve-it
Мобильная версия