尽管被添加到视图中,片段仍未显示

时间:2018-05-11 15:56:56

标签: java android android-studio fragment

我正在制作一个应用程序,我正在使用片段,我的问题是,尽管片段已成功添加到视图中,但它并未显示在我的代码中。

我可能做错了什么?

  

这是我添加片段的主要活动

    import android.annotation.TargetApi;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.os.Build;
    import android.os.Bundle;
    import android.support.annotation.RequiresApi;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v4.app.FragmentTransaction;
    import android.view.View;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;

    public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
MenuItem menuSettings;
//Fragments variables
DictionaryFragment dictionaryFragment;
BookmarkFragment bookmarkFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

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

    // new dictionary objects
    dictionaryFragment = new DictionaryFragment();
    bookmarkFragment = new BookmarkFragment();
    // Call the gotoFragment method and pass the dictionaryFragment as the default ontop parameter
    gotToFragment(dictionaryFragment, true);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    menuSettings = menu.findItem(R.id.action_settings);

   String id = Global.getState(this,"dic_type");
   // Handles the null value on first run
   if (id !=null){
       onOptionsItemSelected(menu.findItem(Integer.valueOf(id)));
   }
    return true;
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@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();
    Global.saveState(this,"dic_type",String.valueOf(id));

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_engluo) {
        menuSettings.setIcon(getDrawable(R.drawable.eng_luo));

    }
    else if(id == R.id.action_luoeng){
        menuSettings.setIcon(getDrawable(R.drawable.luo_eng));

    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_bookmark) {
        gotToFragment(bookmarkFragment, false);
    } else if (id == R.id.nav_rate) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_help) {

    } else if (id == R.id.nav_about) {

   // } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

void gotToFragment(Fragment fragment, boolean isTop){
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.fragment_container, fragment);
    if(!isTop){
        fragmentTransaction.addToBackStack(null);
    }
    fragmentTransaction.commit();
    }
}

如您所见,我已将它们添加到我的app_bar_main.xml文件中作为FrameLayout的fragment_container。

  

以下是主要活动代码

     <?xml version="1.0" encoding="utf-8"?>
     <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"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    <include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/activity_main_drawer" />
   </android.support.v4.widget.DrawerLayout>
  

这是app_bar_main的代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <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"
        app:contentInsetStartWithNavigation="0dp">

        <EditText
            android:id="@+id/edit_search"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/radius_edit_text"
            android:drawableLeft="@drawable/ic_search"
            android:textColor="@color/colorBlack"
            android:textColorHint="@color/colorGrey"
            android:hint="@string/search"
            android:textSize="20sp"
            android:paddingLeft="5dp"
            android:drawablePadding="5dp"
            android:inputType=""
            android:drawableStart="@drawable/ic_search"
            android:paddingStart="5dp" />
            </android.support.v7.widget.Toolbar>

           </android.support.design.widget.AppBarLayout>

       <FrameLayout
       android:id="@+id/fragment_container"
       android:layout_width="match_parent"
      android:layout_height="match_parent">
     </FrameLayout>
    </LinearLayout>

我可能做错了什么? 感谢

2 个答案:

答案 0 :(得分:2)

您的线性布局是水平的。您的框架布局可能已被推离屏幕

答案 1 :(得分:1)

由于您的布局是水平的,因此您的片段将被推开,并且它将不可见,因此最好用垂直替换它。 它也可取代

    fragmentTransaction.add(R.id.fragment_container, fragment);

    fragmentTransaction.replace(R.id.fragment_container, fragment);