Ich habe mehrere andere Fragen in Bezug auf GetView in einem Adapter mehrmals aufgerufen und verursacht Leistungsprobleme. Diese Frage ist jedoch kein Duplikat.GetView mehrmals aufgerufen
All die Antworten, die ich erwähnt haben gefunden, dass ein Listview mit mit einer Höhe von wrap_content ein solches Verhalten verursachen wird, aber ich bin mit einem Adapter eine Grid, füllen mit der Höhe zu match_parent eingestellt. GetView wird immer wieder aufgerufen, bis die Aktivität abstürzt. Hier
ist das Layout mit dem Gridview Ich aufzufüllen versuche:
<?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>
Dies ist der Adapter:
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;
}
Ich weiß, ich habe ein paar hartcodierte Werte, und die Code-Bedürfnisse ein bisschen aufgeräumt zu werden, aber ich bin noch nicht dazu gekommen, zu säubern/zu refactoring.
Jede Hilfe wäre willkommen. Danke.
versuchen Sie dies: http://stackoverflow.com/a/18377020/3981656 –
fill_parent ist seit dem API-Ebene als veraltet 8 –