NavigationDrawer在itemclick上什么都不做

时间:2014-01-13 18:56:00

标签: android navigation onclick menuitem navigation-drawer

我搜索了几个小时,如果有人有或像我一样有问题,我找不到任何有助于解决这个问题...

问题是,NotificationDrawer打开了,我给了他所有的项目,但他没有注意到任何项目的onClick! APP只关闭抽屉,没有任何反应,尝试记录和不同类型的检查是否有任何项目被点击。

我用过的代码:

    public class run extends Activity implements OnInitListener {

            private String[] drawerListViewItems;
            private DrawerLayout drawerLayout;
            private ListView drawerListView;
            private ActionBarDrawerToggle actionBarDrawerToggle;

            // nav drawer title
            private CharSequence mDrawerTitle;

            // used to store app title
            private CharSequence mTitle;


         @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
            mTitle = mDrawerTitle = getTitle();
                    // get list items from strings.xml
                    drawerListViewItems = getResources().getStringArray(R.array.options_array);
                    // 2. App Icon 
                    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
                    // get ListView defined in activity_main.xml
                    drawerListView = (ListView) findViewById(R.id.left_drawer);

                    // Set the adapter for the list view
                    drawerListView.setAdapter(new ArrayAdapter<String>(this,
                            R.layout.drawer_list_item, drawerListViewItems));


                    // 2.1 create ActionBarDrawerToggle
                    actionBarDrawerToggle = new ActionBarDrawerToggle(
                            this,                  /* host Activity */
                            drawerLayout,         /* DrawerLayout object */
                            R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                            R.string.drawer_open,  /* "open drawer" description */
                            R.string.drawer_close  /* "clos

    e drawer" description */
                        ){
                    public void onDrawerClosed(View view) {
                        getActionBar().setTitle(mTitle);
                        // calling onPrepareOptionsMenu() to show action bar icons
                        invalidateOptionsMenu();
                    }

                    public void onDrawerOpened(View drawerView) {
                        getActionBar().setTitle(mDrawerTitle);
                        // calling onPrepareOptionsMenu() to hide action bar icons
                        invalidateOptionsMenu();
                    }
                };

                // 2.2 Set actionBarDrawerToggle as the DrawerListener
                drawerLayout.setDrawerListener(actionBarDrawerToggle);

                // 2.3 enable and show "up" arrow
                getActionBar().setDisplayHomeAsUpEnabled(true); 

                // just styling option
                drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

                drawerListView.setOnItemClickListener(new SlideMenuClickListener());

                if (savedInstanceState == null) {
                    // on first time display view for first nav item
                    Log.d("LOGTAG", "DoSomething");
                }
    /**
         * Slide menu item click listener
         * */
        private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                // display view for selected nav drawer item
                selectItem(position);
            }
        }

private void selectItem(int position) {
        Log.d("LOGTAG", "ETWAS WURDE GEÄHLT! " + position);
        switch(position) {
        case 0:
            Log.d("LOGTAG", "ITEM 0 GEWÄHLT!");
        case 1:
            Log.d("LOGTAG", "Hier ist jetzt DING 1 gewählt!");
            Toast.makeText(mContext, "Item 1 Clicked", Toast.LENGTH_SHORT).show();
            //          mDrawerList.setItemChecked(position, true);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Lauf beenden")
            .setMessage("Bist du sicher?")
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton("Ja",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int which) {
                    // Yes button clicked, do something
                    finish();
                }
            }).setNegativeButton("Nein", null).show();
            //          return true;

        default:
            //          return (position);
        }

    }

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

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);

        menu.getItem(0).setVisible(false);
        menu.getItem(1).setVisible(false);
        menu.getItem(3).setVisible(false);


        return super.onCreateOptionsMenu(menu);
    }
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // toggle nav drawer on selecting action bar app icon/title
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action bar actions click
        switch (item.getItemId()) {
        case R.id.close:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
}

