将视图动态添加到RecyclerView

时间:2016-12-23 13:45:24

标签: java android listview android-recyclerview android-cardview

我一直在开发一个列表视图,在列表视图(卡片视图)的每一行上我有一个水平scrool视图,我在scrool视图上添加了一些视图(动态视图)(这是动态和每行视图的计数)是不同的) 但当我scrool向下或向上适配时在scrool视图上添加额外的视图(我的意思是例如它应该添加2个视图并且它添加但是当它下降或向上时它也添加了2个extara视图)

我不知道如何解决这个问题。

我的代码:

@Override
public void onBindViewHolder(SicknessResulteHolder holder, int position) {

    //Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
    /**********************************************************************************************************/

    List<String> user_symptoms = new ArrayList<String>();
    for (int i = 0 ; i<frag_recognize_1.list.size() ; i++){

        for (int j = 0 ; j< list.get(position).user_symptoms.size() ; j++){
            if (list.get(position).user_symptoms.get(j) == frag_recognize_1.list.get(i).getSymptoms_id()){
                user_symptoms.add(frag_recognize_1.list.get(i).getTitle());
            }
        }
    }
    /**********************************************************************************************************/


    if (list.get(position).addView){
        for (int i = 0 ; i < list.get(position).user_symptoms.size() ; i++){

            LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.symptoms_row,null,false);
            TextView textView = (TextView) linearLayout.findViewById(R.id.textView);
            textView.setText(String.valueOf(user_symptoms.get(i)));
            textView.setTextColor(Color.WHITE);

            holder.scrollSymptoms.addView(linearLayout);
        }
        list.get(position).setAddView(false);
    }





}

1 个答案:

答案 0 :(得分:0)

那是因为您在View Holder绑定时添加了视图。尝试在创建View holder时执行此操作。

@Override
public YourAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//your addView goes here
}