Android导航抽屉显示两次项目

时间:2016-08-19 05:10:00

标签: android navigation-drawer android-navigationview

我正在开发一个应用程序,我必须在主页中显示导航抽屉。我正在使用NavigationView,我将指出它应该填充哪个菜单。现在我面临的问题是,它显示项目两次。下面的代码将会告诉你到目前为止我做了什么

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.mylaw.rkme3.mylawproject.fragments.HomeFragment;
import com.mylaw.rkme3.mylawproject.fragments.VideoHomeFragment;
import com.mylaw.rkme3.mylawproject.interfaces.FragmentInterface;
import com.mylaw.rkme3.mylawproject.utils.Constants;


/**
 * Created by Lincy on 7/4/2016.
 **/
public class MainActivity extends AppCompatActivity implements FragmentInterface {


    private static final String TAG = "MainActivity";
    private Toolbar mToolBar;
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        displayScreens();
    }

    private void initView() {
        mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
        mToolBar = (Toolbar) findViewById(R.id.toolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        setUpNavigationDrawer();
        mNavigationView.inflateMenu(R.menu.drawer);

    }

    private void setUpNavigationDrawer() {
        setSupportActionBar(mToolBar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,
                mDrawerLayout, mToolBar,
                R.string.openDrawer,
                R.string.closeDrawer) {

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

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };

        mDrawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                if (item.isChecked())
                    item.setChecked(false);
                else item.setChecked(true);

                mDrawerLayout.closeDrawers();

                switch (item.getItemId()) {
                    case R.id.sub_item_in_progress:
                        goToInProgress();
                        return true;
                    case R.id.sub_item_completed:
                        goToCompleted();
                        return true;
                    case R.id.sub_lapsed:
                        goToLapsed();
                        return true;
                    default:
                        Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
                        return true;
                }
            }
        });
    }

    private void goToLapsed() {
        Intent courseLapsedIntent = new Intent(MainActivity.this, CourseProgressActivity.class);
        Bundle extras = new Bundle();
        extras.putString(Constants.BundleParameters.FRAGMENT_NAME, "Lapsed");
        courseLapsedIntent.putExtras(extras);
        startActivity(courseLapsedIntent);
    }

    private void goToCompleted() {
        Intent courseCompletedIntent = new Intent(MainActivity.this, CourseProgressActivity.class);
        Bundle extras = new Bundle();
        extras.putString(Constants.BundleParameters.FRAGMENT_NAME, "Completed");
        courseCompletedIntent.putExtras(extras);
        startActivity(courseCompletedIntent);
    }

    private void goToInProgress() {
        Intent courseProgressIntent = new Intent(MainActivity.this, CourseProgressActivity.class);
        Bundle extras = new Bundle();
        extras.putString(Constants.BundleParameters.FRAGMENT_NAME, "InProgress");
        courseProgressIntent.putExtras(extras);
        startActivity(courseProgressIntent);
    }

    private void displayScreens() {
        this.action(Constants.FragmentInterfaceConstants.ACTION_HOME, null);
    }

    @Override
    public void action(int tag, Bundle args) {
        switch (tag) {
            case Constants.FragmentInterfaceConstants.ACTION_HOME:
                handleHomeScreen();
                break;
            case Constants.FragmentInterfaceConstants.ACTION_VIDEO_HOME:
                handleVideoHomeScreen();
                break;
            default:
                break;
        }
    }

    @Override
    public void showActionBar(boolean show) {

    }

    @Override
    public void showHomeButton(boolean show) {

    }

    @Override
    public void setActionBarTitle(String title) {

    }


    private void handleHomeScreen() {
        HomeFragment homeFragment = new HomeFragment();
        addFragment(homeFragment, HomeFragment.TAG);
    }

    private void handleVideoHomeScreen() {
        VideoHomeFragment videoHomeFragment = new VideoHomeFragment();
        addFragment(videoHomeFragment, HomeFragment.TAG);
    }


    private void addFragment(Fragment fragment, String tag) {
        FragmentTransaction ft =
                getSupportFragmentManager()
                        .beginTransaction();
        ft.replace(R.id.mainFrame, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.addToBackStack(tag);
        ft.commitAllowingStateLoss();
    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
            finish();
        }else {
            getSupportFragmentManager().popBackStack();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.item_Register:
                Intent registerIntent = new Intent(MainActivity.this, TransparentActionBarActivity.class);
                Bundle extras = new Bundle();
                extras.putString("fragName", "Register");
                registerIntent.putExtras(extras);
                startActivity(registerIntent);
                break;
            case R.id.item_search:
                Intent searchIntent = new Intent(MainActivity.this, TransparentActionBarActivity.class);
                Bundle searchExtras = new Bundle();
                searchExtras.putString("fragName", "Search");
                searchIntent.putExtras(searchExtras);
                startActivity(searchIntent);
                break;
            case R.id.item_login:
                Intent loginIntent = new Intent(MainActivity.this, TransparentActionBarActivity.class);
                Bundle loginExtras = new Bundle();
                loginExtras.putString("fragName", "Login");
                loginIntent.putExtras(loginExtras);
                startActivity(loginIntent);
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

0 个答案:

没有答案