项目单击菜单图形不起作用

时间:2018-05-21 12:40:13

标签: java android

我正在尝试制作一个导航抽屉,当用户想要一个项目时,它会打开一个新的活动。

我不知道怎么做?

我的抽屉中有5个项目,我希望每个项目都使用intent而不是片段来打开不同的活动。

当我点击任意项目时,我需要使用意图进行其他活动

我的代码如下

public class menu extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

TextView tt1 , txtnom , txtadress;
Typeface ty;
ViewPager viewp;
TabLayout tab;
ImageView img;
private DrawerLayout mdrawer;
private ActionBarDrawerToggle mtoggle;
private int[] tabIcons = {
        R.drawable.ic_tablayout_tache,
        R.drawable.ic_ic_tablayout_rendez,
        R.drawable.ic_tablayout_eve,
        R.drawable.ic_projettab
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    Toolbar tol = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(tol);
    getSupportActionBar().setTitle("");
    tt1 = (TextView) findViewById(R.id.textbar);
    ty = Typeface.createFromAsset(getAssets(), "fonts/BALOOBHAI-REGULAR.TTF");
    tt1.setTypeface(ty);
    mdrawer = (DrawerLayout) findViewById(R.id.drawerlayout);
    mtoggle = new ActionBarDrawerToggle(this, mdrawer, R.string.ouvrir, R.string.fermer);
    mdrawer.addDrawerListener(mtoggle);
    mtoggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    viewp = (ViewPager) findViewById(R.id.viewpage);
    tab = (TabLayout) findViewById(R.id.tablayout);
    setupViewPager(viewp);
    tab.setupWithViewPager(viewp);
    setupTabIcons();
    NavigationView navigationView = (NavigationView)findViewById(R.id.navview);
    View header = LayoutInflater.from(this).inflate(R.layout.header_menu , null);
    navigationView.addHeaderView(header);
    navigationView.setNavigationItemSelectedListener(this);
    img = (ImageView)header.findViewById(R.id.pic);
    txtnom = (TextView)header.findViewById(R.id.textnom);
    txtadress = (TextView)header.findViewById(R.id.textadr);
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null){
        Picasso.with(getApplicationContext()).load(user.getPhotoUrl()).transform(new CircleTransform()).into(img);
        String name = user.getDisplayName();
        String email = user.getEmail();
        txtnom.setText(name);
        txtadress.setText(email);
    } else {
        gopagelogin();
    }
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mtoggle.onOptionsItemSelected(item)) {
        return true;
    }
    int id = item.getItemId();
        if ( id == R.id.dec) {
            alertmsg();
    }
    return super.onOptionsItemSelected(item);
}
public void runThis(View v) {
    Intent ii = new Intent(menu.this, partietache.class);
    startActivity(ii);

}

public void Rendezvous(View v) {
    Intent intent = new Intent(menu.this, partierendezvous.class);
    startActivity(intent);
}
public void Evenement(View v) {
    Intent intent = new Intent(menu.this, partieevenement.class);
    startActivity(intent);
}
public void Projet(View v) {
    Intent intent = new Intent(menu.this, partieprojet.class);
    startActivity(intent);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new Tachefragment(), "Tâches");
    adapter.addFragment(new RendezvousFragment(), "Rendez-vous");
    adapter.addFragment(new Evenementfragment(), "Événements");
    adapter.addFragment(new ProjetFragment(), "Projets");
    viewPager.setAdapter(adapter);
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.para) {
        Intent i = new Intent(menu.this, settingmenu.class);
        startActivity(i);
    } else if (id == R.id.prof) {
        Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
    }

    mdrawer.closeDrawer(GravityCompat.START);
    return true;

}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String taches) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(taches);
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
private void setupTabIcons() {
    tab.getTabAt(0).setIcon(tabIcons[0]);
    tab.getTabAt(1).setIcon(tabIcons[1]);
    tab.getTabAt(2).setIcon(tabIcons[2]);
    tab.getTabAt(3).setIcon(tabIcons[3]);
}

private void gopagelogin() {
    Intent ii = new Intent(this , login.class);
    ii.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(ii);
}
private void alertmsg() {
    AlertDialog.Builder alert = new AlertDialog.Builder(menu.this);
    alert.setTitle("Déconnexion ?");
    alert.setMessage("voulez-vous déconnecter?");
    alert.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
           FirebaseAuth.getInstance().signOut();
            gopagelogin();
        }
    });
    alert.setNegativeButton("Non", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.cancel();
        }
    });
    alert.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.deconnexion , menu);
    return true;
}}

