如何使用android从导航抽屉导航活动?

时间:2016-09-01 08:42:55

标签: android android-activity navigation-drawer

我正在开发一个应用程序,我在其中创建了导航抽屉。通过使用此导航抽屉我想导航到活动但有一些问题。当我点击导航项目时它不会导航。我正在使用以下代码:

活动类

 public class Search_ActivityND extends AppCompatActivity {

                private DrawerLayout drawerLayout;
                private Toolbar toolbar;
                NavigationView navigationView;

                Context ctx = this;
                String ride_root, cab_type, email, ve_type, region;
                private boolean loggedIn = false;
                TextView textView, textView1, textView2;

                Spinner spinner1, spinner2;
                Button submit, logout;
                private ArrayList<String> students1;
                private ArrayList<String> students2;
                private JSONArray result1, result2, result;
                    @Override
                    protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.search_activity_nd);
                        toolbar = (Toolbar) findViewById(R.id.toolbar);
                        setSupportActionBar(toolbar);

                        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                        getSupportActionBar().setDisplayShowHomeEnabled(true);

                        initNavigationDrawer();


                        submit = (Button) findViewById(R.id.button_search);
                        // logout = (Button) findViewById(R.id.button3_logout);
                        textView = (TextView) findViewById(R.id.textView_user);


          }

                    public void initNavigationDrawer() {

                        navigationView = (NavigationView) findViewById(R.id.navigation_view);

                        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                            @Override
                            public boolean onNavigationItemSelected(MenuItem menuItem) {

                                int id = menuItem.getItemId();

                                switch (id) {
                                    case R.id.about:
                                        Intent i1 = new Intent(Search_ActivityND.this, About_Activity.class);
                                        startActivity(i1);
                                        drawerLayout.closeDrawers();
                                        break;
                                    case R.id.contact:
                                        Intent i2 = new Intent(Search_ActivityND.this, Contact_Activity.class);
                                        startActivity(i2);
                                        drawerLayout.closeDrawers();
                                        break;
                                    case R.id.privacy:
                                        Intent i3 = new Intent(Search_ActivityND.this, Privacy_Activity.class);
                                        startActivity(i3);
                                        drawerLayout.closeDrawers();
                                        break;

                                    case R.id.logout:
                                        finish();

                                }
                                return true;
                            }
                        });


                        SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                        loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false);
                        email = sharedPreferences.getString(Config.EMAIL_SHARED_PREF, "Not Available");

                        View header = navigationView.getHeaderView(0);
                        TextView tv_email = (TextView) header.findViewById(R.id.tv_email);
                        tv_email.setText(email);
                        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);

                        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

                            @Override
                            public void onDrawerClosed(View v) {
                                super.onDrawerClosed(v);
                            }

                            @Override
                            public void onDrawerOpened(View v) {
                                super.onDrawerOpened(v);
                            }
                        };
                        drawerLayout.addDrawerListener(actionBarDrawerToggle);
                        actionBarDrawerToggle.syncState();
                    }

                }

xml文件

   <android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_navigation"/>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bgbutton">

        <include layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></include>

        <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView3">



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RelativeLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:background="@color/white"
                android:weightSum="1"
                android:layout_gravity="center">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/selectregion"
                    android:textSize="23sp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginTop="10dp"
                    android:textColor="@color/black"
                    android:textStyle="bold"
                    android:id="@+id/textView2"
                    android:fontFamily="fonts/Helvetica.otf"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true" />


                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner_region"
                    android:fontFamily="fonts/Helvetica.otf"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/textView2" />


            </RelativeLayout>

            <RelativeLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:background="@color/white"
                android:weightSum="1"
                android:layout_gravity="center">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/travelfmto"
                    android:textColor="@color/black"
                    android:id="@+id/textView4"
                    android:textSize="23sp"
                    android:textStyle="bold"
                    android:fontFamily="fonts/Helvetica.otf"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="15dp"
                    android:layout_marginTop="10dp"/>

                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner_root"
                    android:fontFamily="fonts/Helvetica.otf"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/textView4" />
            </RelativeLayout>

            <RelativeLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:background="@color/white"
                android:weightSum="1"
                android:layout_gravity="center">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/selectvehicle"
                    android:textColor="@color/black"
                    android:id="@+id/textView6_ve"
                    android:textSize="23sp"
                    android:textStyle="bold"
                    android:fontFamily="fonts/Helvetica.otf"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="15dp"
                    android:layout_marginTop="10dp"/>

                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner_vetype"
                    android:fontFamily="fonts/Helvetica.otf"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/textView6_ve" />

            </RelativeLayout>

            <RelativeLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:background="@color/bgbutton"
                android:weightSum="1"
                android:layout_gravity="center">

                <Button
                    android:layout_width="350dp"
                    android:layout_height="45dp"
                    android:text="@string/searchtaxi"
                    android:textSize="15sp"
                    android:textColor="@color/black"
                    android:background="@drawable/mybutton_y"
                    android:fontFamily="fonts/Helvetica.otf"
                    android:id="@+id/button_search"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true" />
            </RelativeLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text=""
                android:fontFamily="fonts/Helvetica.otf"
                android:textColor="@color/bgbutton"
                android:textSize="10sp"
                android:id="@+id/textView_user" />


        </LinearLayout>




    </ScrollView>

</LinearLayout>

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

0 个答案:

没有答案