我正在自定义我的Android应用程序以在四个方向上滑动,我使用ViewPager
水平滑动和VerticalViewPager垂直滑动(这就像DirectionalViewPager的修改,因为原始的已被弃用)。问题是当在VerticalViewPager
中包含ViewPager
时,垂直滑动仅起作用,当在ViewPager
中包含VerticalViewPager
时,水平滑动仅起作用,这是我的第一个状态的代码:
VerticalViewPager verticalViewPager = (VerticalViewPager) findViewById(R.id.vertical_pager);
verticalViewPager.setAdapter(new TabPagerAdapter(getSupportFragmentManager()));
verticalViewPager.setPageTransformer(true, new ViewPager.PageTransformer() {
@Override
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
int pageHeight = view.getHeight();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 1) { // [-1,1]
// Modify the default slide transition to shrink the page as well
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) {
view.setTranslationY(vertMargin - horzMargin / 2);
} else {
view.setTranslationY(-vertMargin + horzMargin / 2);
}
// Scale the page down (between MIN_SCALE and 1)
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
// Fade the page relative to its size.
view.setAlpha(MIN_ALPHA +
(scaleFactor - MIN_SCALE) /
(1 - MIN_SCALE) * (1 - MIN_ALPHA));
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
});
verticalViewPager.setCurrentItem(1);
TabPagerAdapter TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
ViewPager Tab = (ViewPager) findViewById(R.id.pager);
Tab.setAdapter(TabAdapter);
Tab.setCurrentItem(1);
TabPagerAdapter类:
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
//Fragment for Left side
return new AC_Left();
case 1:
//Fragment for Center side
return new AC_Center();
case 2:
//Fragment for Right side
return new AC_Right();
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
AC_Left、AC_Right和AC_Center类似于片段
,除了返回的布局
:
public class AC_Center extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View ac_center_frag = inflater.inflate(R.layout.ac_center_fragment, container, false);
return ac_center_frag;
}
}
还有XML文件:
<RelativeLayout 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:background="@drawable/bg" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/buttonBar"
android:orientation="vertical"
android:weightSum="1" >
<com.example.myapp.VerticalViewPager
android:id="@+id/vertical_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.example.myapp.VerticalViewPager>
</LinearLayout>
</RelativeLayout>
我用一个示例应用程序为此创建了一个库,并将其放在github上。基本上,它维护一个视图端口,并在每次页面更改时更新视图分页器。你必须实现一个适配器,为每一行、每列索引提供片段。诚然,它有点被黑在一起,但应该可以同时工作。你可以设置一个位掩码,让它按照你需要的任何方向换行。将其设置为GRID_WRAP_NONE,让它根本不换行。
https://github.com/btate/BTGridPager-Android
这可能不是你所需要的,但它应该会让你开始。