1

ich für immer haben kämpfen mit diesem und bin der Hoffnung, endlich Figur es mit etwas Hilfe. Ich versuche, der Symbolleiste auf dem Startbildschirm (Hauptaktivität) meiner App eine Navigationsleiste hinzuzufügen. Im Moment wird die Symbolleiste nicht angezeigt wird, mit dem DrawerLayout in meinem XML und das nav Schublade Symbol auch ins't zeigt. Um die Schublade zu öffnen, muss ich nach rechts wischen. Wenn ich dann auf ein Element in der Schublade klicke, wird die Ansicht nicht geändert. Die 2 Gegenstände in den Schubladen sind beide Fragmente. Ich glaube, es gibt ein Problem in meinem Code sowie in meinem XML, aber ich bin nicht gut mit Android-XML-Dateien.Navigationsleiste nicht angezeigt wird und funktioniert nicht auf Klick

RobotChooser.java entsprechender Code

public class RobotChooser extends AppCompatActivity implements AddEditRobotDialogFragment.DialogListener, ConfirmDeleteDialogFragment.DialogListener, ListView.OnItemClickListener { 

private View mEmptyView; 
private RecyclerView mRecyclerView; 
private RecyclerView.Adapter mAdapter; 

private ShowcaseView showcaseView; 
private boolean addedRobot; 
private Toolbar mToolbar; 

private String[] mFeatureTitles; 
private DrawerLayout mDrawerLayout; 
private ListView mDrawerList; 
private int drawerIndex = 1; 
private String mTitle; 
private String mDrawerTitle; 
private ActionBarDrawerToggle mDrawerToggle; 

Fragment fragment; 


public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    this.setContentView(R.layout.robot_chooser); 

    mEmptyView = findViewById(R.id.robot_empty_view); 
    mRecyclerView = (RecyclerView) findViewById(R.id.robot_recycler_view); 

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this); 
    mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 
    mRecyclerView.setLayoutManager(mLayoutManager); 

    mToolbar = (Toolbar) findViewById(R.id.robot_chooser_toolbar); 
    setSupportActionBar(mToolbar); 

    RobotStorage.load(this); 

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_robot_chooser); 
    mFeatureTitles = getResources().getStringArray(R.array.chooser_titles); //Where you set drawer item titles 
    mDrawerList = (ListView) findViewById(R.id.left_drawer2); 

    mTitle="ROS Control"; 
    mDrawerTitle=mTitle; 

    if (getActionBar() != null) { 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 
    } 

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
      R.string.drawer_open, 
      R.string.drawer_close) { 
     public void onDrawerClosed(View view) { 
      //getActionBar().setTitle(mTitle); 
      invalidateOptionsMenu(); // creates call to 
      // onPrepareOptionsMenu() 
     } 

     public void onDrawerOpened(View drawerView) { 
      //getActionBar().setTitle(mDrawerTitle); 
      invalidateOptionsMenu(); // creates call to 
      // onPrepareOptionsMenu() 
     } 
    }; 

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

    int[] imgRes = new int[]{ 
      R.drawable.ic_android_black_24dp, 
      R.drawable.ic_settings_black_24dp, 
      R.drawable.ic_info_outline_black_24dp 
    }; 

    List<DrawerItem> drawerItems = new ArrayList<>(); 

    for (int i = 0; i < mFeatureTitles.length; i++) { 
     drawerItems.add(new DrawerItem(mFeatureTitles[i], imgRes[i])); 
    } 

    NavDrawerAdapter drawerAdapter = new NavDrawerAdapter(this, 
      R.layout.nav_drawer_menu_item, 
      drawerItems); 

    mDrawerList.setAdapter(drawerAdapter); 
    mDrawerList.setOnItemClickListener(this); 

    private void selectItem(int position){ 
    Bundle args = new Bundle(); 
    FragmentManager fragmentManager = getFragmentManager(); 

    switch (position) { 
     case 0: 

      mDrawerLayout.closeDrawers(); 
      return; 

     case 1: 
      fragment = new PreferencesFragment(); 
      fragment.setArguments(args); 

      // Insert the fragment by replacing any existing fragment 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame2, fragment) 
        .commit(); 
      //fragmentsCreatedCounter = 0 

      break; 

     case 2: 
      fragment = new AboutFragment(); 
      //fragmentsCreatedCounter = fragmentsCreatedCounter + 1; 
      fragment.setArguments(args); 

      // Insert the fragment by replacing any existing fragment 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame2, fragment) 
        .commit(); 

      break; 

     default: 
      break; 
    } 


    // Highlight the selected item, update the title, and close the drawer 
    mDrawerList.setItemChecked(position, true); 
    mDrawerLayout.closeDrawer(mDrawerList); 
    setTitle(mFeatureTitles[position]); 
} 

