简体   繁体   English

Android抽屉导航单击事件

[英]Android drawer navigation click events

I made a drawer navigation based on a tutorial on the internet and I followed everything but when I open my app everything is working correctly except my onclick event. 我根据互联网上的教程进行了抽屉式导航,我遵循了所有内容,但是当我打开我的应用程序时,除了onclick事件之外,其他所有功能均正常运行。 I'm pretty new to android (2 weeks) and tried to figure it out by myself but it didn't work out. 我刚接触android(2周),并试图自己解决这个问题,但没有成功。 I tries onclicklistener but that one didn't give any possitive feedback for me. 我尝试了onclicklistener,但没有给我任何积极的反馈。

How do I make a click event that will take me to another activity? 我如何进行点击事件以将我带到另一项活动?

my code: 我的代码:

public class LayoutOneActivity extends ActionBarActivity {
    String[] menu;
    DrawerLayout dLayout;
    ListView dList;
    ArrayAdapter<String> adapter;



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

        Resources res = getResources();
        String [] menu_items = res.getStringArray(R.array.menu_items); // String array where the menu items will be stored        

            menu = menu_items; // Variable for the menu items
            dLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // Looking for the id "drawer_layout" and apply as layout
            dList = (ListView) findViewById(R.id.left_drawer); // Looking for the listview where the items will be stored
            adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);// Making a new adapter
            dList.setAdapter(adapter);// Give the list-layout the variable "adapter" which is an adapter (obviously)            
            dList.setSelector(R.drawable.back);// Sets the colour of the list-layout

            dList.setOnItemClickListener(new OnItemClickListener()
            {   
                @Override
                public void onItemClick(AdapterView<?> arg0, View v, int position, long id) 
                {

                  dLayout.closeDrawers();// The layout will be clossed when clicked outside the layout
                  Bundle args = new Bundle();// New bundle which will parse the data between various activities
                  args.putString("Menu", menu[position]);
                  Fragment detail = new DetailFragment();
                  detail.setArguments(args);
                  FragmentManager fragmentManager = getFragmentManager();
                  fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
            }
         });
      }

layout_one layout_one

<android.support.v4.widget.DrawerLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
     android:layout_height="match_parent">
     <FrameLayout
         android:id="@+id/content_frame"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@drawable/bg">

     <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:layout_centerHorizontal="true"
         android:layout_marginBottom="48dp"        
         android:orientation="vertical">

         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="90dp"
             android:orientation="horizontal">

             <Button
                 android:layout_width="80dp"
                 android:layout_height="85dp"
                 android:background="@drawable/button"
                 android:onClick="openNewActivity1"
                 android:text="@string/clickActivity1" />

             <Button
                 android:layout_width="80dp"
                 android:layout_height="85dp"
                 android:background="@drawable/button"
                 android:onClick="openNewActivity2"
                 android:text="@string/clickActivity2" />

             <Button
                 android:layout_width="80dp"
                 android:layout_height="85dp"
                 android:background="@drawable/button"
                 android:onClick="openNewActivity3"
                 android:text="@string/clickActivity3" />
         </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
           android:layout_height="90dp"
             android:orientation="horizontal">

             <Button
                 android:layout_width="80dp"
                 android:layout_height="85dp"
                 android:background="@drawable/button"
                 android:onClick="openNewActivity4"
                 android:text="@string/clickActivity4"/>

             <Button
                 android:layout_width="80dp"
                 android:layout_height="85dp"
                 android:background="@drawable/button"
                 android:onClick="openNewActivity5"
                 android:text="@string/clickActivity5"/>

             <Button
android:layout_width="80dp"
               android:layout_height="85dp"
                 android:background="@drawable/button"
                 android:onClick="openNewActivity6"
                 android:text="@string/clickActivity6"/>
         </LinearLayout>

     </LinearLayout>

 </RelativeLayout> </FrameLayout>

         <ListView android:id="@+id/left_drawer"
         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="#fff"/> </android.support.v4.widget.DrawerLayout>

menu_detail_fragment menu_detail_fragment

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:gravity="center"
         android:background="#5ba4e5"
      android:layout_height="match_parent">
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:textSize="40px"
             android:textColor="#ffffff"
             android:layout_gravity="center"
            android:id="@+id/detail"/> </LinearLayout>

To set a navigation menu onClickListener you have to implement the NavigationView.OnNavigationItemSelectedListener interface on the Activity. 要在onClickListener上设置导航菜单,您必须在Activity上实现NavigationView.OnNavigationItemSelectedListener接口。

On the onCreate method you have to define the navigation onClickListener to the context Activity like this: 在onCreate方法上,您必须这样定义上下文活动的导航onClickListener:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

Then you have to Override the method 然后,您必须重写方法

@Override
public boolean onNavigationItemSelected(MenuItem item)

I write an example of how to implement the method below: 我写了一个有关如何实现以下方法的示例:

@Override
public boolean onNavigationItemSelected(MenuItem item){
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        EventFragment eventFragment = new EventFragment();
        Bundle bundle = new Bundle();
        bundle.putString("selectedEvent", "Urban");
        eventFragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame,eventFragment).commit();
    } else if (id == R.id.nav_gallery) {

        EventFragment eventFragment = new EventFragment();
        Bundle bundle = new Bundle();
        bundle.putString("selectedEvent", "Cosmos");
        eventFragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame,eventFragment).commit();

    } else if (id == R.id.nav_slideshow) {

        EventFragment eventFragment = new EventFragment();
        Bundle bundle = new Bundle();
        bundle.putString("selectedEvent", "Sink");
        eventFragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame,eventFragment).commit();

    } else if (id == R.id.nav_share) {
        ShopListFragment intent = new ShopListFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame, intent).commit();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(START);
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM