如何在android中的视图寻呼机中滑动时逐个显示标签

时间:2017-11-09 09:04:26

标签: android android-viewpager android-tablayout android-customtabs

我需要一个TabLayout,它在初始阶段只显示两个标签。
在滑动活动标签后,将在中间,下一个标签位于左侧,前一个标签位于右侧。
如何操作?

以下是确切的场景快照: enter image description here enter image description here  请有人给我建议。?

5 个答案:

答案 0 :(得分:0)

在.xml

中添加此coad
   <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"
        android:id="@+id/toolbar"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"/>

</android.support.design.widget.AppBarLayout>

<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"  />
  </android.support.design.widget.CoordinatorLayout>

如果你想改变tabmode,你可以做app:tabMode =“fix”

java代码是

public class Tabmenu extends AppCompatActivity {
 private TabLayout tabLayout;
  private ViewPager viewPager;
   Toolbar toolbar;

 //  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
     protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_tabmenu);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Menus Item");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    viewPager.setCurrentItem(item);
           }

    private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new 
    ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new Norththalis(), "NORTH INDIAN THALIS");
    adapter.addFrag(new Northmenu(), "NORTH INDIAN MENU");
    adapter.addFrag(new Biryani(), "BIRYANI");
    adapter.addFrag(new Packs(), "PACKS");
    adapter.addFrag(new Roles(), "ROLES");
    adapter.addFrag(new Sides(), "SIDES");
    adapter.addFrag(new Sweets(), "SWEETS");
    adapter.addFrag(new Beverages(), "BEVERAGES");

    viewPager.setAdapter(adapter);
          }

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

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

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

           }

答案 1 :(得分:0)

将此插入您自定义tabLayout的代码中:

 tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
 tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

答案 2 :(得分:0)

经过长时间的搜索,使用库PagerSlidingTabStrip得到解决方案。

应用程式:pstsPaddingMiddle =&#34;真&#34; 应用程式:pstsTabPaddingLeftRight =&#34; 80dp&#34;

这就成了伎俩。

答案 3 :(得分:0)

使用

<android.support.v4.view.PagerTabStrip
   android.support.v4.view.PagerTabStrip
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />
使用ViewPager

xml

答案 4 :(得分:0)

像魅力一样工作

您的标签xml

  <com.google.android.material.tabs.TabLayout
    android:id="@+id/tab_layout"
    android:layout_gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

在设置viewpager适配器后添加此

  for (int i = 0; i < tabLayout.getTabCount(); i++) {
        View tab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(i);
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) tab.getLayoutParams();
        if (i == 0) {
            p.setMargins(400, 0, 200, 0);
        } else {
            p.setMargins(0, 0, 200, 0);
        }

        tab.requestLayout();
    }