@Override 
public void setTitle(CharSequence title) { 
    try { 
     //noinspection ConstantConditions 
     getActionBar().setTitle(title); 
    } catch (NullPointerException e) { 
     // Ignore 
    } 
} 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    // Sync the toggle state after onRestoreInstanceState has occurred. 
    mDrawerToggle.syncState(); 
} 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    selectItem(position); 

robot_chooser.xml

<?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" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
    android:id="@+id/robot_chooser_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:elevation="4dp" 
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout_robot_chooser" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- The main content view --> 
    <LinearLayout 
     android:id="@+id/content_frame2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" /> 

    <fragment 
     android:id="@+id/hud_fragment" 
     android:name="com.robotca.ControlApp.Fragments.PreferencesFragment" 
     android:layout_width="wrap_content" 
     android:layout_height="64sp"/> 
    <!--tools:layout="@layout/fragment_hud"--> 

    <fragment 
     android:id="@+id/about_fragment" 
     android:name="com.robotca.ControlApp.Fragments.AboutFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:layout="@layout/fragment_about"/> 


<android.support.v7.widget.RecyclerView 
    android:id="@+id/robot_recycler_view" 
    android:paddingTop="5dp" 
    android:scrollbars="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 
<TextView 
    android:id="@+id/robot_empty_view" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:textSize="20sp" 
    android:text="@string/no_robots" 
    android:elevation="3dp" 
    android:layout_gravity="center" 
    android:visibility="gone" 
    android:gravity="center" /> 

    <ListView android:id="@+id/left_drawer2" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#ddd"/> 
</android.support.v4.widget.DrawerLayout> 

+0

Verwenden Sie Android Studio? Erstellen Sie eine Demo mit einer eingebauten Vorlage und vergleichen Sie diesen Code mit Ihrem. Das kannst du auch in Eclipse machen. – Harry

+0

Ja, ich benutze Android Studio. Ich denke das große Problem ist mein Xml und auch wie ich das Fragment geklickt habe in der Schublade, um den neuen Bildschirm gesehen zu haben –

Antwort

0

xml sollte wie dies

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:id="@+id/profileDrawer" 
android:layout_height="match_parent" > 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/robot_chooser_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:elevation="4dp" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

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

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/robot_recycler_view" 
     android:paddingTop="5dp" 
     android:scrollbars="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <TextView 
     android:id="@+id/robot_empty_view" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:textSize="20sp" 
     android:text="@string/no_robots" 
     android:elevation="3dp" 
     android:layout_gravity="center" 
     android:visibility="gone" 
     android:gravity="center" /> 
    </FrameLayout> 

</LinearLayout> 

<ListView 
    android:id="@+id/left_drawer2" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#eee" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="0dp" 
    android:listSelector="@android:color/darker_gray" /> 

</android.support.v4.widget.DrawerLayout> 

wird dann in dem Schalt Fällen 1 und 2 , du ne Ed, um die Recycler-Ansicht so zu verbergen

mRecyclerView.setVisibility(View.GONE); 
Verwandte Themen