替换片段无效

时间:2015-10-16 08:01:41

标签: android android-fragments

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

问题:我在主要活动中实现了滑动标签,导航抽屉项目的onclick打开了不同的片段,它工作正常,但它隐藏在我在主要活动中实现的滑动标签内,

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

代码如下。

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);


    }
}
}

3 个答案:

答案 0 :(得分:0)

您必须将滑动标签设为片段,以便可以将其替换为其他片段,而不是进行滑动活动

答案 1 :(得分:0)

如上所述,您只想显示一个片段中的选项卡,那么您应该删除主布局中的选项卡并直接在片段布局中使用它。

保持

drawerlayout&gt; 工具栏&gt;片段[你的片段包含使用fragment.xml膨胀的标签]&gt; mainactivity布局文件中的navigationview

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

答案 2 :(得分:0)

以下是来自Google @ SlidingTabsBasic的精彩网页,我已经使用过了。我相信它可以满足您将滑动多个标签作为主要用户界面的要求。 MainActivity从FragmentActivity扩展为子类。换句话说,MainActivity可能是一个片段,但我不建议你让示例项目更复杂,让它开始变得简单。