导航抽屉点击事件无效

时间:2014-05-05 06:21:56

标签: android

这是我的布局。我有一个导航抽屉和活动按钮中的一个按钮Click事件正在运行,但抽屉的Click事件不起作用。

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

    <!-- The main content view -->

    <Frame Layout
        android:id="@+id/content_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />



    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#666"
        android:dividerHeight="1dp"
        android:background="#333"
        android:paddingLeft="15sp"
        android:paddingRight="15sp"
       />
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e5e5e5"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


<Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:onClick="actionbtn"
        android:text="Button" />
 </RelativeLayout>


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

我已实施的代码:

public class MainActivity extends Activity {

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // get list items from strings.xml
    drawerListViewItems = getResources().getStringArray(R.array.items);
    // 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_listview_item, drawerListViewItems));

    // 2. App Icon 
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    // 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  /* "close drawer" description */
            );

    // 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 DrawerItemClickListener());
    Button btn = (Button)findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();  
        }
    });
}


@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 onOptionsItemSelected(MenuItem item) {

     // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
    // then it has handled the app icon touch event

    if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(MainActivity.this, ((TextView)view).getText(), Toast.LENGTH_LONG).show();
        drawerLayout.closeDrawer(drawerListView);

    }
}

}

4 个答案:

答案 0 :(得分:1)

在XML布局中您已向Button标签

提供以下属性
  android:onClick="actionbtn"

这意味着当用户点击按钮时,将调用 actionbtn 方法。

MainActivity 下创建名为 actionbtn 的方法,如下所示,参数为查看类的对象。

 public void actionbtn(View view){
        << Put your code for drawer action here
        drawerLayout.openDrawer(drawerListView)>>;
        Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show(); 
 }

请注意,我不确定要放入方法的代码,但在 MainActivity 类中创建上述方法可以解决您的问题。

更新 -

请尝试使用以下XML布局

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

   <!-- The main content view -->

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#e5e5e5"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
       >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:onClick="actionbtn"
        android:text="Button" />
   </RelativeLayout>

   <!-- The navigation drawer -->

   <ListView
       android:id="@+id/left_drawer"
       android:layout_width="240dp"
       android:layout_height="match_parent"
       android:layout_gravity="start"
       android:background="#333"
       android:choiceMode="singleChoice"
       android:divider="#666"
       android:dividerHeight="1dp"
       android:paddingLeft="15dp"
       android:paddingRight="15dp" />

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

答案 1 :(得分:1)

  public void onDrawerOpened(View drawerView) {
                drawerListView.bringToFront();}

答案 2 :(得分:0)

您的java代码非常完美。问题在于你的xml文件。 您需要从xml代码的android.support.v4.widget.DrawerLayout中删除此部分,然后尝试它将像冠军一样运行。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e5e5e5"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


<Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:onClick="actionbtn"
        android:text="Button" />
 </RelativeLayout>

请记住,您无法将导航抽屉项目包含在布局项目中。

答案 3 :(得分:0)

试试这个 XML: -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <!--
         As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions.
        -->

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

        <RelativeLayout
            android:id="@+id/relative_week"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true" >

            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:onClick="actionbtn"
                android:text="Button" />
        </RelativeLayout>

        <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="300dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:dividerHeight="0.5dp" />
    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>