1

私はこれを永遠に苦労しており、最終的にいくつかの助けを借りてそれを把握したいと考えています。私は私のアプリのホーム画面(メインアクティビティ)のツールバーにナビゲーションバーを追加しようとしています。 今、ツールバーは表示されません。私のXMLにはDrawerLayoutがあり、ナビゲーションドロワーアイコンも表示されません。私は右にスワイプしなければならない引き出しを開く。引き出し内のアイテムをクリックすると、ビューは変更されません。引き出し内の2つのアイテムは両方とも断片です。私は自分のコードと私のXMLに問題があると信じていますが、私はアンドロイドのXMLファイルには不満です。ナビゲーションバーが表示されず、クリックで機能しない

RobotChooser.java関連するコード

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

アンドロイドスタジオを使用していますか?組み込みのテンプレートでデモを作成し、あなたのコードと比較してください。あなたはまた、日食でそれを行うことができます。 – Harry

+0

はい私はアンドロイドスタジオを使用しています。大きな問題は私のXMLだと思います。また、引き出しの中でクリックされた部分を新しい画面にする方法 –

答えて

0

XMLは、スイッチケース1及び2において、この

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

ようにする必要があります、あなたねこのようにリサイクラービューを非表示にするには

mRecyclerView.setVisibility(View.GONE); 
関連する問題