gpt4 book ai didi

Android 4.0.x 和 4.1.x viewPager 错误?

转载 作者:行者123 更新时间:2023-11-29 20:54:26 25 4
gpt4 key购买 nike

我在 android API 15 和 16 上使用 viewPager 时遇到问题,我的应用程序基本上是简单的远程控制机顶盒,具有三个屏幕,在第一个屏幕上您可以选择要控制的机顶盒,第二个和第三个只是带控件的屏幕。现在的问题是,一切都在其他版本的 android 上正常运行,但在 API 15 和 16 上,当应用程序启动时,我无法使用带有框的屏幕,当我点击任何地方时,它会检测到第二个屏幕上的操作,这些操作是控件,当我滑动到第三个屏幕然后返回时,再次调用第一个屏幕的 onCreateView 并且一切正常。这是第一屏和第二屏的图像enter image description here second这是我的 viewPager 代码:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch (position) {
case 0:
return boxesFragment;
case 1:
return miscpadFragment;
case 2:
if (metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM) {
return volPadFragment;
} else return numberPadFragment;
case 3:
return numberPadFragment;
}
return null;
}

@Override
public int getCount() {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
case DisplayMetrics.DENSITY_MEDIUM:
if (isTablet) {
return 2;
} else return 4;
case DisplayMetrics.DENSITY_HIGH:
if (isTablet) {
return 2;
} else return 3;
case DisplayMetrics.DENSITY_XHIGH:
if (isTablet) {
return 2;
} else return 3;
case DisplayMetrics.DENSITY_XXHIGH:
return 3;
case DisplayMetrics.DENSITY_TV:
return 2;
}
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}

和我也用于动画的 DepthPageTransformer:

public class DepthPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.8f;

public void transformPage(View view, float position) {
int pageWidth = view.getWidth();

if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);

} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(1);
view.setScaleY(1);

} else if (position <= 1) { // (0,1]
// Fade the page out.
view.setAlpha(1 - position);

// Counteract the default slide transition
view.setTranslationX(pageWidth * -position);

// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE
+ (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);

} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}

最佳答案

所以我解决了我的问题,它是我从 developers.google.com 下载的 DepthPageTransformer 中的 transformPage 方法,我将其修改如下:

public void transformPage(View view, float position) {
float translationX;
float scale;
float alpha;

if (position >= 1 || position <= -1) {
// Fix for https://code.google.com/p/android/issues/detail?id=58918
translationX = 0;
scale = 1;
alpha = 1;
} else if (position >= 0) {
translationX = -view.getWidth() * position;
scale = -0.2f * position + 1;
alpha = Math.max(1 - position, 0);
} else {
translationX = 0.5f * view.getWidth() * position;
scale = 1.0f;
alpha = Math.max(0.1f * position + 1, 0);
}

view.setTranslationX( translationX);
view.setScaleX(scale);
view.setScaleY(scale);
view.setAlpha(alpha);
}

一切都很完美。

关于Android 4.0.x 和 4.1.x viewPager 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28188315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com