1

我想我整天都在与这个问题作斗争,但仍然无法解决这个问题。我正在为我的一个朋友构建一些应用程序。我正在使用 ActionBarSherlock,所以我可以使用操作栏和其他一些东西,比如页面查看器。

无论如何,我正在尝试进行主要活动:3 个选项卡,每个选项卡都显示一个片段(3 个不同的片段),每个片段都必须显示一个 fragmentPagerView,以便我可以来回滚动片段(也许我会添加“在我做对后的某个时候永远滚动”)

问题是我已经启动并运行了我的选项卡(我从 apiDemos 复制了一些代码)但是我无法显示选项卡,而且我找不到原因(它甚至没有崩溃)

这是我的 MainActivity 包含 navActionbar (和一些选项)

public class MainActivity extends SherlockFragmentActivity
{
    SherlockFragment yesTabFragment = new YesTabFragment();
    SherlockFragment newTabFragment = new NewTabFragment();
    SherlockFragment trendingTabFragment = new TrendingTabFragment();

    private void createTabs()
    {
        // put ActionBar to nav. mode
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab yesTab = actionBar
                .newTab()
                .setText("yes")
                .setTabListener(new TabListener<YesTabFragment>(
                        this, "yes", YesTabFragment.class));
        actionBar.addTab(yesTab);

        ActionBar.Tab trendingTab = actionBar
                .newTab()
                .setText("trending")
                .setTabListener(new TabListener<TrendingTabFragment>(
                        this, "trending", TrendingTabFragment.class));
        actionBar.addTab(trendingTab);

        ActionBar.Tab newTab = actionBar
                .newTab()
                .setText("new")
                .setTabListener(new TabListener<NewTabFragment>(
                        this, "new", NewTabFragment.class));
        actionBar.addTab(newTab);
    }

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        createTabs();
    }

    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.main_activity_menu, menu);
        return true;
    }

    public static class TabListener<T extends SherlockFragment> implements ActionBar.TabListener
    {
        private final SherlockFragmentActivity mActivity;
        private final String mTag;
        private final Class<T> mClass;
        private final Bundle mArgs;
        private SherlockFragment mFragment;

        public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz)
        {
            this(activity, tag, clz, null);
        }

        public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz, Bundle args)
        {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
            mArgs = args;

            // Check to see if we already have a fragment for this tab, probably
            // from a previously saved state. If so, deactivate it, because our
            // initial state is that a tab isn't shown.
            mFragment = (SherlockFragment)mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
            if (mFragment != null && !mFragment.isDetached())
            {
                FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
                ft.detach(mFragment);
                ft.commit();
            }
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft)
        {
            if (mFragment == null)
            {
                mFragment = (SherlockFragment)SherlockFragment.instantiate(mActivity, mClass.getName(), mArgs);
                ft.add(android.R.id.content, mFragment, mTag);
            }
            else
            {
                ft.attach(mFragment);
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft)
        {
            if (mFragment != null)
            {
                ft.detach(mFragment);
            }
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft)
        {
            //Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show();
        }
    }
}

正如我所说,我直接从 apiDemos 复制了 TabListener 实现(尽管我写的完全一样)

这是其中一个选项卡片段的实现(它们都是相同的)

public class YesTabFragment extends SherlockFragment
{
    View _fragmentView;
    private Vector<stuff> list;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        _fragmentView = inflater.inflate(R.layout.yes_fragment, container, false);

        ViewPager pager = (ViewPager)_fragmentView.findViewById(R.id.gag_pager);
        API api = API.getInstance();

        list = api.get("Yes", 1, 10);
        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getFragmentManager(), listOfGags);

        pager.setAdapter(adapter);

        return _fragmentView;
    }
}

yesTabFragment 类的实现

public class YesTabFragment extends SherlockFragment
{
    View _fragmentView;
    private Vector<things> list;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        _fragmentView = inflater.inflate(R.layout.yes_fragment, container, false);

        ViewPager pager = (ViewPager)_fragmentView.findViewById(R.id.thing_pager);
        API api = API.getInstance();

        list = api.get("Yes", 1, 10);
        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getFragmentManager(), list);

        pager.setAdapter(adapter);

        return _fragmentView;
    }
}

当然还有 main 和 yes 选项卡的 XML(同样,选项卡几乎相同)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/List"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</LinearLayout>

和 yesTab

<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/thing_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>

</LinearLayout>

我真的找不到标签片段不替换框架布局并向我展示viewpager的原因。

我真的可以在这里使用一些好的建议,因为我肯定在这里遗漏了一些重要的东西并且无法理解......

谢谢大佬...

4

1 回答 1

3

您的 MainActivity 应包含 ViewPager 定义,例如:

viewPager=(ViewPager)findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();

/** Defining a listener for pageChange */
ViewPager.SimpleOnPageChangeListener pageChangeListener = 
    new ViewPager.SimpleOnPageChangeListener()
    {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            actionBar.setSelectedNavigationItem(position);
        }
    };

/** Setting the pageChange listner to the viewPager */
viewPager.setOnPageChangeListener(pageChangeListener);

/** Creating an instance of FragmentPagerAdapter */
fragmentPagerAdapter = new PhotoMugFragmentPagerAdapter(fm);

/** Setting the FragmentPagerAdapter object to the viewPager object */
viewPager.setAdapter(fragmentPagerAdapter);
于 2012-11-15T14:28:44.447 回答