• 项目开发第二天


    今天的任务是设置fragment控件实现多页面滑动。

    共设置三个控件:概要、支出、收入。

    源代码:

    三个控件menu:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:id="@+id/navigation_home"
            android:icon="@drawable/tab_icon1"
            android:title="@string/title_summary" />
    
        <item
            android:id="@+id/navigation_dashboard"
            android:icon="@drawable/tab_icon2"
            android:title="@string/title_income" />
    
        <item
            android:id="@+id/navigation_notifications"
            android:icon="@drawable/tab_icon3"
            android:title="@string/title_outlay" />
    
    </menu>
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".MainActivity"
        tools:showIn="@layout/app_bar_main">
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/navigation"
            android:layout_marginBottom="-80dp" />
    
        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            android:layout_alignParentBottom="true"
            app:menu="@menu/navigation" />
    
    </RelativeLayout>
    package net.hnjdzy.tinyaccount;
    
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.design.widget.BottomNavigationView;
    import android.support.v4.view.ViewPager;
    import android.view.View;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    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.MenuItem;
    import android.widget.TextView;
    
    import net.hnjdzy.tinyaccount.activity.AboutActivity;
    import net.hnjdzy.tinyaccount.activity.HelpActivity;
    import net.hnjdzy.tinyaccount.activity.RegisterActivity;
    import net.hnjdzy.tinyaccount.activity.SettingActivity;
    import net.hnjdzy.tinyaccount.adapter.ViewPagerAdapter;
    import net.hnjdzy.tinyaccount.fragment.IncomeFragment;
    import net.hnjdzy.tinyaccount.fragment.OutlayFragment;
    import net.hnjdzy.tinyaccount.fragment.SummaryFragment;
    
    /**
     * 主界面,导航,测边菜单
     * @author androiddev@163.com,hnjdzy
     */
    public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {
    
        private ViewPager viewPager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            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.addDrawerListener(toggle);
            toggle.syncState();
    
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
    
            BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
            navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    
            viewPager = (ViewPager) findViewById(R.id.viewpager);
            setupViewPager(viewPager);
    
    
    
    
        }
    
        @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;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                Intent intent = new Intent(this, RegisterActivity.class);
                startActivity(intent);
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        //左侧边菜单栏的事件处理
        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();
    
            if (id == R.id.nav_manage) {
                //设置界面,类别维护
                Intent intent = new Intent(this, SettingActivity.class);
                startActivity(intent);
            } else if (id == R.id.nav_share) {
                // 分享
            } else if (id == R.id.nav_send) {
                //报告
            }else if (id == R.id.nav_help) {
                //帮助界面
                Intent intent = new Intent(this, HelpActivity.class);
                startActivity(intent);
            }
            else if (id == R.id.nav_about) {
                //关于软件
                Intent intent = new Intent(this, AboutActivity.class);
                startActivity(intent);
            }
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    
        // 底部导航栏的事件处理
        private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
                = new BottomNavigationView.OnNavigationItemSelectedListener() {
    
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        viewPager.setCurrentItem(0);
                        return true;
                    case R.id.navigation_dashboard:
                        viewPager.setCurrentItem(1);
                        return true;
                    case R.id.navigation_notifications:
                        viewPager.setCurrentItem(2);
                        return true;
                    default:break;
                }
                return false;
            }
        };
    
        //创建底部导航对应的Fragment
        private void setupViewPager(ViewPager viewPager) {
            ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
            SummaryFragment f1 = new SummaryFragment();
            IncomeFragment f2 = new IncomeFragment();
            OutlayFragment f3 = new OutlayFragment();
            adapter.addFragment(f1);
            adapter.addFragment(f2);
            adapter.addFragment(f3);
            viewPager.setAdapter(adapter);
        }
    }
  • 相关阅读:
    模板之导入include
    模板之继承
    jQuery ajax简单用法
    Django 路由系统URL
    Django 的简单使用
    python selenium web自动化的三种等待的区别
    Django的数据库介绍
    Django 安装与介绍
    插件轮播
    bootstrap 简单使用(实现图标和按钮下拉)
  • 原文地址:https://www.cnblogs.com/w669399221/p/12319376.html
Copyright © 2020-2023  润新知