简体   繁体   English

Android Navigation Drawer - 调用基础内容视图

[英]Android Navigation Drawer - Call Base Content View

I wanted to know how to call the base content_main.xml (layout) from the Navigation Drawer after calling other FragmentActivity.class , wherein the content_main.xml is the first view you'll see on starting the android application.我想知道如何在调用其他FragmentActivity.class之后从导航抽屉中调用基础content_main.xml (布局),其中content_main.xml是您在启动 android 应用程序时将看到的第一个视图。

I can't do this because I think the Main Activity ( MainActivity_Fragment.class ) along with the NavigationDrawer extends AppCompatActivity ... If you need a code I'll just display the code snippets... since the code too long.我不能这样做,因为我认为 Main Activity ( MainActivity_Fragment.class ) 与 NavigationDrawer 一起扩展了AppCompatActivity ...如果您需要代码,我将只显示代码片段...因为代码太长。

If there's a way to call the default contentview, let me know... I searched google many times and unfortunately no answers... Please Help, thank you...如果有办法调用默认的内容视图,请告诉我......我在谷歌上搜索了很多次,不幸的是没有答案......请帮助,谢谢......

MainActivity_Fragment.class MainActivity_Fragment.class

public class MainActivity_Fragment extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener,
    SlideFragment.OnFragmentInteractionListener,
    View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
...
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
...
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    if (id == R.id.nav_camera) {
        //startActivity(new Intent(this, MainActivity_Fragment.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        MainActivity_Fragment newFragment = new MainActivity_Fragment();
        transaction.replace(R.id.activity_translator, newFragment); // THIS IS THE ERROR WHEREIN MAINACTIVITY.CLASS SHOULD IMPLEMENT FRAGMENT
        transaction.addToBackStack(null);
        transaction.commit();
...
}

And this is my content_main.xml layout这是我的content_main.xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout_for_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/card_activity_background"
android:orientation="vertical"
tools:showIn="@layout/app_bar_main">

...// MY DEFAULT DISPLAY CONTENT

</LinearLayout>

Platform: Android Studio 2.2 Preview 5平台:Android Studio 2.2 Preview 5

Android SDK Target: 24 Android SDK 目标:24

Min 15 15 分钟

NOTE : I didn't used FragmentActivity as extension of my Class, I simply used AppCompatActivity注意:我没有使用FragmentActivity作为我的类的扩展,我只是使用AppCompatActivity

try to do this may be helpful to you just change the bellow line尝试这样做可能对您有所帮助,只需更改波纹管即可

FragmentTransaction transaction = getFragmentManager().beginTransaction();

To

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

In onCreate() of MainActivity_Fragment, the first view should be default fragment like this:在 MainActivity_Fragment 的 onCreate() 中,第一个视图应该是这样的默认片段:

            mFragmentManager = getSupportFragmentManager();
            mFragmentTransaction = mFragmentManager.beginTransaction();
            //Default fragment when begin
            Bundle bundle = new Bundle();
            HomeFragment homeFragment = new HomeFragment();
            homeFragment.setArguments(bundle);
            mFragmentTransaction.replace(R.id.containerView, homeFragment);
            mFragmentTransaction.commit();

I am not sure, I think it's in my own Activity, I should have re-code my MainActivity_Fragment.class and extend it to FragmentActivity .我不确定,我认为它在我自己的 Activity 中,我应该重新编码我的MainActivity_Fragment.class并将其扩展到FragmentActivity But I'm still looking forward for the answer to not migrate to FragmentActivity .但我仍然期待不迁移到FragmentActivity的答案。

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

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