我有RecyclerView
的适配器。每个适配器项都有ImageView
和另一个适用于RecyclerView
的适配器,每个项目的ImageView
也有。{必须单击子项并转到其详细视图。在该视图中,当我单击REMOVE按钮时,它必须删除此子项。所以我有几个问题:
我的适配器的代码。在主适配器中,我有适用于子项的内部类:
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
的实例。我做错了什么?
有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 :(得分:1)
好吧,我们去吧。在 private List getFakeData()方法中,您将 fakeDuplicates 的相同列表传递给主适配器中的所有项目。这意味着他们共享基础数据。对它的更改将反映在从数据创建的所有视图中。将列表的实例化放在循环中,看看是否有帮助。