【发布时间】:2014-06-26 00:47:37
【问题描述】:
所以,我的应用程序上的 GridView 有一个非常奇怪的问题。
我想要做的非常简单:我只想获取已安装应用程序的列表(a-la 应用程序抽屉)并在网格视图中按字母顺序显示它们。
它工作得很好,它们都显示得很好,但是当我向下滚动以查看更多应用并向上滚动时,顶部的应用略有偏移。垂直方向,更准确地说,每次我向下滚动超过 1-2 行并一直向上滚动时都会发生这种情况。
另外,有时我会滚动回顶部,它会一直滚动到顶部的应用程序之外,而不会让我向下滚动。我不确定这些问题是否相关,但我肯定想解决这两个问题。在应用中单击其中一项或切换窗口后,gridView 会完全恢复正常。
这只是我使用 android Layouts 等的第二天,虽然我以前做过 Java 编程很长时间,所以我可能会遗漏一些东西,但我对此并不完全陌生。
我在网上查看过,但找不到有效的答案,我已经尝试了所有我找到的答案,现在我只是遇到了我已经在我的代码中实现的解决方案,所以我不知道除了这里还能去哪里。
我也不确定要发布我的代码的哪一部分来解决这个问题,我不想把它变成一个巨大的文本墙,包含我的每个相关文件,所以我将不胜感激如果有人能指导我展示什么,那就太好了。
我将从我的 GridView 布局、我的单个单元格布局和我的适配器的 getView 方法开始,因为这些似乎是我找到的大多数解决方案被引导到的地方。如果您需要更多我的代码,请告诉我。
layout_grid
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="488dp"
android:numColumns="5"
android:textAlignment="center"
android:verticalSpacing="5dp" >
</GridView>
</LinearLayout>
layout_app
<ImageView
android:id="@+id/ivIcon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:scaleType="center"
android:adjustViewBounds="true"
/>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
/>
</LinearLayout>
获取视图
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView == null){
v = LayoutInflater.from(mContext).inflate(R.layout.layout_app, null);
//mContext is defined in the constructor for my Adapter
}else{
v = convertView;
}
ImageView icon = (ImageView)v.findViewById(R.id.ivIcon);
TextView name = (TextView)v.findViewById(R.id.tvName);
icon.setImageDrawable(getItem(position).getIcon());
name.setText(getItem(position).getName());
//getIcon and getName are from a custom App class made to store app info.
return v;
}
我有它的外观图片,但由于我的声誉,它不允许我发布它们或提供所有它们的链接,所以如果你想看一些具体的东西,请问我。
P.S.:我遇到的另一个小问题是,一些图标似乎没有完全缩小到 ImageView 的大小,但据我所知,这完全不相关,也不是本文的重点。不过,如果你愿意,你也可以帮我解决这个问题:P
【问题讨论】:
标签: android android-layout android-gridview