简体   繁体   English

动作栏中的标签Sherlock

[英]Tabs in Action Bar Sherlock

I'm working on an application using action bar Sherlock for Android . 我正在使用操作栏Sherlock for Android应用程序。 I have currently three tabs with the following categories home calculator and drive. 我目前有以下三个类别的标签,分别是家用计算器和驱动器。

I made a calculator app earlier and I want to be able to use that code in the calculator tab how do I use a activity inside a action bar Sherlock tab? 我之前制作了一个计算器应用程序,并且希望能够在“计算器”选项卡中使用该代码,如何在操作栏“ Sherlock”选项卡中使用活动?

Here's my view pager code: 这是我的查看寻呼机代码:

package com.d4a.stzh;

import com.d4a.stzh.FragmentTab1;
import com.d4a.stzh.FragmentTab2;
import com.d4a.stzh.FragmentTab3;
import com.d4a.stzh.CMainActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class ViewPagerAdapter extends FragmentPagerAdapter {


    // Declare the number of ViewPager pages
    final int PAGE_COUNT = 4;

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

    @Override
    public Fragment getItem(int arg0) {
        switch (arg0) {

            // Open FragmentTab1.java
        case 0:
            FragmentTab1 fragmenttab1 = new FragmentTab1();
            return fragmenttab1;

            // Open FragmentTab2.java
        case 1:
            FragmentTab2 fragmenttab2 = new FragmentTab2();
            return fragmenttab2;

            // Open FragmentTab3.java
        case 2:
            FragmentTab3 fragmenttab3 = new FragmentTab3();
            return fragmenttab3;
        }
        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return PAGE_COUNT;
    }

}

And here's my MainActivity code: 这是我的MainActivity代码:

package com.d4a.stzh;

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

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.d4a.stzh.ViewPagerAdapter;
import com.d4a.stzh.R;

public class MainActivity extends SherlockFragmentActivity {

    // Declare Variables
    ActionBar mActionBar;
    ViewPager mPager;
    Tab tab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from activity_main.xml
        setContentView(R.layout.activity_main);

        // Activate Navigation Mode Tabs
        mActionBar = getSupportActionBar();
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Locate ViewPager in activity_main.xml
        mPager = (ViewPager) findViewById(R.id.pager);

        // Activate Fragment Manager
        FragmentManager fm = getSupportFragmentManager();

        // Capture ViewPager page swipes
        ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                // Find the ViewPager Position
                mActionBar.setSelectedNavigationItem(position);
            }
        };

        mPager.setOnPageChangeListener(ViewPagerListener);
        // Locate the adapter class called ViewPagerAdapter.java
        ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
        // Set the View Pager Adapter into ViewPager
        mPager.setAdapter(viewpageradapter);

        // Capture tab button clicks
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {

            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
                // Pass the position on tab click to ViewPager
                mPager.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
            }
        };

        // Create first Tab
        tab = mActionBar.newTab().setText("Home").setTabListener(tabListener);
        mActionBar.addTab(tab);

        // Create second Tab
        tab = mActionBar.newTab().setText("Books").setTabListener(tabListener);
        mActionBar.addTab(tab);

        // Create third Tab
        tab = mActionBar.newTab().setText("Drive").setTabListener(tabListener);
        mActionBar.addTab(tab);
        // Create fourth Tab
                tab = mActionBar.newTab().setText("Calculator").setTabListener(tabListener);
                mActionBar.addTab(tab);
    }

}

Is there any way to insert an activity into Sherlock fragment tab? 有什么方法可以将活动插入Sherlock片段选项卡?

For your info. 为您的信息。

A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. 片段必须始终嵌入到活动中,并且片段的生命周期直接受到宿主活动的生命周期的影响。 For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. 例如,活动被暂停时,其中的所有片段也将暂停;活动被销毁时,所有片段也将被暂停。

Source : http://developer.android.com/guide/components/fragments.html 来源: http : //developer.android.com/guide/components/fragments.html

You can't use an activity in a Fragment. 您不能在Fragment中使用活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM