繁体   English   中英

我在项目单击侦听器上的导航抽屉不起作用

[英]My Navigation Drawer on item click listener not worked

我想在它显示的主要活动中显示导航抽屉导航抽屉,但是当我在点击监听器上使用它时它无法工作。 我想更改导航抽屉中单击侦听器的片段。 我尝试了很多,但它无法工作。还请指导我如何在不同的片段中隐藏导航抽屉,并指导我如何在自定义设计按钮单击时再次打开导航抽屉。

package com.sprintsolutions.palmtree.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.material.navigation.NavigationView;
import com.sprintsolutions.palmtree.R;
import com.sprintsolutions.palmtree.fragment.ForgetPasswordFragment;
import com.sprintsolutions.palmtree.fragment.HomeFragment;
import com.sprintsolutions.palmtree.fragment.LoginStartFragment;

public class FragmentContainerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;

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



        final Fragment menuPractiseFragment=new HomeFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer,
                menuPractiseFragment,menuPractiseFragment.getClass().getSimpleName())
                .addToBackStack(null).commit();

        mDrawerLayout=(DrawerLayout)findViewById(R.id.activity_main);
        mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

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

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int id = menuItem.getItemId();

                if (id == R.id.db) {
                    Toast.makeText(FragmentContainerActivity.this, "Dashboard Activity", Toast.LENGTH_SHORT).show();
                } else if (id == R.id.search) {

                    Toast.makeText(FragmentContainerActivity.this, "Search Activity", Toast.LENGTH_SHORT).show();
                } else if (id == R.id.event) {

                    Toast.makeText(FragmentContainerActivity.this, "Event aCTIVITY", Toast.LENGTH_SHORT).show();
                }

                mDrawerLayout.closeDrawer(GravityCompat.START);
                return true;
            }
        });

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mToggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {


        return false;
    }

}

你已经在FragmentContainerActivity中实现NavigationView.OnNavigationItemSelectedListener所以使用覆盖方法,不需要使用

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()

         public class FragmentContainerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

        private DrawerLayout mDrawerLayout;
        private ActionBarDrawerToggle mToggle;

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



            final Fragment menuPractiseFragment=new HomeFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer,
                    menuPractiseFragment,menuPractiseFragment.getClass().getSimpleName())
                    .addToBackStack(null).commit();

            mDrawerLayout=(DrawerLayout)findViewById(R.id.activity_main);
            mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
            mDrawerLayout.addDrawerListener(mToggle);
            mToggle.syncState();
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

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

        }


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (mToggle.onOptionsItemSelected(item)){
                return true;
            }
            return super.onOptionsItemSelected(item);
        }


        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
         int id = menuItem.getItemId();

                    if (id == R.id.db) {
                        Toast.makeText(FragmentContainerActivity.this, "Dashboard Activity", Toast.LENGTH_SHORT).show();
                    } else if (id == R.id.search) {

                        Toast.makeText(FragmentContainerActivity.this, "Search Activity", Toast.LENGTH_SHORT).show();
                    } else if (id == R.id.event) {

                        Toast.makeText(FragmentContainerActivity.this, "Event aCTIVITY", Toast.LENGTH_SHORT).show();
                    }

                    mDrawerLayout.closeDrawer(GravityCompat.START);
                    return true;
    }

}

暂无
暂无

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

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