Android 替换片段不起作用

Android 替换片段不起作用,android,android-fragments,Android,Android Fragments,我的应用程序,有一个导航抽屉和滑动标签 问题:我已经在主活动中实现了滑动选项卡,单击导航抽屉项会打开不同的片段,效果很好,但它隐藏在我在主活动中实现的滑动选项卡中 我的目标:我希望默认页面是滑动选项卡,如果我单击导航图标,滑动选项卡页面必须关闭并打开我为导航图标实现的片段中的内容 代码如下 activity_main.xml: <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/

我的应用程序,有一个导航抽屉和滑动标签

问题:我已经在主活动中实现了滑动选项卡,单击导航抽屉项会打开不同的片段,效果很好,但它隐藏在我在主活动中实现的滑动选项卡中

我的目标:我希望默认页面是滑动选项卡,如果我单击导航图标,滑动选项卡页面必须关闭并打开我为导航图标实现的片段中的内容

代码如下

activity_main.xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
    android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />

    <FrameLayout
        android:id="@+id/container_body"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    <tabs.SlidingTabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/ColorPrimary"
        android:elevation="2dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    </FrameLayout>
</LinearLayout>


<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="keleno.example.ramz.mapper_city.FragmentDrawer"
    android:layout_width="260dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_nav_drawer"
    tools:layout="@layout/fragment_nav_drawer" />
MainActivity.java:

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {

Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[] = {"CITY", "GO", "NEAR"};
int Numboftabs = 3;
private FragmentDrawer drawerFragment;
private Handler mHandler;

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


    // Creating The Toolbar and setting it as the Toolbar for the activity

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);


    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);


    drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
    drawerFragment.setDrawerListener(this);


    // display the first navigation drawer view on app launch
    displayView(0);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onDrawerItemSelected(View view,  final int position) {
displayView(position);

}

private void displayView(int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) {
        case 0:
            fragment = new ContactFragment();
            title = getString(R.string.nav_item_contact);

            break;
        case 1:
            fragment = new MyLocation();

            title = getString(R.string.nav_item_myloc);
            break;

        case 2:
            fragment = new TermsandCondition();
            title = getString(R.string.nav_item_terms);
            break;

        case 3:
            fragment = new UpgradePlan();
            title = getString(R.string.nav_item_upgrade);
            break;
        case 4:
            fragment = new Aboutus();
            title = getString(R.string.nav_item_about);


            break;


        default:
            break;
    }
    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //   fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out,android.R.anim.fade_in,android.R.anim.fade_out);
        fragmentManager.addOnBackStackChangedListener(null);
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();

        // set the toolbar title
        // actionBar.setTitle(Html.fromHtml("<font color='#ff0000'>ActionBartitle </font>"));
        getSupportActionBar().setTitle(Html.fromHtml("<font color='#000000'>" + title + " </font>"));
        //  final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        //  upArrow.setColorFilter(getResources().getColor(R.color.grey), PorterDuff.Mode.SRC_ATOP);
        // getSupportActionBar().setHomeAsUpIndicator(upArrow);


    }
}
}

您必须将滑动选项卡制作为片段,以便它可以被其他片段替换,而不是制作滑动选项卡活动

如前所述,如果您只想在一个片段中显示选项卡,则应删除主布局中的选项卡,并直接在片段布局中使用它

保持

抽屉布局> 工具栏>片段[您的片段将包含使用fragment.xml膨胀的选项卡]>mainactivity布局文件中的navigationview


将标签xml代码移到other fragment.xml文件中

这是一个来自Google@的好网页,我已经用过了。我相信它将满足您的要求,将滑动多个选项卡作为主UI。MainActivity从FragmentActivity扩展为子类。换句话说,MainActivity可以是一个片段,但我不建议您让示例项目变得更复杂,让它从简单开始。

您需要一个好的滑动选项卡示例代码。如果我有机会,我会提供一个链接。你能给我提供链接吗?你想要滑动标签只在一个特定的片段下,对吗?是的。我只想在一个片段中显示滑动点击您可以将选项卡布局代码移动到片段中..无需将其保留在mainactivity布局文件中..因为您只在mainactivity文件中保留跨片段的常见内容。。更多详细信息,请参见下面的回答谢谢,实际上,如果应用程序开始运行,默认情况下应该显示滑动选项卡布局在我的情况下,它是主要活动,然后如果我单击导航抽屉项目之一,它应该显示特定的片段,而不是主要活动。这是可行的,但主活动与片段的内容重叠。这就是我要说的填充主活动,但不要在mainactivity.xml文件中包含tablayout。但只需保留最小布局,如工具栏+导航抽屉+片段,其内容将按片段显示