【发布时间】:2012-01-20 20:08:35
【问题描述】:
我的一个应用程序中的 Gallery 小部件遇到了一个相当奇怪的问题,我认为是时候询问其他人了,因为它困扰了我很长时间。
我正在为Gallery 小部件使用自定义Adapter,并在此Adapter 中为Gallery 中的每个项目添加自定义布局。您可以在下面查看Adapter 的代码和布局。 Gallery 小部件目前没有重复使用Views,所以我没有使用任何ViewHolder 技巧等。
问题
问题是当Gallery 小部件在平板电脑屏幕上运行时,我遇到了“跳跃”,但它在手机上运行良好。我为这两种类型的设备使用了不同的布局,并且我知道它们的使用是正确的——我已经进行了相当彻底的测试。
我的尝试
- 更改
Gallery项目布局宽度/高度 - 没有变化。但如果我设置了背景颜色,它就会出现。 - 尝试删除
Gallery间距,但这会使Gallery项目相互重叠 - 确保
Adapter正在工作并且正在加载正确的布局 - 确保我没有更改代码中的任何
Adapter设置 - 清理项目
问题的视频示例
这两个设备使用完全相同的应用程序包和相同的代码库,唯一的区别是布局,如下所示。
不工作 - 在平板电脑上(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 - 市场上有一个免费版本,您可以自己尝试查看问题。当您滑动水平滚动视图并且即将显示下一个图像时会发生这种情况。免费版目前仅适用于平板电脑。
【问题讨论】:
-
您是否尝试在调试模式下将平板电脑连接到开发机器?这可能会在 logcat 中提供一些线索。
-
是的,在 Logcat 中绝对没有。我花了好几个小时试图解决这个问题。
-
我会说这就是所谓的“捕捉到”功能不跳跃。还没有读过你的代码,只是看了视频。 ...如果您明确设置大小,则不会在 xml 中声明最小和最大
-
不,“对齐”发生在您松开手指时,而不是在您滚动时。
-
从谷歌开发者那里读到一句话,当视图大小发生变化时,就会发生快照。因此,当新视图出现在屏幕上而旧视图消失时,快照就发生了。为什么不尝试检查 convertView 是否为 null 并重新使用它?
标签: android android-widget gallery android-3.0-honeycomb android-4.0-ice-cream-sandwich