2017-12-02 18 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