当我选择它时,TabItem位于中间

问题描述 投票:0回答:1

我有一个tabrollout可滚动,但是当我选择一个tabitem时,可滚动停止了,当我选择任何一个tabitem时,我都需要在中心的TabItem。谢谢。

<com.google.android.material.tabs.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabSelectedTextColor="@color/colorPrimary">

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Calle Mármol complejo 23" />

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Calle Mármol complejo 23" />
        <FrameLayout
                android:id="@+id/fragment_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

现在是java文件,仅用于转到tabitem

 @Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

    boolean fragmentTransaction = false;
    Fragment fragment = null;

    switch (menuItem.getItemId()) {
        case R.id.nav_home:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new HomeFragment()).commit();
            titleToolbar = getResources().getString(R.string.Menu_Home);
            fragmentTransaction = true;
            getSupportActionBar().setTitle(titleToolbar);
            break;

        case R.id.nav_receipts:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new ReceiptsFragment()).commit();
            titleToolbar = getResources().getString(R.string.Menu_Receipts);
            fragmentTransaction = true;
            getSupportActionBar().setTitle(titleToolbar);
            break;
java android tabitem
1个回答
0
投票

XML:

<androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab"
            style="@style/TabStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="5dp"
            app:tabMode="scrollable" />

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </androidx.appcompat.widget.LinearLayoutCompat>

样式

 <style name="TabStyle" parent="Widget.Design.TabLayout">
        <item name="android:background">@color/colorPrimary</item>
        <item name="tabTextAppearance">@style/TabTextStyle</item>
        <item name="tabIndicatorColor">@color/white</item>
        <item name="tabTextColor">@color/red_light</item>
        <item name="tabSelectedTextColor">@color/white</item>
    </style>

    <style name="TabTextStyle" parent="TextAppearance.AppCompat.Button">
        <item name="android:textSize">@dimen/_10sdp</item>
    </style>

Java

public class MainActivity extends AppCompatActivity {
  private SampleAdapter adapter;
  private TabLayout tabs;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ViewPager pager=(ViewPager)findViewById(R.id.pager);

    adapter=new SampleAdapter(this, getSupportFragmentManager());
    pager.setAdapter(adapter);

    tabs=(TabLayout)findViewById(R.id.tabs);
    tabs.setupWithViewPager(pager);
    tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
  }
}

根据tabItem在适配器中分配片段

© www.soinside.com 2019 - 2024. All rights reserved.