我的xml代码:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerlayout"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include
        android:id="@+id/toolbar"
        layout="@layout/mytoolbar" />
    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/AppTabLayout"
        app:tabTextAppearance="@style/AppTabTextAppearance"
        app:tabMode="fixed" />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tablayout" />
</LinearLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/navview"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_gravity="start"
    android:background="#ededed"
    app:itemIconTint="#000000"
    app:itemTextColor="#000000"
    app:menu="@menu/menudeb">
</android.support.design.widget.NavigationView>
<RelativeLayout xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignEnd="@+id/material_design_android_floating_action_menu"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/material_design_android_floating_action_menu">
    <TextView
        android:id="@+id/textbar"
        android:layout_width="125dp"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:layout_marginBottom="100dp"
        android:layout_marginLeft="125dp"
        android:layout_marginTop="11dp"
        android:gravity="center"
        android:text="WACKTY"
        android:textColor="#01b698"
        android:textSize="24dp" />
    <RelativeLayout
        xmlns:fab="http://schemas.android.com/apk/res-auto"
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textmenu">

        <com.github.clans.fab.FloatingActionMenu
            android:id="@+id/menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="20dp"
            android:layout_marginEnd="12dp"
            android:layout_marginRight="12dp"
            fab:menu_animationDelayPerItem="55"
            fab:menu_backgroundColor="@android:color/transparent"
            fab:menu_buttonSpacing="0dp"
            fab:menu_colorNormal="#ffffff"
            fab:menu_colorPressed="#ffffff"
            fab:menu_colorRipple="#99d4d4d4"
            fab:menu_fab_size="normal"
            fab:menu_icon="@drawable/ic_action_plus"
            fab:menu_labels_colorNormal="#c4c0c0"
            fab:menu_labels_colorPressed="#444"
            fab:menu_labels_colorRipple="#66efecec"
            fab:menu_labels_cornerRadius="0dp"
            fab:menu_labels_ellipsize="none"
            fab:menu_labels_hideAnimation="@anim/fab_slide_out_to_right"
            fab:menu_labels_margin="0dp"
            fab:menu_labels_maxLines="-1"
            fab:menu_labels_padding="1dp"
            fab:menu_labels_position="left"
            fab:menu_labels_showAnimation="@anim/fab_slide_in_from_right"
            fab:menu_labels_showShadow="false"
            fab:menu_labels_singleLine="false"
            fab:menu_labels_textColor="#000000"
            fab:menu_labels_textSize="14sp"
            fab:menu_openDirection="up">
            <com.github.clans.fab.FloatingActionButton
                android:id="@+id/projet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_projeticon"
                fab:fab_colorNormal="#ffffff"
                fab:fab_colorPressed="#444"
                fab:fab_label="Projet"
                android:onClick="Projet"
                fab:fab_size="mini" />

            <com.github.clans.fab.FloatingActionButton
                android:id="@+id/événement"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_eventicon"
                fab:fab_colorNormal="#ffffff"
                fab:fab_colorPressed="#444"
                fab:fab_label="Événement"
                android:onClick="Evenement"
                fab:fab_size="mini" />

            <com.github.clans.fab.FloatingActionButton
                android:id="@+id/rendezvous"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Rendezvous"
                android:src="@drawable/ic_action_rendez"
                fab:fab_colorNormal="#ffffff"
                fab:fab_colorPressed="#444"
                fab:fab_label="Rendez-Vous"
                fab:fab_size="mini" />

            <com.github.clans.fab.FloatingActionButton
                android:id="@+id/tache"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="runThis"
                android:src="@drawable/ic_action_task"
                fab:fab_colorNormal="#ffffff"
                fab:fab_colorPressed="#444"
                fab:fab_label="Tâches"
                fab:fab_size="mini" />
        </com.github.clans.fab.FloatingActionMenu>
    </RelativeLayout>
</RelativeLayout>

菜单xml:

<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
    android:id="@+id/prof"
    android:icon="@drawable/ic_user"
    android:title="Profile"
    />
<item
    android:id="@+id/proj"
    android:icon="@drawable/ic_project"
    android:title="Projets"/>
<item
    android:id="@+id/para"
    android:icon="@drawable/ic_action_set"
    android:title="Parametres" />
