老长时间没有编辑过Vp滑动切换Fragment的代码了
这段时间有时间赶紧复习了一下,贴在网上备用---->
这是布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".view.activity.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<TextView
android:id="@+id/one"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/oneFragment" />
<TextView
android:id="@+id/two"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/twoFragment" />
<TextView
android:id="@+id/three"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/threeFragment" />
</LinearLayout>
</LinearLayout>
这是activity的代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ViewPager mVp;
private TextView mOne;
private TextView mTwo;
private TextView mThree;
private ArrayList<Fragment> fragments;
private ArrayList<TextView> textViews;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mVp = findViewById(R.id.vp);
mOne = findViewById(R.id.one);
mTwo = findViewById(R.id.two);
mThree = findViewById(R.id.three);
//装载fragment
fragments = new ArrayList<>();
fragments.add(new OneFragment());
fragments.add(new TwoFragment());
fragments.add(new ThreeFragment());
//装载textview
textViews = new ArrayList<>();
textViews.add(mOne);
textViews.add(mTwo);
textViews.add(mThree);
mOne.setOnClickListener(this);
mTwo.setOnClickListener(this);
mThree.setOnClickListener(this);
//Vp默认展示第一页 textview也设置一下设置对应样式
textViews.get(0).setTextColor(Color.RED);
//Vp设置适配器
mVp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return fragments.get(i);
}
@Override
public int getCount() {
return fragments.size();
}
});
//Vp设置页面改变监听
mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
for (TextView text :
textViews) {
text.setTextColor(Color.BLACK);
}
//一定在最后一行!!! 否则样式不会被应用!
textViews.get(i).setTextColor(Color.RED);
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
/**
* textview点击切换fragment
* @param v
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.one:
mVp.setCurrentItem(0);
break;
case R.id.two:
mVp.setCurrentItem(1);
break;
case R.id.three:
mVp.setCurrentItem(2);
break;
}
}
}