滑动界面

TabLayout + Fragment

效果

向左滑动切换Fragment

代码

layout:ac_sliding

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#155e10"
        app:tabIndicatorColor="#ffffff"
        app:tabSelectedTextColor="#999999"
        app:tabTextColor="#ffffff"/>

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

    </android.support.v4.view.ViewPager>

</LinearLayout>

Activity:SlidingActivity

/**
 * Created by kangfh on 2016/9/12.
 * 滑动界面
 */
public class SlidingActivity extends BaseActivity {
    private TabLayout tablayout;
    private ViewPager viewpager;
    private List<String> list_title;
    private List<Fragment> list_fragment;
    private TabLayoutAdapter tabLayoutAdapter ;

    private void assignViews() {
        tablayout = (TabLayout) findViewById(R.id.tablayout);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
    }


    @Override
    public void initViews() {
        setContentView(R.layout.ac_sliding);
        assignViews();
    }

    @Override
    public void initData() {
        list_title = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list_title.add("第" + i + "个");
        }

        list_fragment = new ArrayList<>();
        for (int i = 0; i < list_title.size(); i++) {
            SlidingFragment slidingFragment = new SlidingFragment();
            Bundle bundle = new Bundle();
            bundle.putString("content", "第" + i + "个界面");
            slidingFragment.setArguments(bundle);
            list_fragment.add(slidingFragment);
        }
        //TabLayout滑动模式
        tablayout.setTabMode(TabLayout.MODE_SCROLLABLE);//滑动
        for(int i=0;i<list_title.size();i++){
            tablayout.addTab(tablayout.newTab().setText(list_title.get(i)));
        }
        tabLayoutAdapter=new TabLayoutAdapter(getSupportFragmentManager(),list_title,list_fragment) ;
        viewpager.setAdapter(tabLayoutAdapter);
        tablayout.setupWithViewPager(viewpager);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值