代码示例
时间: 2023-07-02 07:16:14 浏览: 81
以下是一个简单的代码示例,演示了如何使用ViewPager和Fragment实现左右滑动切换页面的效果:
1. MainActivity.java
```
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private MyPagerAdapter pagerAdapter;
private List<Fragment> fragmentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
viewPager = findViewById(R.id.viewPager);
}
private void initData() {
fragmentList = new ArrayList<>();
fragmentList.add(new Fragment1());
fragmentList.add(new Fragment2());
fragmentList.add(new Fragment3());
pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragmentList);
viewPager.setAdapter(pagerAdapter);
viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
}
}
```
2. MyPagerAdapter.java
```
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
public MyPagerAdapter(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();
}
}
```
3. ZoomOutPageTransformer.java
```
public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.85f;
private static final float MIN_ALPHA = 0.5f;
@Override
public void transformPage(@NonNull View page, float position) {
int pageWidth = page.getWidth();
int pageHeight = page.getHeight();
if (position < -1) { // 左侧不可见
page.setAlpha(0);
} else if (position <= 1) { // [-1,1],页面正常滑动时
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float vertMargin = pageHeight * (1 - scaleFactor) / 2;
float horzMargin = pageWidth * (1 - scaleFactor) / 2;
if (position < 0) {
page.setTranslationX(horzMargin - vertMargin / 2);
} else {
page.setTranslationX(-horzMargin + vertMargin / 2);
}
page.setScaleX(scaleFactor);
page.setScaleY(scaleFactor);
page.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
} else { // 右侧不可见
page.setAlpha(0);
}
}
}
```
4. Fragment1.java, Fragment2.java, Fragment3.java
```
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container, false);
return view;
}
}
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2, container, false);
return view;
}
}
public class Fragment3 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment3, container, false);
return view;
}
}
```
这里的布局文件和其他代码都比较简单,就不一一列举了。需要注意的是,ZoomOutPageTransformer是用来实现页面滑动效果的,可以根据需要自行修改。
阅读全文