NavigationDrawer Intent函数无法正常工作

时间:2017-03-18 09:12:14

标签: android android-intent navigation-drawer

我在我的应用程序中使用了NavigationDrawer模板但是Intent的功能无法正常工作。我已经在类声明中实现了导航监听器,但仍然没有使用意图导航。这是我第一次陷入困境的地方。我搜索了很多网站,但我的申请中没有发生任何事情。你能帮助我吗?我添加了一些额外的评论,请不要考虑它

public class MainActivity extends AppCompatActivity
        implements ViewPager.OnPageChangeListener {
    /**
     * Extra to add the the launch intent to specify that user comes from the notification (used to
     * show not the current month but the last one)
     */
    public static final String FROM_NOTIFICATION_EXTRA = "fromNotif";

    /**
     * List of first date of each month available
     */
    private List<Date> dates;
    /**
     * TextView that displays the name of the month
     */
    private TextView monthTitleTv;
    /**
     * Button to go the previous month
     */
    private Button previousMonthButton;
    /**
     * Button to go the next month
     */
    private Button nextMonthButton;
    /**
     * ViewPager used to display each month in a Fragment
     */
    private ViewPager pager;
    /**
     * The current {@link #pager} position
     */
    private int selectedPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.monthly_report_progress_bar);
        final View content = findViewById(R.id.monthly_report_content);
        monthTitleTv = (TextView) findViewById(R.id.monthly_report_month_title_tv);
        previousMonthButton = (Button) findViewById(R.id.monthly_report_previous_month_button);
        nextMonthButton = (Button) findViewById(R.id.monthly_report_next_month_button);
        pager = (ViewPager) findViewById(R.id.monthly_report_view_pager);

        previousMonthButton.setText("<");
        nextMonthButton.setText(">");
        previousMonthButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectedPosition > 0) {
                    selectPagerItem(selectedPosition - 1, true);
                }
            }
        });
        nextMonthButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectedPosition < dates.size() - 1) {
                    selectPagerItem(selectedPosition + 1, true);
                }
            }
        });
        UIHelper.removeButtonBorder(previousMonthButton);
        UIHelper.removeButtonBorder(nextMonthButton);
        // Load list of monthly asynchronously since it can take time
        new AsyncTask<Void, Void, List<Date>>() {
            @Override
            protected List<Date> doInBackground(Void... params) {
                return DateHelper.getListOfMonthsAvailableForUser(MainActivity.this);
            }

            @Override
            protected void onPostExecute(List<Date> dates) {
                if (isFinishing()) {
                    return;
                }

                MainActivity.this.dates = dates;

                configureViewPager();

                progressBar.setVisibility(View.GONE);
                content.setVisibility(View.VISIBLE);
            }
        }.execute();
    }

    private void configureViewPager() {
        pager.setOffscreenPageLimit(0);
        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return new MonthlyReportFragment(dates.get(position));
            }

            @Override
            public int getCount() {
                return dates.size();
            }
        });
        pager.addOnPageChangeListener((ViewPager.OnPageChangeListener) this);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = item.getItemId();
                switch (id) {
                    case R.id.nav_camera:
                        Intent i = new Intent(MainActivity.this, Monthly_ExpenseEdit_activity.class);
                        startActivity(i);
                        break;
                    case R.id.nav_gallery:
                        Intent i1 = new Intent(MainActivity.this, ExpenseEditActivity.class);
                        startActivity(i1);
                        break;
                }
                return false;
            }
        });
    }

 /**
     * Extra to add the the launch intent to specify that user comes from the notification (used to
     * show not the current month but the last one)
     */



    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }




    private void selectPagerItem(int position, boolean animate)
    {
        pager.setCurrentItem(position, animate);
        onPageSelected(position);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }
 /**
     * Extra to add the the launch intent to specify that user comes from the notification (used to
     * show not the current month but the last one)
     */
    @Override
    public void onPageSelected(int position)
    {
        selectedPosition = position;

        Date date = dates.get(position);

        monthTitleTv.setText(DateHelper.getMonthTitle(this, date));

        // Last and first available month
        boolean last = position == dates.size() - 1;
        boolean first = position == 0;

/ **          *额外添加启动意图以指定用户来自通知(用于          *显示不是当前月份而是最后一个月)          * /

        nextMonthButton.setEnabled(!last);
        nextMonthButton.setTextColor(ContextCompat.getColor(this, last ? R.color.monthly_report_disabled_month_button : android.R.color.white));
        previousMonthButton.setEnabled(!first);
        previousMonthButton.setTextColor(ContextCompat.getColor(this, first ? R.color.monthly_report_disabled_month_button : android.R.color.white));
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


}

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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".view.MonthlyReportActivity">

        <ProgressBar android:id="@+id/monthly_report_progress_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:indeterminate="true" />

        <LinearLayout android:id="@+id/monthly_report_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="47dp"
                android:orientation="horizontal"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:gravity="center_vertical"
                android:background="@color/primary_dark">
                <Button
                    android:id="@+id/test"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                />

                <Button android:id="@+id/monthly_report_previous_month_button"
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/white"
                    android:textSize="24dp"
                    android:background="@drawable/calendar_month_switcher_button_drawable" />

                <TextView android:id="@+id/monthly_report_month_title_tv"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:textSize="21dp"
                    android:textColor="@android:color/white"
                    android:textAllCaps="true"
                    android:gravity="center" />

                <Button android:id="@+id/monthly_report_next_month_button"
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/white"
                    android:textSize="24dp"
                    android:background="@drawable/calendar_month_switcher_button_drawable" />

            </LinearLayout>

            <android.support.v4.view.ViewPager
                android:id="@+id/monthly_report_view_pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </LinearLayout>



    </FrameLayout>

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

1 个答案:

答案 0 :(得分:0)

onNavigationItemSelected()方法中返回true而不是false

还要确保导航抽屉菜单中存在ID。

 app:menu="@menu/menu_navigation" 

使用此代码:

    @Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.nav_camera:
            Intent i = new Intent(MainActivity.this, Monthly_ExpenseEdit_activity.class);
            startActivity(i);
            break;
        case R.id.nav_gallery:
            Intent i1 = new Intent(MainActivity.this, ExpenseEditActivity.class);
            startActivity(i1);
            break;
    }
    return true;   //change here
}