actionbarsherlock使用tabnavigation黄褐色导航(actionbarshe

2019-06-27 18:21发布

我有一个需要标签导航后期需求变更。 我已经在我的应用程序中使用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;
        }
    }
}

Answer 1:

当然,你将不必重新编写应用程序。 你只需要一些refractoring仅此而已。

首先,你应该在你的代码移动开始SherlockFragment (S)。

所以我们可以说你有3个活动: ActivityAActivityBActivityC

1 -创建3个SherlockFragments: FragmentAFragmentBFragmentC

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

3 - 现在设置您的主要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>


文章来源: actionbarsherlock using tabnavigation tan navigation