• ViewPager


    先建一个带底部菜单的

    ViewPager切换 碎片

    1、activity_main.xml

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

    2、根据引导创建两个Fragment

    public class MainActivity extends AppCompatActivity implements BlankFragmentM.OnFragmentInteractionListener,PlusOneFragment.OnFragmentInteractionListener {
    
        private TextView mTextMessage;
    
        private ViewPager viewPager;
        private List<Fragment> fragmentList = new ArrayList<>();
        private BlankFragmentM fragmentM;
        private PlusOneFragment fragmentN;
        private MyPagerAdapter adapter;
    
    
        private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
                = new BottomNavigationView.OnNavigationItemSelectedListener() {
    
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        mTextMessage.setText(R.string.title_home);
                        viewPager.setCurrentItem(0);
                        return true;
                    case R.id.navigation_dashboard:
                        mTextMessage.setText(R.string.title_dashboard);
                        viewPager.setCurrentItem(1);
                        return true;
                    case R.id.navigation_notifications:
                        mTextMessage.setText(R.string.title_notifications);
                        viewPager.setCurrentItem(0);
                        return true;
                }
                return false;
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mTextMessage = (TextView) findViewById(R.id.message);
            BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
            navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    
            bindID();
            fragmentM = new BlankFragmentM();
            fragmentN = new PlusOneFragment();
            //填充数据
            fragmentList.add(fragmentM);
            fragmentList.add(fragmentN);
    
            adapter = new MyPagerAdapter(getSupportFragmentManager(),fragmentList);
            viewPager.setAdapter(adapter);
            viewPager.setCurrentItem(0);
    
        }
        private void bindID() {
            viewPager = findViewById(R.id.viewpager_a);
        }
    
        @Override
        public void onFragmentInteraction(Uri uri) {
    
        }
    
    
        public class MyPagerAdapter extends FragmentPagerAdapter {
    
            private List<Fragment> mfragmentList;
    
            public MyPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
                super(fm);
                this.mfragmentList = fragmentList;
            }
    
            @Override
            public Fragment getItem(int position) {
                return mfragmentList.get(position);
            }
    
            @Override
            public int getCount() {
                return mfragmentList.size();
            }
        }
    
    }
  • 相关阅读:
    Linux命令之用户与组管理
    ubantu命令安装banner
    Linux命令之必杀绝技Vi文本编辑的使用
    Linux命令初步了解
    C#中的一些技巧
    .net对文件的操作之对文件目录的操作
    Linux学习一些在Terminal可以用到的快捷键及Shell常用的通配符
    【初识】正则表达式
    .net对文件的操作之文件读写
    分享一次在Windows Server2012 R2中安装SQL Server2008
  • 原文地址:https://www.cnblogs.com/cnchengv/p/9865745.html
Copyright © 2020-2023  润新知