将工具栏中的视图从“活动”转换为“片段”

时间:2016-01-23 18:59:37

标签: android android-fragments android-support-library android-toolbar

我有一个AppCompatActivity屏幕,其中包含工具栏,导航抽屉和内容中的自定义视图。我想显示配置。我不会使用对话框,因为它会很大并且会打开对话框。我的朋友建议我重写代码以获得两个片段并在它们之间切换。

当前代码:

public class PuzzleActivity extends AppCompatActivity implements
    NavigationView.OnNavigationItemSelectedListener

protected void onCreate(Bundle state) {
    setContentView(R.layout.activity_puzzle);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

activity_puzzle.xml(简化)

<android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout"
android:fitsSystemWindows="true" tools:openDrawer="start">

<include
    layout="@layout/app_bar_puzzle"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:fitsSystemWindows="true"
    app:menu="@menu/drawer_main" />

app_bar_puzzle.xml(简化)

<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_puzzle" />

最后是content_puzzle.xml(再次缩小,为简单起见,自定义视图替换为textView)

<RelativeLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Content"/>

最初我创建了一个Fragment并用这个片段替换了android.R.id.content:我最终得到重叠片段和原始视图:

enter image description here

我有一个理论认为,FragmentManager无法替换从XML定义的活动视图或者Activity视图和片段之间的不兼容性。所以我决定引入新的Fragment并将所有UI移动到这个片段。

PuzzleFragment.java派生自支持库:

public class PuzzleFragment extends Fragment {    
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_puzzle, container, false);

        Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(getActivity(), drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();    

        return view;
    }

但是在工具栏初始化时失败了:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:98)
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:91)
at android.support.v7.app.ToolbarActionBar.<init>(ToolbarActionBar.java:73)
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:205)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
at PuzzleFragment.onCreateView(PuzzleFragment.java:30)

工具栏为空,findViewById在片段/ inflater.inflate(R.layout.activity_puzzle, container, false)/中找不到它。但它确实在Activity / setContentView(R.layout.activity_puzzle) /中。

我怎样才能让它发挥作用?

1 个答案:

答案 0 :(得分:0)

诀窍是将初始化过程移动到后期阶段,如源代码所示:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.d(logTag, "onCreateView()");
    return inflater.inflate(R.layout.activity_puzzle, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    Log.d(logTag, "onActivityCreated()");
    super.onActivityCreated(savedInstanceState);
    Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
    ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(getActivity(), drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) getActivity().findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}