android– 导航抽屉中的onClick事件

android– 导航抽屉中的onClick事件,第1张

概述我在Android中制作了一个导航抽屉,我想在其中实现onClick.这是我的主要活动:publicclassMainActivityextendsAppCompatActivity{privateDrawerLayoutmDrawerLayout;privateActionBarDrawerToggleaToggle;privateToolbartoolbar;privateRecyclerViewrecyclerView

我在Android中制作了一个导航抽屉,我想在其中实现onClick.这是我的主要活动:

public class MainActivity extends AppCompatActivity {private DrawerLayout mDrawerLayout;private ActionbarDrawerToggle aToggle;private Toolbar toolbar;private RecyclerVIEw recyclerVIEw;private RecyclerAdapter recyclerAdapter;private RecyclerVIEw.Adapter adapter;private NavigationVIEw navigationVIEw;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    mDrawerLayout = (DrawerLayout) findVIEwByID(R.ID.drawer);    aToggle = new ActionbarDrawerToggle(this, mDrawerLayout, R.drawable.navig, R.string.open, R.string.Close);    navigationVIEw = (NavigationVIEw) findVIEwByID(R.ID.nav_vIEw);    mDrawerLayout.addDrawerListener(aToggle);    toolbar = (Toolbar) findVIEwByID(R.ID.nav_action);    toolbar.setNavigationIcon(R.drawable.navig);    setSupportActionbar(toolbar);    aToggle.syncState();    getSupportActionbar().setdisplayHomeAsUpEnabled(true);    navigationVIEw.setItemIconTintList(null);    recyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.recycler);    recyclerAdapter = new RecyclerAdapter(getApplicationContext());    RecyclerVIEw.LayoutManager layoutManager = new GrIDLayoutManager(this, 2);    recyclerVIEw.setLayoutManager(layoutManager);    recyclerVIEw.setAdapter(recyclerAdapter);}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) {    if (aToggle.onoptionsItemSelected(item)) {        return true;    }    return super.onoptionsItemSelected(item);}}

这是我的活动的XML布局:

 <?xml version="1.0" enCoding="utf-8"?><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"    tools:context="com.example.alpit.formula2.MainActivity">    <relativeLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:layout_marginStart="0dp"        androID:orIEntation="vertical">        <androID.support.v7.Widget.RecyclerVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"            androID:ID="@+ID/recycler"            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"            androID:layout_margintop="58dp"            androID:orIEntation="vertical"></androID.support.v7.Widget.RecyclerVIEw>        <androID.support.v7.Widget.Toolbar            androID:ID="@+ID/nav_action"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:background="#EF6C00"            androID:orIEntation="vertical"            androID:theme="@style/themeOverlay.AppCompat.Dark"></androID.support.v7.Widget.Toolbar>    </relativeLayout>    <androID.support.design.Widget.NavigationVIEw        androID:ID="@+ID/nav_vIEw"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:layout_gravity="start"        androID:background="#FFA726"        app:menu="@menu/navigation_menu"        app:theme="@style/Navigationtheme">    </androID.support.design.Widget.NavigationVIEw></androID.support.v4.Widget.DrawerLayout>

这是我的菜单项:

    

    <group        androID:ID="@+ID/gp1"        androID:checkableBehavior="single">        <item            androID:ID="@+ID/nav_maths"            androID:icon="@drawable/maths"            androID:title="Maths" />        <item            androID:ID="@+ID/nav_physics"            androID:icon="@drawable/physics"            androID:title="Physics" />        <item            androID:ID="@+ID/nav_chem"            androID:icon="@drawable/chem"            androID:title="Chemistry" />        <item            androID:ID="@+ID/EEE"            androID:icon="@drawable/lightbulb"            androID:title="Electronics Electrical" />    </group>    <group        androID:ID="@+ID/gp2"        androID:checkableBehavior="single">        <item            androID:ID="@+ID/unitconversion"            androID:icon="@drawable/unitconversion"            androID:title="Unit Conversion" />        <item            androID:ID="@+ID/ScIEntist"            androID:icon="@drawable/scIEntist"            androID:title="ScIEntist" />        <item            androID:ID="@+ID/favourite"            androID:icon="@drawable/favourite"            androID:title="Favourite" />    </group>    <group        androID:ID="@+ID/gp3"        androID:checkableBehavior="single">        <item            androID:ID="@+ID/Share"            androID:icon="@drawable/share"            androID:title="Share" />        <item            androID:ID="@+ID/Rate"            androID:icon="@drawable/rate"            androID:title="Rate" />        <item            androID:ID="@+ID/ads"            androID:icon="@drawable/ad"            androID:title="Remove Ads" />        <item            androID:ID="@+ID/aboutus"            androID:icon="@drawable/aboutus"            androID:title="About Us" />    </group></menu>

问题是我无法理解如何在导航抽屉上实现onClick,因为它由我们给出的列表而不是任何ListVIEw填充.

如何在导航抽屉的项目上实现onClick?

解决方法:

您需要添加工具NavigationVIEw.OnNavigationItemSelectedListener

public class MainActivity extends AppCompatActivity implements NavigationVIEw.OnNavigationItemSelectedListener

并添加方法

@OverrIDepublic boolean onNavigationItemSelected(@NonNull MenuItem item) {    // Handle navigation vIEw item clicks here.    switch (item.getItemID()) {       case R.ID.nav_maths: {      //do somthing            break;        }      }    //close navigation drawer    mDrawerLayout.closeDrawer(GravityCompat.START);    return true;}

设置Listener的方法

private voID setNavigationVIEwListner() {    NavigationVIEw navigationVIEw = (NavigationVIEw) findVIEwByID(R.ID.nav_vIEw);    navigationVIEw.setNavigationItemSelectedListener(this);}

onCreate()中的调用方法

protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setNavigationVIEwListener()}
总结

以上是内存溢出为你收集整理的android – 导航抽屉中的onClick事件全部内容,希望文章能够帮你解决android – 导航抽屉中的onClick事件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100517.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存