</group>
<item android:title="Compte">
    <menu>
        <item
            android:id="@+id/dec"
            android:icon="@drawable/ic_action_set"
            android:title="Déconnexion"/>
    </menu>
</item>
</menu>

1 个答案:

答案 0 :(得分:0)

在你的overNidedNavigationItemSelected(MenuItem item)方法中,你必须得到getItemId,并且你必须通过意图打开所选项目。

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.ItemId) {
        Intent i = new Intent(YourActivityName.this, OtherActivity.class);
        startActivity(i);
    } 
    mdrawer.closeDrawer(GravityCompat.START);
    return true;

}


<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerlayout"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include
        android:id="@+id/toolbar"
        layout="@layout/mytoolbar" />
    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/AppTabLayout"
        app:tabTextAppearance="@style/AppTabTextAppearance"
        app:tabMode="fixed" />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tablayout" />
</LinearLayout>

<RelativeLayout xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignEnd="@+id/material_design_android_floating_action_menu"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/material_design_android_floating_action_menu">
    <TextView
        android:id="@+id/textbar"
        android:layout_width="125dp"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:layout_marginBottom="100dp"
        android:layout_marginLeft="125dp"
        android:layout_marginTop="11dp"
        android:gravity="center"
        android:text="WACKTY"
        android:textColor="#01b698"
        android:textSize="24dp" />
    <RelativeLayout
        xmlns:fab="http://schemas.android.com/apk/res-auto"
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textmenu">

        <com.github.clans.fab.FloatingActionMenu
            android:id="@+id/menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="20dp"
            android:layout_marginEnd="12dp"
            android:layout_marginRight="12dp"
            fab:menu_animationDelayPerItem="55"
            fab:menu_backgroundColor="@android:color/transparent"
            fab:menu_buttonSpacing="0dp"
            fab:menu_colorNormal="#ffffff"
            fab:menu_colorPressed="#ffffff"
            fab:menu_colorRipple="#99d4d4d4"
            fab:menu_fab_size="normal"
            fab:menu_icon="@drawable/ic_action_plus"
            fab:menu_labels_colorNormal="#c4c0c0"
            fab:menu_labels_colorPressed="#444"
            fab:menu_labels_colorRipple="#66efecec"
            fab:menu_labels_cornerRadius="0dp"
            fab:menu_labels_ellipsize="none"
            fab:menu_labels_hideAnimation="@anim/fab_slide_out_to_right"
            fab:menu_labels_margin="0dp"
            fab:menu_labels_maxLines="-1"
            fab:menu_labels_padding="1dp"
            fab:menu_labels_position="left"
            fab:menu_labels_showAnimation="@anim/fab_slide_in_from_right"
            fab:menu_labels_showShadow="false"
            fab:menu_labels_singleLine="false"
            fab:menu_labels_textColor="#000000"
            fab:menu_labels_textSize="14sp"
            fab:menu_openDirection="up">
            <com.github.clans.fab.FloatingActionButton
                android:id="@+id/projet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_projeticon"
                fab:fab_colorNormal="#ffffff"
                fab:fab_colorPressed="#444"
                fab:fab_label="Projet"
                android:onClick="Projet"
                fab:fab_size="mini" />

            <com.github.clans.fab.FloatingActionButton
                android:id="@+id/événement"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_eventicon"
                fab:fab_colorNormal="#ffffff"
                fab:fab_colorPressed="#444"
                fab:fab_label="Événement"
                android:onClick="Evenement"
                fab:fab_size="mini" />

            <com.github.clans.fab.FloatingActionButton
                android:id="@+id/rendezvous"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Rendezvous"
                android:src="@drawable/ic_action_rendez"
                fab:fab_colorNormal="#ffffff"
                fab:fab_colorPressed="#444"
                fab:fab_label="Rendez-Vous"
                fab:fab_size="mini" />

            <com.github.clans.fab.FloatingActionButton
                android:id="@+id/tache"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="runThis"
                android:src="@drawable/ic_action_task"
                fab:fab_colorNormal="#ffffff"
                fab:fab_colorPressed="#444"
                fab:fab_label="Tâches"
                fab:fab_size="mini" />
        </com.github.clans.fab.FloatingActionMenu>
    </RelativeLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/navview"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_gravity="start"
    android:background="#ededed"
    app:itemIconTint="#000000"
    app:itemTextColor="#000000"
    app:menu="@menu/menudeb">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>