的strings.xml

<!-- TestTest -->
    <string-array name="options_array">
        <item>End run</item>
        <item>Test X</item>
    </string-array>
    <string name="drawer_open">StringDrawerOpen</string>
    <string name="drawer_close">StringDrawerClose</string>

drawer_list_items.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textColor="#fff"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

activity_main.xml中

  

  

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


<ListView
    android:id="@+id/left_drawer"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="480dp" >

        <ImageView
            android:id="@+id/herzschlagImg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/heartImgDes"
            android:src="@drawable/heartbeat" />

        <ImageView
            android:id="@+id/distanzImg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/distImgDes"
            android:paddingTop="150dp"
            android:src="@drawable/distance" />

        <TextView
            android:id="@+id/geschwindigkeit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/distanz"
            android:layout_alignBottom="@+id/distanz"
            android:layout_toLeftOf="@+id/geschwindigkeitVar"
            android:text="@string/speed"
            android:textSize="40sp" />

        <TextView
            android:id="@+id/herzschlag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="48dp"
            android:minHeight="60dp"
            android:minWidth="200dp"
            android:text="@string/heartbeat"
            android:textSize="40sp" />

        <TextView
            android:id="@+id/herzschlagVar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/herzschlag"
            android:layout_alignBottom="@+id/herzschlag"
            android:layout_marginLeft="40dp"
            android:layout_toRightOf="@+id/distanzVar"
            android:text="@string/heartbeatDes"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/geschwindigkeitVar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/distanz"
            android:layout_alignBottom="@+id/distanz"
            android:layout_alignParentRight="true"
            android:text="@string/speedDes"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/distanz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/distanzImg"
            android:layout_alignRight="@+id/start"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="20dp"
            android:text="@string/distance"
            android:textSize="40sp" />

        <TextView
            android:id="@+id/feedback"
            android:layout_width="fill_parent"
            android:layout_height="85dp"
            android:layout_alignParentBottom="true"
            android:background="#848482"
            android:gravity="center"
            android:text="@string/feed"
            android:textSize="30sp" />

        <Button
            android:id="@+id/start"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/stop"
            android:layout_alignBottom="@+id/stop"
            android:layout_toLeftOf="@+id/herzschlag"
            android:onClick="startTime"
            android:text="@string/btn_start"
            android:textSize="35sp" />

        <Button
            android:id="@+id/stop"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_above="@+id/feedback"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="29dp"
            android:layout_marginRight="29dp"
            android:onClick="stopTime"
            android:text="@string/btn_stop"
            android:textSize="35sp" />

        <TextView
            android:id="@+id/distanzVar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/distanz"
            android:layout_alignBottom="@+id/distanz"
            android:layout_marginRight="36dp"
            android:layout_toLeftOf="@+id/geschwindigkeit"
            android:text="@string/distanceDes"
            android:textSize="30sp" />

        <ImageView
            android:id="@+id/zeitImg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:contentDescription="@string/chronoDes"
            android:paddingTop="20dp"
            android:src="@drawable/timer" />

        <Chronometer
            android:id="@+id/zeit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/geschwindigkeit"
            android:layout_below="@+id/geschwindigkeit"
            android:paddingTop="25dp"
            android:textSize="40sp" />
    </RelativeLayout>
</LinearLayout>
     

如果格式错误,请抱歉。 我没有得到异常或其他东西,日志也没有帮助...... 希望我没有忘记发布任何内容。

我真的希望你们能帮助我!

1 个答案:

答案 0 :(得分:0)

在这段代码中

/**
     * Slide menu item click listener
     * */
    private class SlideMenuClickListener implements
    ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // display view for selected nav drawer item
            selectItem(position);
        }
    }

OnItemClickListener接口仅在AdapterView上定义,而不在ListView上定义。据此,您的代码甚至应该在运行之前给出错误。

ListView.OnItemClickListener不应该有效。正确的应该是AdapterView.OnItemClickListener