2016-06-21 117 views
1

我有一個適配器用於我的RecyclerView。每個適配器的項目有ImageView和另一個適配器RecyclerView,其中每個項目也有ImageView。我必須點擊子項並轉到其詳細視圖。在這個視圖中,當我點擊REMOVE按鈕時,它必須刪除這個子項。所以我有幾個問題:Android從RecyclerView刪除子項目

  1. 我在主適配器,它有兩個參數實現方法:在主列表中的位置和在子列表中的位置。這就是我試圖去除它。問題是,當我在一個位置刪除一個子項時,它會刪除另一個位置中的相同子項(我使用假數據,其中一些是相同的)。
  2. 刪除一個子項後,當我嘗試刪除第二個子項時,它不會刪除。我已經檢查了調試模式,並且看到子項目列表的大小在開始時變得如此。

有我的適配器的代碼。在主適配器我有子項的適配器內部類:

public class PhotoWithDuplicatesAdapterTest extends RecyclerView.Adapter<PhotoWithDuplicatesAdapterTest.ViewHolder> { 

    private Context mContext; 
    private List<MediaWithDuplicatesTest> mTests; 
    private OnDuplicateClickListener mListener; 

    public PhotoWithDuplicatesAdapterTest(Context context, List<MediaWithDuplicatesTest> tests) { 
     mContext = context; 
     mTests = tests; 
    } 

    public interface OnDuplicateClickListener { 
     void onDuplicateClick(int photoPosition, int duplicatePosition); 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.photo_with_duplicates_item, parent, false); 
     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     Picasso.with(mContext) 
       .load(mTests.get(position).getPhoto()) 
       .fit() 
       .centerCrop() 
       .into(holder.mPhotoImageView); 
     DuplicateAdapter adapter = new DuplicateAdapter(mContext, position, mTests.get(position).getDuplicates()); 
     adapter.setOnDuplicateClickListener(new DuplicateAdapter.OnDuplicateClickListener() { 
      @Override 
      public void onDuplicateClick(int photoPosition, int duplicatePosition) { 
       if (mListener != null) mListener.onDuplicateClick(photoPosition, duplicatePosition); 
      } 
     }); 
     LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false); 
     holder.mDuplicatesRecyclerView.setLayoutManager(manager); 
     holder.mDuplicatesRecyclerView.setAdapter(adapter); 
    } 

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

    public void setOnDuplicateClickListener(OnDuplicateClickListener listener) { 
     mListener = listener; 
    } 

    public void removeDuplicate(int photoPosition, int duplicatePosition) { 
     List<String> duplicates = mTests.get(photoPosition).getDuplicates(); 
     duplicates.remove(duplicatePosition); 
     if (duplicates.size() == 0) { 
      mTests.remove(photoPosition); 
      notifyItemRemoved(photoPosition); 
     } else notifyItemChanged(photoPosition); 
    } 

    public String getDuplicate(int photoPosition, int duplicatePosition) { 
     return mTests.get(photoPosition).getDuplicates().get(duplicatePosition); 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder { 

     @Bind(R.id.iv_photo) SquareImageView mPhotoImageView; 
     @Bind(R.id.rv_duplicates) RecyclerView mDuplicatesRecyclerView; 

     public ViewHolder(View itemView) { 
      super(itemView); 
      ButterKnife.bind(this, itemView); 
     } 
    } 

    public static class DuplicateAdapter extends RecyclerView.Adapter<DuplicateAdapter.ViewHolder> { 

     private Context mContext; 
     private int mPhotoPosition; 
     private List<String> mDuplicates; 
     private OnDuplicateClickListener mListener; 

     public DuplicateAdapter(Context context, int photoPosition, List<String> duplicates) { 
      mContext = context; 
      mPhotoPosition = photoPosition; 
      mDuplicates = duplicates; 
     } 

     public interface OnDuplicateClickListener { 
      void onDuplicateClick(int photoPosition, int duplicatePosition); 
     } 

     @Override 
     public DuplicateAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.duplicate_item, parent, false); 
      return new ViewHolder(view); 
     } 

     @Override 
     public void onBindViewHolder(DuplicateAdapter.ViewHolder holder, int position) { 
      Picasso.with(mContext) 
        .load(mDuplicates.get(position)) 
        .fit() 
        .centerCrop() 
        .into(holder.mDuplicateImageView); 
     } 

     @Override 
     public int getItemCount() { 
      return mDuplicates == null ? 0 : mDuplicates.size(); 
     } 

     public void setOnDuplicateClickListener(OnDuplicateClickListener listener) { 
      mListener = listener; 
     } 

     public class ViewHolder extends RecyclerView.ViewHolder { 

      @Bind(R.id.iv_duplicate) SquareImageView mDuplicateImageView; 

      @OnClick(R.id.iv_duplicate) 
      void onDuplicateClick() { 
       if (mListener != null) mListener.onDuplicateClick(mPhotoPosition, getAdapterPosition()); 
      } 

      public ViewHolder(View itemView) { 
       super(itemView); 
       ButterKnife.bind(this, itemView); 
      } 
     } 
    } 
} 

