TabLayout+滑动+网络展示

来源:互联网 发布:线切割报价软件 编辑:程序博客网 时间:2024/06/03 20:10
public class MainActivity extends FragmentActivity {

    private ViewPager vp;
    private TabLayout tablayout;
    private List<Fragment> list;
    private String str[] = new String[]{"题目一", "题目二", "题目三","题目四"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
//使用适配器将ViewPager与Fragment绑定在一起
        vp = (ViewPager) findViewById(R.id.vp);
//添加数据
        listadd();
        Vpadapter adapter = new Vpadapter(getSupportFragmentManager());
        vp.setAdapter(adapter);
//将TabLayout与ViewPager绑定在一起
        tablayout = (TabLayout) findViewById(R.id.tablayout);
//绑定
        tablayout.setupWithViewPager(vp);
//字体颜色
        tablayout.setTabTextColors(getResources().getColor(R.color.black), getResources().getColor(R.color.hui));
//指示器颜色(选中状态下划线的颜色)
        tablayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.hui));
//模式
        tablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }

    private void listadd() {
        list = new ArrayList<>();
        list.add(new One());
        list.add(new Two());
        list.add(new Three());
        list.add(new Four());
    }

    class Vpadapter extends FragmentPagerAdapter {

        @Override
        public CharSequence getPageTitle(int position) {
            return str[position];
        }

        public Vpadapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}

    private void listadd() {
        list = new ArrayList<>();
        list.add(new One());
        list.add(new Two());
        list.add(new Three());
        list.add(new Four());
    }

    class Vpadapter extends FragmentPagerAdapter {

        @Override
        public CharSequence getPageTitle(int position) {
            return str[position];
        }

        public Vpadapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}





    <android.support.design.widget.TabLayout        android:id="@+id/tablayout"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        >    </android.support.design.widget.TabLayout>    <com.youth.banner.Banner        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="3"        android:id="@+id/banner"        ></com.youth.banner.Banner>        <android.support.v4.view.ViewPager         android:id="@+id/vp"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="5"            android:layout_alignParentBottom="true"            android:layout_alignParentStart="true">        </android.support.v4.view.ViewPager>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:orientation="horizontal"        >        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:layout_marginTop="20dp"            android:textSize="15sp"            android:text="技术"            />        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:layout_marginTop="20dp"                      android:textSize="15sp"            android:text="资讯"            />        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:layout_marginTop="20dp"            android:textSize="15sp"            android:text="学园"            />        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:layout_marginTop="20dp"            android:textSize="15sp"            android:text="我的"            />    </LinearLayout></LinearLayout>