gpt4 book ai didi

android - 选项卡布局中间选项卡滚动不流畅

转载 作者:行者123 更新时间:2023-12-05 00:06:49 26 4
gpt4 key购买 nike

选项卡布局中间选项卡在页面滚动时重新居中。具有可滚动选项卡模式和选项卡重心的动态选项卡布局。在 Viewpager 页面上滚动和选项卡选择中间选项卡在页面更改之前或之后变得困惑。您可以在此链接上检查问题

enter image description here

XML文件

<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" />

样式.xml

 <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>

首页.java

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();
}
}

最佳答案

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RadioGroup
android:id="@+id/payment_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="6dp"
android:orientation="horizontal">


<RadioButton
android:id="@+id/pizza"
style="@style/LinkButton"

android:text="pizza" />


<RadioButton
android:id="@+id/pasta"
style="@style/LinkButton"
android:text="pasta" />

<RadioButton
android:id="@+id/cheese"
style="@style/LinkButton"
android:text="cheese" />

<RadioButton
android:id="@+id/burger"
style="@style/LinkButton"
android:text="burger" />


<RadioButton
android:id="@+id/lasagne"
style="@style/LinkButton"
android:text="lasagne" />

<RadioButton
android:id="@+id/pasta"
style="@style/LinkButton"
android:text="Credited" />
<RadioButton
android:id="@+id/pasta"
style="@style/LinkButton"
android:visibility="gone"
android:text="OrderBalance" />

</RadioGroup>
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
</TabHost>

style.xml

 <style name="LinkButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="typeface">roboto_condensed_light</item>
<item name="android:textSize">12sp</item>
<item name="android:paddingTop">2dp</item>
<item name="android:paddingBottom">2dp</item>
<item name="android:paddingLeft">6dp</item>
<item name="android:paddingRight">6dp</item>
<item name="android:layout_margin">4dp</item>
<item name="android:textColor">@color/registration_button_color</item>
<item name="android:buttonTint">@color/registration_button_color</item>
</style>

关于android - 选项卡布局中间选项卡滚动不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50617171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com