這就是我所說如何刪除子項(在Activity

// implementing of DuplicatesFragment.OnRemoveListener 
@Override 
public void onRemove(int photoPosition, int duplicatePosition) { 
    fm.popBackStackImmediate(); 
    Fragment fragment = fm.findFragmentById(R.id.fragment_container); 
    if (fragment instanceof DuplicatesFragment) { 
     DuplicatesFragment duplicatesFragment = (DuplicatesFragment) fragment; 
     duplicatesFragment.removeDuplicate(photoPosition, duplicatePosition); 
    } 
} 

而且removeDuplicate方法DuplicatesFragment

public void removeDuplicate(int photoPosition, int duplicatePosition) { 
    if (mAdapter != null) mAdapter.removeDuplicate(photoPosition, duplicatePosition); 
} 

mAdapter - PhotoWithDuplicatesAdapterTest的實例。我究竟做錯了什麼?

這是它的外觀: enter image description here

MediaWithDuplicatesTest對象:

public class MediaWithDuplicatesTest { 

    private String mPhoto; 
    private List<String> mDuplicates; 

    public String getPhoto() { 
     return mPhoto; 
    } 

    public void setPhoto(String photo) { 
     mPhoto = photo; 
    } 

    public List<String> getDuplicates() { 
     return mDuplicates; 
    } 

    public void setDuplicates(List<String> duplicates) { 
     mDuplicates = duplicates; 
    } 
} 

也許,這將是有益的,如果我還張貼創建我的測試數據和適配器的代碼:

private void populateRecyclerViewHolder() { 
    LinearLayoutManager manager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false); 
    mAdapter = new PhotoWithDuplicatesAdapterTest(getContext(), getFakeData()); 
    mAdapter.setOnDuplicateClickListener(this); 
    mViewHolder.mPhotosWithDuplicatesRecyclerView.setLayoutManager(manager); 
    mViewHolder.mPhotosWithDuplicatesRecyclerView.setAdapter(mAdapter); 
} 

private List<MediaWithDuplicatesTest> getFakeData() { 
    List<MediaWithDuplicatesTest> fakeData = new ArrayList<>(); 
    List<String> fakeDuplicates = getFakeDuplicates(); 
    for (String fakeDuplicate : fakeDuplicates) { 
     MediaWithDuplicatesTest fake = new MediaWithDuplicatesTest(); 
     fake.setPhoto(fakeDuplicate); 
     fake.setDuplicates(fakeDuplicates); 
     fakeData.add(fake); 
    } 
    return fakeData; 
} 

private List<String> getFakeDuplicates() { 
    return new ArrayList<String>() { 
     { 
      add("http://2.bp.blogspot.com/-CmBgofK7QzU/TVj3u3N1h2I/AAAAAAAADN8/OszBhGvvXRU/s640/tumblr_lg7h9gpbtP1qap9qio1_500.jpeg"); 
      add("http://www.newcastlewildflower.com.au/wp-content/uploads/2013/05/9fkUCY02te7bqobeZzdT9SEio1_500.jpg"); 
      add("http://img.ffffound.com/static-data/assets/6/51cc46900bf5fe574293d49c4d9939e0ebfc8ee3_m.jpg"); 
      add("http://2.bp.blogspot.com/-CmBgofK7QzU/TVj3u3N1h2I/AAAAAAAADN8/OszBhGvvXRU/s640/tumblr_lg7h9gpbtP1qap9qio1_500.jpeg"); 
      add("http://www.newcastlewildflower.com.au/wp-content/uploads/2013/05/9fkUCY02te7bqobeZzdT9SEio1_500.jpg"); 
      add("http://img.ffffound.com/static-data/assets/6/51cc46900bf5fe574293d49c4d9939e0ebfc8ee3_m.jpg"); 
     } 
    }; 
} 
+0

呃,我們可以有一個視覺表示的工作流程?它很難想象你想達到什麼。 – GPuschka

+0

@GPuschka我編輯了我的帖子。正如你所看到的,在屏幕上我們有清單。每個列表項代表:ImageView(在灰色背景中)和另一個列表(ImageView的右側) –

+0

嗯,我沒有看到任何可能會破壞所提供代碼邏輯的事情。除了可能執行MediaWithDuplicatesTest。它是如何實現的? – GPuschka

回答

1

好的,我們走吧。在私人列表getFakeData()方法您傳遞相同列表 fakeDuplicates到主適配器中的所有項目。這意味着他們共享底層數據。對其進行的更改反映在從數據創建的所有視圖中。把列表的實例化到循環中,看看是否有幫助。

+0

Wow中顯示在RecyclerView中。它有助於解決我的第一個問題。謝謝!但第二個問題仍然在這裏:( –

+0

試着去觀察'private void populateRecyclerViewHolder()'是什麼時候以及如何調用的,它應該只被調用一次,但是從代碼中看到,描述我的猜測是在它之間的某個地方被調用刪除重複。詳細視圖是一個活性? – GPuschka

+0

細節視圖是一個片段 –