Java 添加滑入表格布局(选项卡栏)

Java 添加滑入表格布局(选项卡栏),java,android,Java,Android,我可以在选项卡之间滑动,但我想保留多个选项卡,但操作栏下方的选项卡栏太乱了。所以我想在选项卡栏本身添加一个滑动功能 这是我的密码: public class MainActivity extends AppCompatActivity { private SectionsPagerAdapter mSectionsPagerAdapter; private ViewPager mViewPager; @Override protected void onCreate(Bundle save

我可以在选项卡之间滑动,但我想保留多个选项卡,但操作栏下方的选项卡栏太乱了。所以我想在选项卡栏本身添加一个滑动功能

这是我的密码:

public class MainActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //sets custom toolbar as actionbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Create the adapter that will return a fragment for each of the two primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }


    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 8 total pages.
        return 8;
    }

    @Override
    public CharSequence getPageTitle(int position) {           //Gets the title of the tabs
        switch (position) {
            case 0:
                return "SECTION 1";
            case 1:
                return "SECTION 2";
            case 2:
                return "SECTION 2";
            case 3:
                return "SECTION 2";
            case 4:
                return "SECTION 2";
            case 5:
                return "SECTION 2";
            case 6:
                return "SECTION 2";
            case 7:
                return "SECTION 2";
           // case 1:
             //   return "SECTION 2";
        }
        return null;
    }
}

这很简单,在你的表格中放上这个属性

app:tabMode="scrollable"
而不是

app:tabMode="fixed"

希望这能有所帮助。

大家好,欢迎来到Stack Overflow,请阅读如何创建一个并进行检查,以增加获得反馈和有用答案的机会。我阅读了该文档,但我认为我无法清楚地表达我的意思(我不知道有问题需要的许多技术词汇)。你能帮我编辑这个问题吗。谢谢你知道我该如何准确地表达@GrayCygnusHe所指的主题标题吗?她正在谈论你所问问题的标题。如果你发现答案是正确的,请将其标记为正确。我将其标记为正确,但我的声誉不足以公开显示为投票。