简体   繁体   English

Android onOptionsItemSelected打开一个新片段

[英]Android onOptionsItemSelected open a new fragment

I am currently have an ActionBar with tabs and an OptionsMenu. 我目前有一个带有选项卡和OptionsMenu的ActionBar。 My tabs open each fragment properly but I can't quite figure out how to use onOptionsItemSelected to open a fragment. 我的标签页正确地打开了每个片段,但我还不太清楚如何使用onOptionsItemSelected打开片段。

@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();
        if (id == R.id.action_info) {
            new ParentInfoSectionFragement();
            return true;
        } else if (id == R.id.action_history){
            new NotificationHistorySectionFragment();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

That is what I tried and is obviously wrong. 那是我尝试过的,显然是错误的。

Any help would be deeply appreciated. 任何帮助将不胜感激。 Thank you 谢谢

activity_main.xml activity_main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.parentprojectmobile.MainActivity" />

fragment_history.xml fragment_history.xml

RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.parentprojectmobile.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/history_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_history" />

/RelativeLayout>

All fragments are just copies of original fragment and only have textviews for learning purpose. 所有片段都是原始片段的副本,并且仅具有用于学习目的的文本视图。

Use the Fragment Transaction of your activity 使用活动的片段交易

example: 例:

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();       
    transaction.replace(R.id.layout_id, new ParentInfoSectionFragement(), null).commit();

where R.id.layout_id is the layout id you want to place your fragment. 其中R.id.layout_id是您要放置片段的布局ID。

you need to handle your fragment stack 您需要处理您的片段堆栈

@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();
    if (id == R.id.action_info) {
        new ParentInfoSectionFragement();
        return true;
    } else if (id == R.id.action_history){
        new NotificationHistorySectionFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        notificationHistorySectionFragment = new NotificationHistorySectionFragment();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, notificationHistorySectionFragment);
        fragmentTransaction.addToBackStack(null);

        //Commit Transaction
        fragmentTransaction.commit();
    }
    return super.onOptionsItemSelected(item);
}

In this example R.id.fragment_container is the ID of your fragment container in your layout. 在此示例中, R.id.fragment_container是布局中片段容器的ID。

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

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