有人能告诉我一个简单的PagerSlidingTabStrip工作实现吗?

时间:2014-04-23 03:08:53

标签: java android android-layout

这是图书馆:https://github.com/astuetz/PagerSlidingTabStrip 我正在尝试实现它,但我的应用程序在启动时不断崩溃。我试图理解发布的示例应用程序,但不认为我做的正确。这是我的代码:

MainActivity.java


    import android.graphics.Color;
    import android.graphics.drawable.ColorDrawable;
    import android.graphics.drawable.Drawable;
    import android.graphics.drawable.LayerDrawable;
    import android.graphics.drawable.TransitionDrawable;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.util.TypedValue;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;

    import com.astuetz.PagerSlidingTabStrip;


    public class MainActivity extends FragmentActivity {

        private final Handler handler = new Handler();

        private PagerSlidingTabStrip tabs;
        private ViewPager pager;
        private MyPagerAdapter adapter;

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

            tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
            pager = (ViewPager) findViewById(R.id.pager);
            adapter = new MyPagerAdapter(getSupportFragmentManager());

            pager.setAdapter(adapter);

            tabs.setViewPager(pager);
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @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();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        @Override
        protected void onSaveInstanceState(Bundle outState){
            super.onSaveInstanceState(outState);
        }
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState){
            super.onRestoreInstanceState(savedInstanceState);
        }

        private Drawable.Callback drawableCallback = new Drawable.Callback() {
            @Override
            public void invalidateDrawable(Drawable who) {
                getActionBar().setBackgroundDrawable(who);
            }

            @Override
            public void scheduleDrawable(Drawable who, Runnable what, long when) {
                handler.postAtTime(what, when);
            }

            @Override
            public void unscheduleDrawable(Drawable who, Runnable what) {
                handler.removeCallbacks(what);
            }
        };

        public class MyPagerAdapter extends FragmentPagerAdapter{
            private final String[] TITLES = {"T1","T2"};

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

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

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

这是acitivty_main.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="edu.purdue.test.app.MainActivity">

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

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tabs"
        tools:context=".MainActivity" />


</RelativeLayout>

3 个答案:

答案 0 :(得分:2)

您没有实例化您希望成为FragmentPagerAdapter,getItem方法中的标签的任何片段。而是返回null,因此没有视图来填充viewpager。我可以看到你已经检查了他的示例项目,或者仔细看看你试图添加到viewpager的片段。上面的答案看起来像是一个实现。

答案 1 :(得分:0)

这是我现在正在使用的一个例子,

public class Formulario extends FragmentActivity implements ActionBar.TabListener {

    private static final FragmentTransaction transaction = null;
    public String ambitorec;
    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;

    // Tab titles
    private String[] tabs = { "tab 1", "tab 2", "tab 3" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_formulario);
        ambitorec = getIntent().getStringExtra("ambito");

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

        viewPager.setAdapter(mAdapter);
        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));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        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, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

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

您还需要添加适配器,在这种情况下:

public class TabsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            // tab 1 fragment activity
            return new Formulario1Fragment();
        case 1:
            // tab2 fragment activity
            return new Formulario2Fragment();
        case 2:
            // tab 3 fragment activity
            return new MultimediaFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 3;
    }
}

希望它有所帮助,如果您需要更多帮助,请尝试解释

答案 2 :(得分:0)

FragmentPagerAdapter#getItem 中,您没有实例化任何想要成为选项卡的片段。您返回的不是片段,而是 null,这意味着没有视图可以填充 viewpager。我看到您已经查看了他的示例项目,因此请再次查看您尝试添加到 viewpager 的片段。上面的响应似乎是第二个实现。