2017-12-02 211 views
0

我有一個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)); 
} 
} 
+0

查看我的答案 – diegoveloper

回答

0

的代碼後的線膨脹如果條件外的視圖移動:

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; 
+0

Woooow,它正在工作。非常感謝你 ! –

+0

我很高興幫助你:) – diegoveloper