版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/u013132758。 https://blog.csdn.net/u013132758/article/details/50095053
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