actionbarsherlock使用tabnavigation棕褐色导航

时间:2012-09-04 06:30:04

标签: android actionbarsherlock

我的需求更改需要更改,需要标签导航。我已在我的应用程序中使用ActionBarSherlock。以前我的UI由活动组成。但是从我读到的TabBar需要片段活动?我怎样才能保留现有代码并实现TabBar,还是需要完全重写我的应用程序?我的所有活动都扩展了SherlockActivity?

亲切的问候,

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;




public class NewMainActivity extends SherlockFragmentActivity implements ActionBar.TabListener,
        ViewPager.OnPageChangeListener
{
    private ViewPager mViewPager;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.textlayout);
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(new MainPagerAdapter(getSupportFragmentManager()));
        mViewPager.setOnPageChangeListener(this);
        //mViewPager.setPageMarginDrawable(R.drawable.border);
        mViewPager.setPageMargin(16);
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.addTab(actionBar.newTab().setText("First Tab").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Second Tab").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Third Tab").setTabListener(this));
    }

    @Override
    public void onPageScrollStateChanged(int arg0)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int arg0)
    {
        // TODO Auto-generated method stub

    }

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

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft)
    {
        // TODO Auto-generated method stub

    }

    private class MainPagerAdapter extends FragmentPagerAdapter
    {
        public MainPagerAdapter(FragmentManager fm)
        {
            super(fm);
        }

        @Override
        public Fragment getItem(int position)
        {
            switch (position)
            {
            case 0:
                // return (mFragmentA = new FragmentA());

            case 1:
                // return (mFragmentB = new FragmentB());

            case 2:
                // return (mFragmentC = new FragmentC());

            }
            return null;
        }

        @Override
        public int getCount()
        {
            return 3;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

当然,您无需重新编写应用程序。你只需要一些折射就可以了。

首先,您应该将代码移至SherlockFragment(s)。

因此,假设您有3项活动:ActivityAActivityBActivityC

1 - 创建3个SherlockFragments:FragmentAFragmentBFragmentC

2 - 将代码移动到片段并进行必要的修改(例如覆盖,上下文引用等)。

3 - 现在使用Tab导航设置主FragmentActivity:

public class MainActivity extends SherlockFragmentActivity implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener {

    private FragmentA mFragmentA;
    private FragmentB mFragmenB;
    private FragmentC mFragmentC;

    private ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(new MainPagerAdapter(
                getSupportFragmentManager()));
        mViewPager.setOnPageChangeListener(this);
        mViewPager.setPageMarginDrawable(R.drawable.border);
        mViewPager.setPageMargin(16);
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.addTab(actionBar.newTab().setText(R.string.tab_a_title)
                .setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.tab_b_title)
                .setTabListener(this));
        actionBar.addTab(actionBar.newTab()
                .setText(R.string.tab_c_title).setTabListener(this));
    }

    @Override
    public void onPageSelected(int position) {
        getSupportActionBar().setSelectedNavigationItem(position);
    }

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

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

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

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

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

    private class MainPagerAdapter extends FragmentPagerAdapter {
        public MainPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
                return (mFragmentA = new FragmentA());

            case 1:
                return (mFragmentB = new FragmentB());

            case 2:
                return (mFragmentC = new FragmentC());

            }
            return null;
        }


        @Override
        public int getCount() {
            return 3;
        }
    }
}

<强> main_layout.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" >

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

</LinearLayout>