viewpager+fragement选项卡绘制

此选项卡可以配合fragment显示数据 就是之前做的那个

main_activity.java

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabLayout tableLayout = findViewById(R.id.tabs);

        ViewPager viewPager = findViewById(R.id.vp_main_show);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tableLayout));
        tableLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
        SectionsPagerAdapter PagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(PagerAdapter);


    }

    public static class DummyFragment extends Fragment {
        private static final String ARG_SECTION_NUMBER = "section_number";

        //方法用来返回一个DummyFragment实例
        public static DummyFragment newInstance(int sectionNumer) {
            DummyFragment fragment = new DummyFragment();
            //定义Bundle用于向DummyFragment传入参数
            Bundle agrs = new Bundle();
            agrs.putInt(ARG_SECTION_NUMBER, sectionNumer);
            fragment.setArguments(agrs);
            return fragment;
        }

        //重写该方法用于生成该Fragment的组件
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            TextView textView = rootView.findViewById(R.id.section_label);
            textView.setText("页面" + getArguments().getInt(ARG_SECTION_NUMBER, 0));
            return rootView;
        }
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int i) {
            return DummyFragment.newInstance(i);
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

}

main-xml





    

        

        

        

    

    


fragment-xml



    


配置dependencies 在bulid.gradle里面

implementation 'com.android.support:design:28.0.0'

同时将targetSdkVersion --defaultConfig {     compileSdkVersion--android { 改为28

最终实现

viewpager+fragement选项卡绘制_第1张图片

这里再挂一个相关链接

https://blog.csdn.net/oqqyyyy1/article/details/54310794

 

你可能感兴趣的:(安卓基础学习,android)