【发布时间】:2018-03-02 20:12:47
【问题描述】:
我目前正在开发一个文件共享应用程序。我创建了一个 GridView,我想在其中列出已下载的带有缩略图的文件。为此,我使用了自定义适配器。它加载良好,一切都可见,看起来很完美。
诀窍如下:加载视图后,我的自定义适配器的 getView 方法在位置 0 上被调用了很多次,这会导致大量滞后,我的 Xperia XZ Premium 上的 CPU 使用率约为 12%。
我已经阅读了有关 GridView 中滞后的答案,但在这些情况下,它是由滚动引起的。在我的情况下,不需要滚动,目前 GridView 中只有 1 个项目,但它仍在挣扎。
在滞后的同时,它还会发送垃圾邮件。如果我将以下代码行添加到我的 TextViews,此消息可能会消失:
android:singleLine="true"
但此属性已被弃用,并不能解决问题,只是消息消失了。
当视图为空时,滞后不存在。
我的适配器的getView方法:
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent){
View listItem = convertView;
if(listItem==null){
listItem = LayoutInflater.from(getContext()).inflate(R.layout.downloaded_grid_item, parent, false);
}
final Downloaded current = getItem(position);
final ImageView thumbnail = (ImageView)listItem.findViewById(R.id.downloaded_grid_item_thumb);
final TextView name = (TextView)listItem.findViewById(R.id.downloaded_grid_item_name);
final TextView subtext = (TextView) listItem.findViewById(R.id.downloaded_grid_item_subtext);
thumbnail.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
int width = thumbnail.getMeasuredWidth();
thumbnail.setLayoutParams(new LinearLayout.LayoutParams(width, width));
return true;
}
});
listItem.setTag(position);
thumbnail.setTag(position);
name.setTag(position);
subtext.setTag(position);
name.setText(current.getName());
subtext.setText(DateFormat.getDateInstance().format(new Date(current.getTime())));
thumbnail.setImageBitmap(ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(current.getPath()), 400, 400));
return listItem;
}
GridView:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/downloaded_grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:layout_margin="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
项目的自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/downloaded_grid_item"
android:background="@drawable/downloaded_grid_item_background"
android:orientation="vertical"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/downloaded_grid_item_thumb"
android:layout_width="match_parent"
android:layout_height="90dp"
android:src="@drawable/ic_menu_gallery"
android:scaleType="centerInside"/>
<TextView
android:id="@+id/downloaded_grid_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
tools:text="name"
android:ellipsize="end"
android:lines="1"/>
<TextView
android:id="@+id/downloaded_grid_item_subtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
tools:text="download date"
android:ellipsize="end"
android:lines="1"/>
</LinearLayout>
以及由设备发送的垃圾邮件:
W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1
【问题讨论】:
标签: android xml android-layout android-gridview