TabItem в центре, когда я выбираю его - PullRequest
0 голосов
/ 02 марта 2020

У меня есть прокручиваемая табуляция, но когда я выбираю один табитем, прокручиваемость останавливается, мне нужен 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, просто за go до табита

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

1 Ответ

0 голосов
/ 02 марта 2020

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

...