如何在Vertical RecyclerView中处理Horizo​​ntal RecyclerView

时间:2016-08-10 14:48:34

标签: android android-recyclerview

我正在使用RecyclerView,在其中我添加了一个Horizo​​ntal RecyclerView,用于显示图库之类的图像。

我在用户输入他的详细信息后逐个插入我的RecyclerView行。

我的主要问题是我通过相机或图库选择图像,它们在水平的RecyclerView中显示效果很好。

但是,当用户再添加一个单元格时,之前的一些图像将进入该单元格。这是不正确的。

如何解决此问题?

我的主要活动: -

how many line(s) do you want to read?

PersonalInformationRecyclerDoctorCatogoryAdapter: -

 private void addDoctorDetails() {

        try {
            personalInformatioDoctorCatogoryBeanArrayList.add(new PersonalInformatioDoctorCatogoryBean("Physician" + i, "MBBS" + i, "1985" + i,imagesBeanArrayList));
            personalInformationRecyclerDoctorCatogoryAdapter.notifyDataSetChanged();
            i++;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    } 

PersonalInformatioDoctorCatogoryBean: -

private List<PersonalInformatioDoctorCatogoryBean> personalInformatioDoctorCatogoryBeanList;
    private Context context;

    public PersonalInformationRecyclerDoctorCatogoryAdapter(Context context, List<PersonalInformatioDoctorCatogoryBean> personalInformatioDoctorCatogoryBeanList) {
        this.context = context;
        this.personalInformatioDoctorCatogoryBeanList = personalInformatioDoctorCatogoryBeanList;
    }

    @Override
    public VHItem onCreateViewHolder(ViewGroup parent, int viewType) {
        try {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_personal_information_step2_list_items, parent, false);
            return new VHItem(v);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    @Override
    public void onBindViewHolder(VHItem viewHolder, final int position) {

        try {

            viewHolder.speciality.setText(personalInformatioDoctorCatogoryBeanList.get(position).getSpeciality());
            viewHolder.qualification.setText(personalInformatioDoctorCatogoryBeanList.get(position).getQualification() + "(" + personalInformatioDoctorCatogoryBeanList.get(position).getYear() + ")");

            //Getting imagesBeanArrayList position images
            ArrayList<ImagesBean> imagesBeanArrayList = personalInformatioDoctorCatogoryBeanList.get(position).getImagesBeanList();

            //Set Horizental recyclerView  adapter
            HorizontalListAdapter horizontalListAdapter = new HorizontalListAdapter(context, imagesBeanArrayList);
            viewHolder.horizentalView.setHasFixedSize(true);
            viewHolder.horizentalView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
            viewHolder.horizentalView.setAdapter(horizontalListAdapter);
            viewHolder.horizentalView.setNestedScrollingEnabled(false);

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {

        return personalInformatioDoctorCatogoryBeanList.size();
    }

    class VHItem extends RecyclerView.ViewHolder {

        private TextView speciality, qualification;
        private RecyclerView horizentalView;

        public VHItem(View view) {

            super(view);

            speciality = (TextView) view.findViewById(R.id.speciality_textview);
            qualification = (TextView) view.findViewById(R.id.qualification_textview);
            horizentalView = (RecyclerView) view.findViewById(R.id.horizontal_recycler);
        }
    }

我的手机: -

enter image description here

1 个答案:

答案 0 :(得分:0)

在我的观察中,您在每个记录插入中传递imagesBeanArrayList的相同引用。添加新数组时尝试传递新数组。

private void addDoctorDetails() {
    try {
        personalInformatioDoctorCatogoryBeanArrayList.add(
                new PersonalInformatioDoctorCatogoryBean(
                         "Physician" + i, "MBBS" + i, "1985" + i, 
                         new ArrayList<ImagesBean>()));

        personalInformationRecyclerDoctorCatogoryAdapter.notifyDataSetChanged();
        i++;
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
}