• TabLayout


    效果图:

    标题和fragment联动效果已经封装好了,非常方便

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:tabMode="scrollable"></android.support.design.widget.TabLayout>
    
    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tabs"></android.support.v4.view.ViewPager>

    配置要特别注意

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.1.1'
        compile 'com.android.support:design:23.1.1'
        compile 'com.android.support:support-v4:23.1.1'
    }
    public class MyFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.myfragment,null);
            return view;
        }
    }
    public class MyAdapter extends FragmentPagerAdapter {
    
        private List<String> titles;
        private List<Fragment> fragments;
    
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }
    
        public MyAdapter(FragmentManager fm, List<String> titles, List<Fragment> fragments) {
            super(fm);
            this.titles = titles;
            this.fragments = fragments;
        }
    
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }
    
        @Override
        public int getCount() {
            return titles.size();
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }
    public class MainActivity extends AppCompatActivity {
    
        private TabLayout tabLayout;
        private ViewPager viewPager;
        private List<String> titles;
        private List<Fragment> fragments;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tabLayout = (TabLayout) findViewById(R.id.tabs);
            viewPager = (ViewPager) findViewById(R.id.vp);
            initTitles();
            initFragments();
            MyAdapter adapter = new MyAdapter(getSupportFragmentManager(), titles, fragments);
            viewPager.setAdapter(adapter);
            tabLayout.setupWithViewPager(viewPager);
        }
    
        private void initTitles() {
            titles = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                titles.add("标题" + i);
            }
        }
    
        private void initFragments() {
            fragments = new ArrayList<>();
            for (int i = 0; i < titles.size(); i++) {
                fragments.add(new MyFragment());
            }
        }
    
    }
  • 相关阅读:
    bzoj:2423: [HAOI2010]最长公共子序列
    bzoj:3994:vijos1949: [SDOI2015]约数个数和
    bzoj4332;vijos1955:JSOI2012 分零食
    bzoj:1726: [Usaco2006 Nov]Roadblocks第二短路
    bzoj:1724: [Usaco2006 Nov]Fence Repair 切割木板
    bzoj:1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏
    bzoj:1230: [Usaco2008 Nov]lites 开关灯
    bzoj:1941: [Sdoi2010]Hide and Seek
    bzoj:1699;poj 3264: [Usaco2007 Jan]Balanced Lineup排队
    bzoj 1705;poj 3612:[Usaco2007 Nov]Telephone Wire 架设电话线
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/5543585.html
Copyright © 2020-2023  润新知