GridView显示项目不正确

时间:2017-12-02 16:30:23

标签: android android-layout gridview android-gridview

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

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));
}
}

1 个答案:

答案 0 :(得分:0)

在if条件之外使视图膨胀后移动代码行:

AT+CMGS