问题描述
我有需要标签导航后期需求变更。我已经使用ActionBarSherlock在我的应用程序。 previously我的用户界面包括活动。但是从我读的TabBar需要片段活动?如何有没有什么办法,我可以保持我现有的code和实施的TabBar或将这一要求我的应用程序的完全重新写?我所有的活动延伸SherlockActivity?
亲切的问候,
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentPagerAdapter;
进口android.support.v4.app.FragmentTransaction;
进口android.support.v4.view.ViewPager;
进口com.actionbarsherlock.app.ActionBar;
进口com.actionbarsherlock.app.ActionBar.Tab;
进口com.actionbarsherlock.app.SherlockFragmentActivity;
公共类NewMainActivity扩展SherlockFragmentActivity实现ActionBar.TabListener,
ViewPager.OnPageChangeListener
{
私人ViewPager mViewPager;
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.textlayout);
mViewPager =(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(新MainPagerAdapter(getSupportFragmentManager()));
mViewPager.setOnPageChangeListener(本);
//mViewPager.setPageMarginDrawable(R.drawable.border);
mViewPager.setPageMargin(16);
最后的动作条动作条= getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab()的setText(第一选项卡)setTabListener(本)。);
actionBar.addTab(actionBar.newTab()的setText(第二个选项卡)setTabListener(本)。);
actionBar.addTab(actionBar.newTab()的setText(第三个选项卡)setTabListener(本)。);
}
@覆盖
公共无效onPageScrollStateChanged(INT为arg0)
{
// TODO自动生成方法存根
}
@覆盖
公共无效onPageScrolled(INT为arg0,浮动ARG1,INT ARG2)
{
// TODO自动生成方法存根
}
@覆盖
公共无效onPageSelected(INT为arg0)
{
// TODO自动生成方法存根
}
@覆盖
公共无效onTabSelected(TAB键,FragmentTransaction英尺)
{
mViewPager.setCurrentItem(tab.getPosition());
}
@覆盖
公共无效onTabUnselected(TAB键,FragmentTransaction英尺)
{
// TODO自动生成方法存根
}
@覆盖
公共无效onTabReselected(TAB键,FragmentTransaction英尺)
{
// TODO自动生成方法存根
}
私有类MainPagerAdapter扩展FragmentPagerAdapter
{
公共MainPagerAdapter(FragmentManager FM)
{
超(FM);
}
@覆盖
公共片段的getItem(INT位置)
{
开关(位置)
{
情况下0:
//回报(mFragmentA =新碎裂());
情况1:
//回报(mFragmentB =新FragmentB());
案例2:
//回报(mFragmentC =新FragmentC());
}
返回null;
}
@覆盖
公众诠释getCount将()
{
返回3;
}
}
}
当然,你将不必重新编写应用程序。你只需要一些refractoring仅此而已。
首先,你应该通过移动code到 SherlockFragment
启动(S)。
因此,让我们说,你有3个活动: ActivityA
, ActivityB
和 ActivityC
。
1 - 创建3 SherlockFragments:碎裂
, FragmentB
和 FragmentC
。
2 - 将你的code的片段,并进行必要的修改(如覆盖,上下文引用,等等。)
。3 - 现在设置您使用Tab键导航FragmentActivity:
公共类MainActivity扩展SherlockFragmentActivity工具
ActionBar.TabListener,ViewPager.OnPageChangeListener {
私人碎裂mFragmentA;
私人FragmentB mFragmenB;
私人FragmentC mFragmentC;
私人ViewPager mViewPager;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main_layout);
mViewPager =(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(新MainPagerAdapter(
getSupportFragmentManager()));
mViewPager.setOnPageChangeListener(本);
mViewPager.setPageMarginDrawable(R.drawable.border);
mViewPager.setPageMargin(16);
最后的动作条动作条= getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab()的setText(R.string.tab_a_title)
.setTabListener(本));
actionBar.addTab(actionBar.newTab()的setText(R.string.tab_b_title)
.setTabListener(本));
actionBar.addTab(actionBar.newTab()
.setText(R.string.tab_c_title).setTabListener(本));
}
@覆盖
公共无效onPageSelected(INT位置){
getSupportActionBar()setSelectedNavigationItem(位置)。
}
@覆盖
公共无效onTabSelected(TAB键,FragmentTransaction英尺){
mViewPager.setCurrentItem(tab.getPosition());
}
@覆盖
公共无效onPageScrollStateChanged(INT为arg0){
}
@覆盖
公共无效onPageScrolled(INT为arg0,浮动ARG1,ARG2 INT){
}
@覆盖
公共无效onTabUnselected(TAB键,FragmentTransaction英尺){
}
@覆盖
公共无效onTabReselected(TAB键,FragmentTransaction英尺){
}
私有类MainPagerAdapter扩展FragmentPagerAdapter {
公共MainPagerAdapter(FragmentManager FM){
超(FM);
}
@覆盖
公共片段的getItem(INT位置){
开关(位置){
情况下0:
返程(mFragmentA =新碎裂());
情况1:
返程(mFragmentB =新FragmentB());
案例2:
返程(mFragmentC =新FragmentC());
}
返回null;
}
@覆盖
公众诠释getCount将(){
返回3;
}
}
}
main_layout.xml
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
< android.support.v4.view.ViewPager
机器人:ID =@ + ID /寻呼机
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =1/>
< / LinearLayout中>
I have a late requirement change that requires tab navigation. I have already using ActionBarSherlock in my application. Previously my UI consisted of Activities. However from what i read the TabBar requires Fragment Activities? How is there any way i can keep my existing code and implement the TabBar or would this require a complete re write of my application ? All of my activities extends SherlockActivity ?
Kind Regards,
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;
}
}
}
Of course you won't have to re-write your application. You just need some refractoring that's all.
First you should start by moving your code to SherlockFragment
(s).
So let's say you have 3 Activities: ActivityA
, ActivityB
and ActivityC
.
1 - Create 3 SherlockFragments: FragmentA
, FragmentB
and FragmentC
.
2 - Move your code to the fragments and make the necessary modifications (e.g. Overrides, Context references, etc.).
3 - Now to setup your main FragmentActivity with Tab navigation:
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使用tabnavigation棕褐色导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!