2016-02-13 60 views
1

我一直使用fresco庫來加載適配器中的圖像,但圖像未按照我的預期正確設置。 這是我的代碼:通過Fresco庫加載的適配器圖像是不規則的

public class HomeListingAdapter_recyler1 extends RecyclerView.Adapter { 
    private Context context; 
    private ArrayList propertyItemList; 
    private ImageLoader mImageLoader; 
    public HomeListingAdapter_recyler1(HomeListingActivity_recycler propertyViews, ArrayList propertyItemList) { 
     Fresco.initialize(propertyViews); 
     this.propertyItemList = propertyItemList; 
     this.context = propertyViews; 
    } 
    @Override 
    public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.property_item_layout,parent,false); 
     CustomViewHolder viewHolder = new CustomViewHolder(itemLayoutView); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(final CustomViewHolder holder, final int position) { 
     mImageLoader = VolleySingletonPattern.getInstance(context).getImageLoader(); 
     holder.txtPropertyName.setText(propertyItemList.get(position).ville); 
     holder.txtPropertyType.setText(propertyItemList.get(position).bienName); 
     if(propertyItemList.get(position).pieces.equalsIgnoreCase("0")){ 
      holder.txtPropertySurfaceArea.setText(propertyItemList.get(position).surface+" "+context.getString(R.string.meter_square)); 
     } else {    holder.txtPropertySurfaceArea.setText(propertyItemList.get(position).surface+" "+context.getString(R.string.meter_square)+" - "+ propertyItemList.get(position).pieces+" "+context.getResources().getString(R.string.pieces)); 
     } 
     holder.txtPropertyPrice.setText(propertyItemList.get(position).montantLoyer); 
     Uri imageUri; 
     try { 
      if(!TextUtils.isNullOrEmpty(propertyItemList.get(position).photo)) { 
       imageUri = Uri.parse(propertyItemList.get(position).photo); 
       holder.imgPropertyImage.setVisibility(View.VISIBLE); 
       holder.imgPropertyImage.setImageURI(imageUri); 
      } 
     } catch (Exception e) { 
     } 
    } 
    @Override 
    public int getItemCount() { 
     return propertyItemList.size(); 
    } 
    public class CustomViewHolder extends RecyclerView.ViewHolder { 
     SimpleDraweeView imgPropertyImage; 
     public TextView txtPropertyName , txtPropertyType , txtPropertySurfaceArea ,txtPropertyPrice; 
     public CustomViewHolder(View itemView) { 
      super(itemView); 
      imgPropertyImage = (SimpleDraweeView) itemView.findViewById(R.id.image_property); 
      txtPropertyName = (TextView) itemView.findViewById(R.id.txt_property_name); 
      txtPropertyType = (TextView) itemView.findViewById(R.id.txt_property_type); 
      txtPropertySurfaceArea = (TextView) itemView.findViewById(R.id.txt_property_surface_piece); 
      txtPropertyPrice = (TextView) itemView.findViewById(R.id.txt_property_price); 
     } 
    } 
} 
+1

請讓我們知道您期望的輸出與實際相比。由於這是一個圖形/用戶界面問題,包括屏幕截圖可能會幫助人們弄清楚什麼是錯誤的。 –

+0

嗨,Henrik Gustafsson!感謝您的回覆。實際上,文本數據已經在列表視圖的相應位置正確設置,但圖像沒有設置在正確的位置,並有很多重複的地方。當我滾動列表視圖時,它會在項目中設置不同的圖像。我附上了屏幕截圖。 1)http://www.mediafire.com/view/bmkhrry9149p43r/fresco3.png 2)http://www.mediafire.com/view/afxzut3fdp5rbk2/fresco2.png 3)http://www.mediafire的.com /視圖/ ek1w1tmx8v3wa7m/fresco1.png –

回答

3

在xml中設置默認的佔位符圖像。在您的適配器類中,如果您的照片url不爲空,請將您的照片url字符串轉換爲uri,並將其設置爲view.setImageUri(uri),否則將view.setImageUri(null)設置爲您的其他部分。

Uri imageUri; 

try { 

holder.imgPropertyImage.setVisibility(View.VISIBLE); 

if(!TextUtils.isNullOrEmpty(propertyItemList.get(position).photo)) { 

imageUri = Uri.parse(propertyItemList.get(position).photo); 

holder.imgPropertyImage.invalidate(); 

holder.imgPropertyImage.setImageURI(imageUri); 

} 

else{ 

holder.imgPropertyImage.invalidate(); 

holder.imgPropertyImage.setImageURI(null); 

} 

} 
catch (Exception e) { 
      e.printStackTrace(); 

} 
相關問題