替换片段时,Android视图寻呼机未被完全替换

时间:2014-08-05 09:17:11

标签: android android-viewpager android-tabs

当我在此处调用此方法替换视图分页器片段时,我的ViewPager选项卡未被正确替换:

getSupportFragmentManager().beginTransaction()
                           .replace(R.id.content_frame, new NotCharactersFragment())
                           .commit();

这里的代码应该用标签取代包含我的ViewPager的Fragment。当我删除选项卡并且只是拥有保存我的ViewPager的片段时,它可以工作,当我实现具有选项卡的视图寻呼机时,它就会混乱。

我怎样才能做到这一点,当我替换包含视图寻呼机的片段时,它会完全删除视图寻呼机中的所有内容?

这是视图寻呼机的样子:

enter image description here

当我使用我的滑动菜单从视图寻呼机导航时会发生什么:

enter image description here

选项卡从视图寻呼机继续,我不想要。

这是视图寻呼机代码:

public class CharactersFragment extends Fragment {

// Declare Variables
ViewPager viewPager;
PagerAdapter adapter;
private ActionBar actionbar;

String[] rank;
String[] country;
String[] population;
int[] flag;
private String[] tabs = { "Top Rated", "Games", "Movies" };

@Override
public View onCreateView(LayoutInflater Inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = Inflater.inflate(R.layout.characters, container, false);

    final ActionBar actionBar = getActivity().getActionBar();

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            // show the given tab



        }

        public void onTabUnselected(ActionBar.Tab tab,
                FragmentTransaction ft) {
            // hide the given tab
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }
    };

    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(tabListener));
    }







    // Generate sample data
    rank = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };

    country = new String[] { "China", "India", "United States",
            "Indonesia", "Brazil", "Pakistan", "Nigeria", "Bangladesh",
            "Russia", "Japan" };

    population = new String[] { "1,354,040,000", "1,210,193,422",
            "315,761,000", "237,641,326", "193,946,886", "182,912,000",
            "170,901,000", "152,518,015", "143,369,806", "127,360,000" };

    flag = new int[] { R.drawable.china, R.drawable.india,
            R.drawable.unitedstates, R.drawable.indonesia,
            R.drawable.brazil, R.drawable.pakistan, R.drawable.nigeria,
            R.drawable.bangladesh, R.drawable.russia, R.drawable.japan };

    // Locate the ViewPager in viewpager_main.xml
    viewPager = (ViewPager) v.findViewById(R.id.pager);
    // Pass results to ViewPagerAdapter Class
    adapter = new ViewPagerAdapter(getActivity(), rank, country,
            population, flag);

    // Binds the Adapter to the ViewPager
    viewPager.setAdapter(adapter);

    return v;
 }
}

查看寻呼机XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

0 个答案:

没有答案