2016-09-22 51 views
0

我有一個兩部分的問題,我希望有人可以提供幫助。 本質上,我正在從API讀取數據。 API的一個結果(在我爲應用程序製作的自定義庫中)是結果是否具有圖像。RecyclerView - 刪除項目並將項目退出

我想添加功能,用戶可以刪除卡,如果他們沒有圖像不顯示他們。我希望能夠在加載(他們存儲選擇)或即時執行此操作。

問題一

是否有可能與填充一個cardviews時recyclerview專門遺漏某些項目?我可以將圖像留在外面,但它不能解決實際將卡片遺留下來的問題。

問題二

是否有可能從上飛一個recyclerview刪除多個項目?我試過的是循環遍歷所有List項目的for循環,如果它們沒有圖像,我將它們刪除。問題是這隻有一半的時間,當它需要點擊4個複選框才能完成。

注:下面的代碼是onCheckedChangedListener

if(imageOnly.isChecked()){ 
    image = true; 
    for(int i = 0; i < strains.size(); i++){ 
     if(strains.get(i) != null) { 
      if (strains.get(i).getImage().equalsIgnoreCase(getContext().getResources().getString(R.string.no_image))) { 
       strains.remove(i); 
       adapter.notifyItemRemoved(i); 
      } 
     } 
    } 
    adapter.notifyDataSetChanged(); 
}else{ 
    image = false; 
} 

內添加適配器

public class StrainCardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 
    private final List<Strain> strains; 
    private Context context; 
    private boolean imageOnly; 

    public StrainCardAdapter(List<Strain> strains, Context context, boolean imageOnly) { 
     this.strains = strains; 
     this.context = context; 
     this.imageOnly = imageOnly; 
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.strain_card_view, parent, false); 
     StrainViewHolder svh = new StrainViewHolder(v); 
     return svh; 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
     if (!strains.get(position).getImage().equals(context.getResources().getString(R.string.no_image))) { 
      Picasso.with(context).load(strains.get(position).getImage()).into(((StrainViewHolder) holder).strainImage); 
     } 
    } 

    @Override 
    public int getItemCount() { 
     return strains.size(); 
    } 

    public static class StrainViewHolder extends RecyclerView.ViewHolder { 
     CardView strainCard; 
     ImageView strainImage; 

     public StrainViewHolder(View itemView) { 
      super(itemView); 
      strainCard = (CardView) itemView.findViewById(R.id.strain_card); 
      strainImage = (ImageView) itemView.findViewById(R.id.strain_image); 
     } 
    } 

} 

回答

0

可以,只要你從API響應對象呼叫,你就會製作,你可以檢查它們是否包含圖像。我猜你有辦法確定它。假設你有一個imageUrl與它關聯;指示一個空的圖像可能是你正在使用一個空字符串或null。

通過對象循環選擇具有圖像的應該是要走的路。

ArrayList<Strain> strainsList = new ArrayList<>(); 
String noImage = getContext().getResources().getString(R.string.no_image) 
for(Strain strain : responseObjects) { 
    if(!strain.getImage().equalsIgnoreCase(noImage)) { 
     strainsList.add(strain); 
    } 
} 

strains.addAll(strainsList); //Assuming strains is the arraylist used by your adapter 
adapter.notifyDataSetChanged(); 

刪除項目應該是直截了當:

您可以添加圖片說明可以在適配器的項目佈局的[可能是一個紅色的X]移除項目;點擊你可以刪除被點擊的項目。

public static class StrainViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
     CardView strainCard; 
     ImageView strainImage; 
     ImageView removeImageView; 

     public StrainViewHolder(View itemView) { 
      super(itemView); 
      strainCard = (CardView) itemView.findViewById(R.id.strain_card); 
      strainImage = (ImageView) itemView.findViewById(R.id.strain_image); 
      removeImageView = (ImageView) itemView.findViewById(R.id.remove_image_view); 
      removeImageView.setOnClickListener(this); 
     } 

     @Override 
     public void onClick(View v) { 
      if(v.getId() == R.id.remove_image_view) { 
       int position = getAdapterPosition(); 
       if(position != RecyclerView.NO_POSITION) { 
       strains.remove(position); 
       notifyItemRemoved(position); 
       } 
      } 
     } 
    } 

讓我知道是否需要更多的說明。