如何实现Fragment的滑动?

1.main.xml

  

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout_mian"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/colorPrimaryDark"
        app:tabSelectedTextColor="@color/colorAccent"
        app:tabTextColor="@color/colorWhite"
        app:tabMode="fixed"
        app:tabIndicatorColor="@color/colorAccent">

    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager_main"
        android:layout_below="@+id/tab_layout_mian"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        >
    </android.support.v4.view.ViewPager>
</RelativeLayout>

2.main.activity

@Bind(R.id.view_pager_main)
ViewPager viewPager;// 声明一个viewpager对象
@Bind(R.id.tab_layout_mian)
TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_call);
    ButterKnife.bind(this);
    initView();
}
/**
 * 添加布局ID
 */
private void initView() {
    viewPager.setAdapter(adapter);
    tabLayout.setTabsFromPagerAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
    //0,第一位开始滑动,1第二位开始滑动
    int currentIndex = getIntent().getIntExtra("current", 1);
    viewPager.setCurrentItem(currentIndex);
}

/**
 * fragment 滑动导航适配
 */
public class PhoneFragemtAdapter extends FragmentPagerAdapter {
     //设置导航标题
    String[] titles = new String[]{getString(R.string.text_call_re),getString(R.string.text_encryption_telephone)};

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

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

    @Override
    public Fragment getItem(int position) {
        switch (position) { //两个碎片fragment
            case 0:
                return CallRecordsFragment.newFragment(); 
            case 1:
                return PhoneFragment.newFragment();

        }
        return PhoneFragment.newFragment();
    }

    @Override
    public int getCount() {
        return titles.length;
    }
}

public class CallRecordsFragment extends Fragment{

    public static CallRecordsFragment newFragment() {
        CallRecordsFragment fragment = new CallRecordsFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.call_records_fragment, container, false);
    }
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值