导航栏未显示且未点击

时间:2016-03-22 23:38:33

标签: android xml android-layout android-fragments navigation-drawer

我一直在努力解决这个问题,并希望最终能在一些帮助下找到答案。我试图在我的应用的主屏幕(主要活动)上的工具栏中添加导航栏。 现在,工具栏没有显示,我的XML中的DrawerLayout和导航抽屉图标也没有显示。打开抽屉我必须向右滑动。然后,当我单击抽屉中的项目时,视图不会更改。抽屉中的2个项目都是片段。我相信我的代码和XML都存在问题,但我对android 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>

1 个答案:

答案 0 :(得分:0)

xml应该是这样的

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

然后在开关案例1和2中,您需要隐藏回收器视图,如此

mRecyclerView.setVisibility(View.GONE);