android 学习之Fragment+ViewPager实现页面左右滑动标签页

ViewPager 结合Fragment实现一个Activity里包含多个可滑动的标签页,每个标签页可以有独立的布局及响应。

如下所示。


我们可以借助TabLayout来实现顶部导航。Activity布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

<--!自定义标题栏!-->
    <include layout="@layout/layout_ordermanagertitles" />
<--!自定义TabLayout布局!-->
    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="@color/colorTitle"
        app:tabSelectedTextColor="@color/black"
        app:tabTextColor="@color/black" />
<--ViewPager 实现滑动-->
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Activity  Java文件,在该文件中定义适配器,实现滑动和布局.

import android.content.Intent;
import android.os.Bundle;
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 com.glc.dianniuapp.R;
import com.glc.dianniuapp.fragments.FragmentDeal;
import com.glc.dianniuapp.fragments.FragmentHistory;
import com.glc.dianniuapp.fragments.FragmentRunning;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Leiqi on 2015/11/27.
 */
public class OrderManagerAcitivity extends FragmentActivity implements ViewPager.OnPageChangeListener {

    @Bind(R.id.viewpager)
    ViewPager mViewPager;
    @Bind(R.id.tablayout)
    TabLayout mTabLayout;
    private List<Fragment> mFragments = new ArrayList<>();//标签页集合
    private MyFragmentAdapter mFragmentAdapter;//自定义适配器
    private List<String> mTitles = new ArrayList<>();//标签集合

    @OnClick(R.id.tv_backhome)
    public void back() {
        Intent intent = new Intent(OrderManagerAcitivity.this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ordermanager);
        ButterKnife.bind(this);
        initView();

    }
//初始化视图
    private void initView() {
        mTitles.add("进行中");//标签
        mTitles.add("违章管理");
        mTitles.add("历史订单");
      // 标签页
        mFragments.add(new FragmentRunning());
        mFragments.add(new FragmentHistory());
        mFragments.add(new FragmentDeal());


        mFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mViewPager.setAdapter(mFragmentAdapter);<span style="font-family: Arial;">//ViewPager设置适配器</span>
        mViewPager.addOnPageChangeListener(this);//将ViewPager加入到该Activity中
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);//设置TabLayout模式,当前为系统默认模式.
        mTabLayout.setupWithViewPager(mViewPager);//将ViewPager和Tablayout关联起来
        mTabLayout.setTabsFromPagerAdapter(mFragmentAdapter);<span style="font-family: Arial;">//TabLayout设置加适配器</span>
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
//自定义适配器
    private class MyFragmentAdapter extends FragmentPagerAdapter {
        private List<String> mTitles;
        private List<Fragment> mFragments;
     
        public MyFragmentAdapter(FragmentManager fm, List<Fragment> mFragments, List<String> mTitles) {
            super(fm);
            this.mFragments = mFragments;
            this.mTitles = mTitles;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles.get(position);
        }
        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}

3、标签页继承于Fragment,其java 代码和布局文件如下:(这只是一个,其余的一样根据需求自己加)

public class FragmentDeal extends Fragment {
    @Nullable
    @Override//构建视图
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_deal,container,false);
    }
}
标签页面具体布局
<?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"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="违章处理" />

</LinearLayout>


如果要实现其他方式,可参考其他博客:

http://blog.csdn.net/zhang31jian/article/details/29867951

http://blog.csdn.net/harvic880925/article/details/38487149






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫雾凌寒

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值