我已经看到了几个关于在适配器中多次调用GetView并导致性能问题的其他问题。但是,这个问题并不重复。
我发现的所有答案都提到, ListView 的高度为 wrap_content 会导致此类行为,但我使用的是适配器来填充 GridView ,高度设置为 match_parent 。一遍又一遍地调用GetView,直到活动崩溃。
这是我试图填充的GridView的布局:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.jeff_fennell.capstone.GroupDetailView"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/members_in_group"
android:textIsSelectable="false"
android:textSize="25sp"
android:id="@+id/group_detail_name"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/invite_member_button"
android:text="@string/invite_friends_button"
android:onClick="openInviteDialog"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/group_empty_message"
android:visibility="invisible"
android:text="@string/group_empty_message"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"/>
<it.sephiroth.android.library.widget.HListView
android:layout_width="match_parent"
android:layout_height="90dp"
android:id="@+id/members_in_group"
android:layout_weight="0">
</it.sephiroth.android.library.widget.HListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/albums"
android:textIsSelectable="false"
android:textSize="25sp"
android:id="@+id/group_albums"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/new_album_button"
android:text="@string/new_album_button_text"
android:onClick="openCreateAlbumDialog"/>
</LinearLayout>
</LinearLayout>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:id="@+id/album_list">
</GridView>
</LinearLayout>
这是适配器:
public class AlbumAdapter extends ArrayAdapter<Album>{
private Activity activity;
public AlbumAdapter(Activity activity, List<Album> albums) {
super(getApplicationContext(), R.layout.album_preview, albums);
this.activity = activity;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Album album = getItem(position);
RelativeLayout albumView = null;
if (convertView != null) {
albumView = (RelativeLayout)convertView;
} else {
albumView = (RelativeLayout)getLayoutInflater().inflate(R.layout.album_preview, null);
}
TextView albumName = (TextView)albumView.findViewById(R.id.album_name);
albumName.setText(album.getName());
Call<List<Image>> getAlbumPreview = Utils.getClient()
.getAlbumPreviewImages(
album.getGroupId(),
album.getAlbumId(),
UserProfile.getAuthenticationToken(activity)
);
getAlbumPreview.enqueue(new Callback<List<Image>>() {
@Override
public void onResponse(Call<List<Image>> call, Response<List<Image>> response) {
if (response.isSuccessful()) {
List<Image> previewImages = response.body();
for (int i = 0; i < previewImages.size(); i++) {
Image image = previewImages.get(i);
int[] imageViewsToPopulate = {
R.id.image_1,
R.id.image_2,
R.id.image_3,
R.id.image_4,
R.id.image_5
};
final ImageView previewImageView = (ImageView)findViewById(imageViewsToPopulate[i]);
Glide
.with(activity)
.load(image.getUrl())
.asBitmap()
.placeholder(R.drawable.ic_people_white_48dp)
.centerCrop()
.into(new BitmapImageViewTarget(previewImageView) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(activity.getResources(), resource);
circularBitmapDrawable.setCircular(true);
previewImageView.setImageDrawable(circularBitmapDrawable);
}
});
}
} else {
//set no images message
}
}
@Override
public void onFailure(Call<List<Image>> call, Throwable t) {
}
});
return albumView;
}
我意识到我有一些硬编码的值,代码需要清理一下,但我还没有完成清理/重构。
任何帮助将不胜感激。 感谢。
答案 0 :(得分:0)
尝试使用ViewHolder模式。
实施例:
https://developer.android.com/training/improving-layouts/smooth-scrolling.html