我在我的一个应用程序中的 Gallery
小部件中遇到了一个相当奇怪的问题,我认为是时候向其他人询问这个问题了,因为它已经困扰我很长时间了.
我正在为 Gallery
小部件使用自定义 Adapter
,并且我在此为 Gallery
中的每个项目填充自定义布局适配器
。您可以在下面看到Adapter
和布局的代码。 Gallery
小部件当前不会重用 Views
,因此我没有使用任何 ViewHolder
技巧等。
问题
问题是,当 Gallery
小部件在平板电脑屏幕上运行时,我遇到了“跳转”,但它在手机上运行良好。我对两种类型的设备使用不同的布局,并且我知道它们的使用正确 - 我已经对此进行了相当彻底的测试。
我尝试过的
- 更改
Gallery
项目布局宽度/高度 - 没有变化。但如果我设置背景颜色,它就会出现。 - 尝试删除
Gallery
间距,但这会使Gallery
项目相互重叠 - 确保
适配器
正常工作并且正在加载正确的布局 - 确保我没有更改代码中的任何
适配器
设置 - 清理项目
问题的视频示例
这两种设备使用完全相同的应用程序包和相同的代码库,唯一的区别是布局,如下所示。
无法工作 - 在平板电脑上(Acer Iconia A500 - 10 英寸、1280 x 800、mdpi):http://www.youtube.com/watch?v=vN2d61_Ojsc
工作 - 在手机(Samsung Galaxy Nexus - 4.7 英寸、720 x 1280、xhdpi)上:http://www.youtube.com/watch?v=j8s-JfwWofo
代码 - 图库适配器
public class ImageAdapter extends BaseAdapter {
LayoutInflater inflater;
public ImageAdapter() {
inflater = (LayoutInflater) Main.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return coverFileNames.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = (ImageView) inflater.inflate(R.layout.main_large_img, null);
((ImageView) convertView).setImageBitmap(coverFileNames.get(position));
return convertView;
}
}
代码 - 主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alwaysDrawnWithCache="true"
android:animationCache="true"
android:orientation="vertical" >
<TextView
android:id="@+id/txtMainTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="0sp"
android:paddingLeft="30sp"
android:paddingTop="20sp"
android:shadowColor="#000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="1.0"
android:text="@string/mainMyMoviesTitle"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<TextView
android:id="@+id/txtNumberofMovies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="30sp"
android:shadowColor="#000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="1.0"
android:text="@string/mainNumberofMovies"
android:textAppearance="?android:attr/textAppearanceSmall" >
</TextView>
<Gallery
android:id="@+id/gallery1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:spacing="20dp"
android:unselectedAlpha="1.0" >
</Gallery>
</LinearLayout>
代码 - 图库项目布局(平板电脑、layout-xlarge-land-mdpi
)
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLargeImg"
android:layout_width="374dp"
android:layout_height="600dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:maxHeight="600dp"
android:maxWidth="374dp"
android:minHeight="600dp"
android:minWidth="374dp"
android:scaleType="fitXY"
android:src="@drawable/noposterxl" />
代码 - 图库项目布局(电话、layout-normal-port-xhdpi
)
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLargeImg"
android:layout_width="188dp"
android:layout_height="300dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:maxHeight="300dp"
android:maxWidth="188dp"
android:src="@drawable/noposterxl" />
PS。有问题的应用程序是 Mizuu - 市场上有一个免费版本,您可以尝试自己查看问题。当您滑动水平 ScrollView 并且即将显示下一个图像时,就会发生这种情况。免费版本目前仅适用于平板电脑。
最佳答案
发现这听起来像是您的问题:
http://www.unwesen.de/2011/04/17/android-jittery-scrolling-gallery/
修复方法是重写这两个方法。我不太确定这是正确的方法:
private long mLastScrollEvent;
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
long now = SystemClock.uptimeMillis();
if (Math.abs(now - mLastScrollEvent) > 250) {
super.onLayout(changed, l, t, r, b);
}
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY)
{
mLastScrollEvent = SystemClock.uptimeMillis();
return super.onScroll(e1, e2, distanceX, distanceY);
}
它有一个 hacky 修复,假设你可以尝试一下。
还有一个错误报告已提交:
关于Android Gallery 小部件 "jitter"/"jumping"/表现怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8947250/