GridView显示项不正确

我有一个GridView,它显示的项目不正确。 我正在尝试加载带有Retrofit的图像,并通过GridView在2列中显示它们。 当电话显示屏显示4张图像时,如果总图像数为8,则通过向下滚动重复前4张图像。没有显示所有8个图像正确。GridView显示项不正确

LinerLayout:

<LinearLayout 

android:id="@+id/panelBody"

android:layout_width="270dp"

android:layout_height="470dp"

android:layout_centerVertical="true"

android:layout_centerHorizontal="true"

android:orientation="vertical"

android:paddingTop="120dp"

android:paddingLeft="10dp"

android:paddingRight="10dp">

<Button

android:id="@+id/photosAddButton"

android:layout_width="match_parent"

android:layout_height="35dp"

android:layout_marginBottom="10dp"

android:background="@drawable/drw_button_sign"

android:text="@string/add_photo"

android:textColor="@color/colorWhite"/>

<GridView

android:id="@+id/imagesGalleryGridView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="50dp"

android:paddingBottom="5dp"

android:verticalSpacing="0dp"

android:horizontalSpacing="0dp"

android:numColumns="2"

android:stretchMode="columnWidth"

android:background="@color/colorPrimary"></GridView>

<Button

android:id="@+id/photosUpdateButton"

android:layout_width="match_parent"

android:layout_height="35dp"

android:layout_marginTop="-45dp"

android:background="@drawable/drw_button_sign"

android:text="@string/update_profile"

android:textColor="@color/colorWhite"/>

</LinearLayout>

负载照片功能:

public void loadPhotos() { 

API_RETROFIT retrofit = this.getRetrofit();

Call<List<PROVIDER_PHOTOS>> call = retrofit.getPhotos(providerID);

call.enqueue(new Callback<List<PROVIDER_PHOTOS>>() {

@Override

public void onResponse(Call<List<PROVIDER_PHOTOS>> call, Response<List<PROVIDER_PHOTOS>> response) {

if(response.isSuccessful() && response.body() != null) {

List<PHOTO_ITEM> photoItemList = new ArrayList<PHOTO_ITEM>();

photoItemList.clear();

photoses = response.body();

for (PROVIDER_PHOTOS items : photoses) {

photoItemList.add(new PHOTO_ITEM(items.getID(), items.getPath()));

}

galleryImageAdapter = new GalleryImageAdapter(PhotosProvider.this, photoItemList);

imagesGalleryGridView.setAdapter(galleryImageAdapter);

loading.dismiss();

}

}

@Override

public void onFailure(Call<List<PROVIDER_PHOTOS>> call, Throwable t) {

call.cancel();

Toast.makeText(getApplicationContext(), R.string.checkConnection, Toast.LENGTH_LONG).show();

loading.dismiss();

}

});

}

GalleryImageAdapter:

public class GalleryImageAdapter extends BaseAdapter { 

private Context context;

private List<PHOTO_ITEM> images;

public GalleryImageAdapter(PhotosProvider c, List<PHOTO_ITEM> items) {

context = c;

images = items;

}

// returns the number of images

public int getCount() {

return images.size();

}

// returns the ID of an item

public Object getItem(int position) {

return images.get(position);

}

// returns the ID of an item

public long getItemId(int position) {

return position;

}

// returns an ImageView view

public View getView(final int position, View convertView, ViewGroup parent) {

final PHOTO_ITEM pitem = getItems(position);

ImageView imageView;

LayoutInflater inflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {

convertView = inflater.inflate(R.layout.provider_photos_item, null);

imageView = (ImageView) convertView.findViewById(R.id.grid_item_image408);

Picasso.with(context).load(REQUEST.UPLOADS_PATH + pitem.URL).resize(110, 110).into(imageView);

imageView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Intent intent = new Intent(context, ProviderGalleryView.class);

intent.putExtra("setURL", pitem.URL);

context.startActivity(intent);

}

});

ImageButton deletePhoto = (ImageButton) convertView.findViewById(R.id.deletePhoto);

deletePhoto.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) { // DELETE PHOTO }

});

}

return convertView;

}

PHOTO_ITEM getItems(int position) {

return ((PHOTO_ITEM) getItem(position));

}

}

回答:

的代码后的线膨胀如果条件外的视图移动:

View row = convertView; 

if (row == null) {

row = inflater.inflate(R.layout.provider_photos_item, null);

}

ImageView imageView = (ImageView) row.findViewById(R.id.grid_item_image408);

imageView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Intent intent = new Intent(context, ProviderGalleryView.class);

intent.putExtra("setURL", pitem.URL);

context.startActivity(intent);

}

});

ImageButton deletePhoto = (ImageButton) row.findViewById(R.id.deletePhoto);

deletePhoto.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) { // DELETE PHOTO }

});

return row;

以上是 GridView显示项不正确 的全部内容, 来源链接: utcz.com/qa/257252.html

回到顶部