如何使用页面滑动视图创建 Android 选项卡式样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22265796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:44:50  来源:igfitidea点击:

How to Create Android Tabbed style with Page Swipe View

androidtabsandroid-viewpagersliding

提问by Person

I want an example of tab like this

google play tab

我想要一个这样的标签示例

谷歌播放标签

I searched but just got this.

我搜索过,但刚刚得到了这个。

viewpageindicator

浏览页面指示器

I couldn't use this source. Can someone tell me another example of tab with sliding option.
I think the viewpageindicator is not the same as google plays tabs.

cause when i'm scrolling in google plays page the line below tabs moves while scrolling, But in viewpageindicator it's not.

Thank you

我无法使用此来源。有人可以告诉我另一个带有滑动选项的选项卡示例。
我认为 viewpageindicator 与谷歌播放标签不同。

因为当我在谷歌播放页面中滚动时,标签下方的行在滚动时移动,但在 viewpageindicator 中不是。

谢谢

回答by Naruto

First you need to create an xml layout file with viewpager android.support.v4.view.ViewPager

首先你需要用viewpager创建一个xml布局文件 android.support.v4.view.ViewPager

Then inflate this layout in your main activity, make sure your activity implements

然后在你的主要活动中膨胀这个布局,确保你的活动实现

android.support.v7.app.ActionBar.TabListenerLets say, you want to have 4 tabs, then create

android.support.v7.app.ActionBar.TabListener假设您想要 4 个选项卡,然后创建

4 different fragments. Create a common adapter to facilitate these fragments based on the

4个不同的片段。创建一个通用的适配器来方便这些基于

tabs you select.

您选择的选项卡。

Here is the simple example with code snippet, http://www.feelzdroid.com/2014/10/android-action-bar-tabs-swipe-views.html

这是带有代码片段的简单示例,http://www.feelzdroid.com/2014/10/android-action-bar-tabs-swipe-views.html

Note: above example works well with android support lib, which provides backward actionbar

注意:上面的例子适用于 android 支持库,它提供了向后的操作栏

compatibility for earlier version of android phones

兼容早期版本的安卓手机

Hope it helps.

希望能帮助到你。

Thanks

谢谢

回答by Michele La Ferla

When I had implemented something similar, I had done it in a nicer way, in the sense of classes. I had created the adapter in a package called working.

当我实现了类似的东西时,我以更好的方式完成了它,在类的意义上。我在一个名为 working 的包中创建了适配器。

Below is the code:

下面是代码:

public class TabsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
          return new WorkingFragment();
        //You can add as many fragments as you wish here by adding the cases and calling the different fragments.
        }      
        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs. 4 is only the number of fragments I had used.
        return 4;
    }
}

You then need to create a new class called WorkingFragmentin the example I am giving. For the sake of clean coding, I created this class in the main package.

然后,您需要创建一个WorkingFragment在我给出的示例中调用的新类。为了干净的编码,我在主包中创建了这个类。

Below is the code for the fragment:

下面是片段的代码:

public class NewEventFragment extends Fragment {

    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //reference the xml file containing the view with all the code here.
    }
}

In the MainActivity, you then need to make the functionality to change form one tab to another. In my case I had included this in the main package.

在 中MainActivity,您需要创建将一个选项卡从一个选项卡更改为另一个选项卡的功能。就我而言,我已将其包含在主包中。

Below is the code for the MainActivity class:

下面是 MainActivity 类的代码:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    private ViewPager ViewPager;
    private TabsPagerAdapter SectionsPagerAdapter;
    private ActionBar actionBar;

    //Include the name for the tabs in this array. Make sure the number of elements in this string matches the number of views you will have in your app. 
    private String[] tabs = {};

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

     // Initialisation
        ViewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        SectionsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        ViewPager.setAdapter(SectionsPagerAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }

        ViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {

    }

    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
        ViewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
    }
} 

Hope this has helped you achieve the necessary funtionality for your app :)

希望这有助于您为您的应用程序实现必要的功能:)

回答by Mahesh Pandit

simple way to work with fragment tabs

使用片段选项卡的简单方法

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

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

in on create and then

在创建然后

private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new 
        ViewPagerAdapter(getSupportFragmentManager());        
        adapter.addFrag(new RootDetailFragment(), "TAB 1");
        adapter.addFrag(new ShiftDetailFragment(), "TAB 2");        
        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);
    }
}

in layout file add

在布局文件中添加

 <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000"
        app:tabGravity="fill"
        app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>