1111111111111111111
前台代码(activity_main.xml):
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"40dp"
android:layout_marginTop
=
"1dp"
android:background
=
"@android:color/white"
android:baselineAligned
=
"false"
android:gravity
=
"center_vertical"
android:orientation
=
"horizontal"
android:paddingBottom
=
"5dp"
android:paddingTop
=
"5dp"
>
<!--detail Tab-->
<
TextView
android:id
=
"@+id/item_detail"
android:layout_width
=
"0dp"
android:layout_height
=
"match_parent"
android:layout_weight
=
"1"
android:gravity
=
"center_horizontal|center_vertical"
android:text
=
"@string/detail_tab"
android:textColor
=
"@color/main_tab_text_color"
android:textSize
=
"20dp"
/>
<!--category report Tab-->
<
TextView
android:id
=
"@+id/item_category_report"
android:layout_width
=
"0dp"
android:layout_height
=
"match_parent"
android:layout_weight
=
"1"
android:gravity
=
"center_horizontal|center_vertical"
android:text
=
"@string/category_report_tab"
android:textColor
=
"@color/main_tab_text_color"
android:textSize
=
"20dp"
/>
</
LinearLayout
>
<
android.support.v4.view.ViewPager
android:id
=
"@+id/mainViewPager"
android:layout_width
=
"match_parent"
android:layout_height
=
"0dp"
android:layout_weight
=
"1"
/>
</
LinearLayout
>
222222222222222222222222222
主界面代码(MainActivity.java):
public
class
MainActivity
extends
AppCompatActivity
implements
View.OnClickListener {
private
TextView item_detail, item_category_report;
private
ViewPager vp;
private
OneFragment oneFragment;
private
TwoFragment twoFragment;
private
List<Fragment> mFragmentList =
new
ArrayList<Fragment>();
private
FragmentAdapter mFragmentAdapter;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
mFragmentAdapter =
new
FragmentAdapter(
this
.getSupportFragmentManager(), mFragmentList);
vp.setOffscreenPageLimit(
2
);
//ViewPager的缓存为2帧
vp.setAdapter(mFragmentAdapter);
vp.setCurrentItem(
0
);
//初始设置ViewPager选中第一帧
item_detail.setTextColor(Color.parseColor(
"#1ba0e1"
));
//ViewPager的监听事件
vp.setOnPageChangeListener(
new
ViewPager.OnPageChangeListener() {
@Override
public
void
onPageScrolled(
int
position,
float
positionOffset,
int
positionOffsetPixels) {
}
@Override
public
void
onPageSelected(
int
position) {
/*此方法在页面被选中时调用*/
changeTextColor(position);
}
@Override
public void onPageScrollStateChanged(int state) {
/*此方法是在状态改变的时候调用,其中arg0这个参数有三种状态(0,1,2)。
arg0==1的时辰默示正在滑动,
arg0==2的时辰默示滑动完毕了,
arg0==0的时辰默示什么都没做。*/
}
});
}
/**
* 初始化布局View
*/
private void initViews() {
item_detail = (TextView) findViewById(R.id.item_detail);
item_category_report = (TextView) findViewById(R.id.item_category_report);
item_detail.setOnClickListener(this);
item_category_report.setOnClickListener(this);
vp = (ViewPager) findViewById(R.id.mainViewPager);
oneFragment = new OneFragment();
twoFragment = new TwoFragment();
//给FragmentList添加数据
mFragmentList.add(oneFragment);
mFragmentList.add(twoFragment);
}
/**
* 点击头部Text 动态修改ViewPager的内容
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.item_detail:
vp.setCurrentItem(0, true);
break;
case R.id.item_category_report:
vp.setCurrentItem(1, true);
break;
}
}
public class FragmentAdapter extends FragmentPagerAdapter {
List<Fragment> fragmentList = new ArrayList<Fragment>();
public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
/**
* 由ViewPager的滑动修改头部导航Text的颜色
* @param position
*/
private
void
changeTextColor(
int
position) {
if
(position ==
0
) {
item_detail.setTextColor(Color.parseColor(
"#1ba0e1"
));
item_category_report.setTextColor(Color.parseColor(
"#000000"
));
}
else
if
(position ==
1
) {
item_category_report.setTextColor(Color.parseColor(
"#1ba0e1"
));
item_detail.setTextColor(Color.parseColor(
"#000000"
));
}
}
}
//333333333333333333333333
需要多少个Fragment,便创建多少个,这里只举例写一个,其它相同
建立Fragment(fragment_one.xml):
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_gravity
=
"center"
android:text
=
"@string/detail_tab"
android:textSize
=
"25sp"
/>
</
LinearLayout
>
/////4444444444444444444444444444444444
Fragment界面代码(OneFragment.java):
public
class
OneFragment
extends
Fragment {
@Override
public
View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return
inflater.inflate(R.layout.fragment_one, container,
false
);
}
}