在android中使用PagerSlidingTabStrip创建多个标签 [英] create multiple tabs using PagerSlidingTabStrip in android

查看:127
本文介绍了在android中使用PagerSlidingTabStrip创建多个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要多个标签的Android应用程序并使用第三方插件,即'com.astuetz:pagerslidingtabstrip:1.0.1'我在这里跟随代码示例

http://stackoverflow.com/questions/26178838/pagerslidingtabstrip-implement-guide [ ^ ]。



已经能够实现代码并且工作正常但是如果我想添加更多选项卡到4-6,我尝试添加更多选项卡但是我收到错误请看下面的代码



I am developing an android app that requires multiple tabs and am using a third party plug-in i.e 'com.astuetz:pagerslidingtabstrip:1.0.1' i followed the code sample here
http://stackoverflow.com/questions/26178838/pagerslidingtabstrip-implement-guide[^].

Have been able to implement the code and it is working fine but what if i want to add more tabs up to 4-6, i have tried adding more tabs but am getting errors see the code below

public class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return (position == 0)? "Headlines" : "All News" : "Videos" : "Sports" ; //errors here
    }
    @Override
    public int getCount() {
        return 4;
    }
    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        return (position == 0)? new FragmentA() : new FragmentB() : new FragmentC() : new FragmentD(); //errors here
    }
}

< br $>


请帮助



Kindly help

推荐答案

三元运算符 [ ^ ]是所谓的,因为它恰好包含三个部分:

The ternary operator[^] is so-called because it consists of precisely three parts:
<test> ? <true part> : <false part>



你不能在最后添加额外的部分,并期望编译器知道你想要它做什么!



您可以组合多个三元运算符:


You can't add extra parts at the end and expect the compiler to know what you want it to do!

You can combine multiple ternary operators:

<test 1> ? <test 1 true part> : (<test 2> ? <test 2 true part> : <false part>)



但是,这不是特别容易阅读。



更好的解决方案是我们 切换 statemet [ ^ ]:


However, that isn't particularly easy to read.

A better solution is to us the switch statemet[^]:

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return "Headlines";
        case 1:
            return "All News";
        case 2:
            return "Videos";
        case 3:
            return "Sports";
        default:
            return "# Unknown position #";
    }
}


这篇关于在android中使用PagerSlidingTabStrip创建多个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