Android操作栏自定义视图和标题更改与标签导航。

时间:2014-05-20 08:33:00

标签: android android-actionbar actionbarsherlock

我正在尝试在Android中开发Tab Navigation应用。我有三个标签。每个标签都是可以浏览的。

创建选项卡的

MainClass extends SherlockFragmentActivity。每个班级都延伸了SherlockFragment。 如果我滑动标签然后我需要更改ActionBar自定义视图和标题。但我无法访问getSupportActionBar() 来自SherlockFragment类的方法。我怎样才能做到这一点。 任何人都可以帮助我。

先谢谢

2 个答案:

答案 0 :(得分:1)

MainActivity.java


import info.androidhive.tabsswipe.adapter.TabsPagerAdapter;
import info.androidhive.tabsswipe.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Top Rated", "Games", "Movies" };

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

        // 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) {
    }

}

使用上面的代码并使用以下示例链接进行尝试..此链接具有源..

来源链接点击此处:Android Tab Layout with Swipeable Views

答案 1 :(得分:1)

public class TabAdapter extends FragmentPagerAdapter implements ActionBar.TabListener ,           ViewPager.OnPageChangeListener{
int resId = {R.drawable.position0, R.drawable.position1, R.drawable.position2, R.drawable.position3, R.drawable.position4};

@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
int resIdLenght = resId.length;
if (position < 0 || position >= resIdLenght)
        return;
int drawableId = resId[position];
mActionBar.setIcon(drawableId);  
}
}

这可能是你的开始:)