简体   繁体   中英

Fragment not showing up despite being added to view

I am making an app where I am using fragments , My problem is that despite the fragment being added to the view successfully, it is not showing up in my code.

What could I be doing wrong?

Here is my main activity where I am adding the fragments

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

As you can see, I have added them to the fragment_container that is a FrameLayout in my app_bar_main.xml file.

Here is the main activity code

     <?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>

And this is the code for the 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>

What could I be doing wrong? Thanks

Your linear layout is horizontal. Your frame layout may have been pushed off screen

Since your layout is horizontal, your fragment will be pushed off sight and it will not be visible, It is therefore good to replace it with vertical. It wil also ve advisable to replace

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

with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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