【问题标题】:Navigation Drawer's click event not working导航抽屉的点击事件不起作用
【发布时间】:2017-07-13 01:04:15
【问题描述】:

这是我的布局。我有一个导航抽屉和活动按钮中的一个按钮 Click 事件正在工作,但 Drawer 的 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);

    }
}

}

【问题讨论】:

标签: android


【解决方案1】:

在 XML 布局中,您为 Button 标签提供了以下属性

  android:onClick="actionbtn"

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

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

 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>

【讨论】:

  • 你能把你的actionbtn方法贴在这里吗?
  • 我的按钮工作正常我的抽屉不工作...我会发布
  • 你能告诉我我的 xml 文件是否正确,因为当我将相对布局作为根布局时,当我将 Darwer 布局作为根按钮时,抽屉可以正常工作,但抽屉不起作用。
  • 如果您认为我给出的答案是您问题的正确答案,请接受答案。这将帮助其他人在他们的应用程序中使用这个答案
【解决方案2】:
  public void onDrawerOpened(View drawerView) {
                drawerListView.bringToFront();}

【讨论】:

  • 一般来说,如果答案包含对代码的用途的解释,以及为什么在不介绍其他人的情况下解决问题的原因,答案会更有帮助。
【解决方案3】:

您的 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>

请记住,您不能在导航抽屉项目中包含布局项目。

【讨论】:

    【解决方案4】:

    试试这个 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      相关资源
      最近更新 更多