Viewpager指标选项卡

时间:2014-11-19 18:45:46

标签: android android-fragments android-tabs pagerslidingtabstrip

嘿伙计们我正在尝试创建一个像google playstore标签的视图寻呼机指示器,我跟随这个例子

Git Example

但由于某种原因,它给了我一个空指针异常。

我有一个名为NewOrder的活动,它扩展了Activity,而在setContentView上我有以下布局

我的xml布局是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip" />

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:id="@+id/pager"
        android:layout_weight="1"
        android:layout_height="0px" />
</LinearLayout>

我创建了一个用于创建可滑动标签的适配器。

我的适配器:

public class TabsPagerAdapter extends FragmentPagerAdapter {

    private final String[] TITLES = { "Coffees", "Sweets", "Snacks", "Drinks" };

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

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0:
                return new CoffeesFragment();
            case 1:
                return new SweetsFragment();
            case 2:
                return new SnacksFragment();
            case 3:
                return new DrinksFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }
}

对于我返回的每个片段,我都有一个具有不同布局的单独片段。

现在问题是我使用

<com.astuetz.PagerSlidingTabStrip
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="48dip" />

        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:id="@+id/pager"
            android:layout_weight="1"
            android:layout_height="0px" />

我应该将它们放在每个片段的布局上还是放在父活动布局中?

我应该这样吗?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:layout_height="match_parent"
    tools:context="com.example.kostas.fragments.CoffeesFragment">

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip" />

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:id="@+id/pager"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Search..."
            android:id="@+id/searchListCoffees" />

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/coffesListView" />
    </LinearLayout>
</RelativeLayout>

这是我设置标签的地方

public class NewOrder extends FragmentActivity implements ActionBar.TabListener{

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private PagerSlidingTabStrip tabPager;
    private ActionBar actionBar;

    private String[] tabs = { "Coffees", "Sweets", "Snacks", "Drinks" };

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

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        tabPager = (PagerSlidingTabStrip)findViewById(R.id.tabs);
        tabPager.setViewPager(viewPager);
        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.SimpleOnPageChangeListener mListener = new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        };
        viewPager.setOnPageChangeListener(mListener);

    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

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

    }
}

我正在执行示例所说的所有操作,当我运行活动时,它会崩溃,并向我提供此错误:

Process: com.example.kostas.ordertakingsystem, PID: 1195
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kostas.ordertakingsystem/com.example.kostas.ordertakingsystem.NewOrder}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.kostas.ordertakingsystem.NewOrder.onCreate(NewOrder.java:38)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)

任何想法?我知道这可能是一个重复的问题,但我还没有找到正确实现它的方法。

我将不胜感激任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:1)

试试这个,https://github.com/hackware1993/MagicIndicator,一个功能强大且可扩展的ViewPager指标框架。