android中的Recycler视图中的动态组件是否重叠?

时间:2017-06-27 16:08:03

标签: android android-recyclerview recycler-adapter

我有回收站视图,其中包含问题和答案字段。答案字段按照答案类型以编程方式创建。如果答案是"描述"然后我提供一个edittext,如果" true / false"键入然后切换等。但在向下滚动时,所有动态创建的答案字段都是重叠的。

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

        private List<AuditToday> formList;
        Context context;
        FrameLayout frameLayout;
        FrameLayout.LayoutParams date_param;
        int mYear,mMonth,mDay;
        HashMap<String,String> hsAllAns;
        private boolean hasLoadButton = true;
        private final int TITLE = 0;
        private final int LOAD_MORE = 1;
        AuditToday auditToday;
        public class MyViewHolder extends RecyclerView.ViewHolder{
            public TextView question_no, question ;
            public EditText answer;
            public Button btn_attach,btn_submit;
            public  CheckBox cb;
            public ToggleButton toggleButton;



            public MyViewHolder(View view) {
                super(view);
                context = view.getContext();
                question_no = (TextView) view.findViewById(R.id.id_qs_no);
                question = (TextView) view.findViewById(R.id.id_qs);
               // answer = (EditText) view.findViewById(R.id.id_ans_text);
                btn_attach = (Button) view.findViewById(R.id.btn_attachment);
                frameLayout = (FrameLayout) view.findViewById(R.id.id_frame_layout);


                //create view dynamically start ------------>

                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.WRAP_CONTENT,
                        FrameLayout.LayoutParams.WRAP_CONTENT
                );
                params.setMargins(30,100,0,0);
                FrameLayout.LayoutParams params_toggle = new FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.WRAP_CONTENT,
                        FrameLayout.LayoutParams.WRAP_CONTENT
                );
                params_toggle.setMargins(60,100,0,0);
                date_param = new FrameLayout.LayoutParams(50,30);

                answer = new EditText(context);
                answer.setLayoutParams(params);
                frameLayout.addView(answer);
                cb = new CheckBox(context);
                cb.setText("Dynamic Checkbox " );
                cb.setLayoutParams(params);
                frameLayout.addView(cb);

                toggleButton = new ToggleButton(context);
                toggleButton.setLayoutParams (params_toggle);

                frameLayout.addView(toggleButton);

                //visibility based on values
                answer.setVisibility(View.GONE);
                cb.setVisibility(View.GONE);
                toggleButton.setVisibility(View.GONE);
                //text_date.setVisibility(View.GONE);
                hsAllAns = new HashMap<>();
                //create view dynamically end ------------>
            }
        }
        public CheckSuiteFormAdapter(List<AuditToday> formList) {
            this.formList = formList;
        }

        @Override
        public RecyclerView.ViewHolder  onCreateViewHolder(ViewGroup parent, int viewType) {
            if(viewType == TITLE) {
                View itemView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.form_list_row, parent, false);
                return new MyViewHolder(itemView);
            }else if (viewType == LOAD_MORE) {
                return new FooterViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.checksuite_form_submit, parent, false));
            } else {
                return null;
                }
        }


        class FooterViewHolder extends RecyclerView.ViewHolder {
            Button btn;

            public FooterViewHolder (View itemView) {
                super (itemView);
                this.btn = (Button) itemView.findViewById (R.id.btn_submit);
            }
        }




        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

            if(holder instanceof MyViewHolder) {
                MyViewHolder headerHolder = (MyViewHolder) holder;
                auditToday = formList.get(position);
                headerHolder.question_no.setText(auditToday.getQuestion_no());
                headerHolder.question.setText(auditToday.getQuestion());
                String ans_type = auditToday.getAnswer();
                if (ans_type.equals("desc")) {
                    headerHolder.answer.setVisibility(View.VISIBLE);
                    String response_desc = headerHolder.answer.getText().toString();
                    Log.d("SUBMIT","response_desc :"+response_desc);
                  //  hsAllAns.put(auditToday.getQuestion_no(), response_desc);
                } else if (ans_type.equals("chk")) {
                    headerHolder.cb.setVisibility(View.VISIBLE);
                    boolean responce_b = headerHolder.cb.isChecked();
                    String res_bol = String.valueOf(responce_b);
                    Log.d("SUBMIT","res_bol :"+res_bol);
                    //hsAllAns.put(auditToday.getQuestion_no(), res_bol);

                } else if (ans_type.equals("num")) {

                    headerHolder.answer.setVisibility(View.VISIBLE);
                    headerHolder.answer.setInputType(InputType.TYPE_CLASS_NUMBER);

                    String response_num = headerHolder.answer.getText().toString();
                    Log.d("SUBMIT","response_num :"+response_num);
                   // hsAllAns.put(auditToday.getQuestion_no(), response_num);

                } else if(ans_type.equals("toggle")){
                    headerHolder.toggleButton.setVisibility(View.VISIBLE);
                    boolean responce_b = headerHolder.toggleButton.isChecked();
                    String res_bol = String.valueOf(responce_b);
                    Log.d("SUBMIT","res_bol :"+res_bol);
                   // hsAllAns.put(auditToday.getQuestion_no(), res_bol);
                }

            } else if(holder instanceof FooterViewHolder) {
                FooterViewHolder footer = (FooterViewHolder) holder;
                footer.btn.setOnClickListener (new View.OnClickListener () {
                    @Override
                    public void onClick (View view) {
                        Toast.makeText (context, "Clicked Header", Toast.LENGTH_SHORT).show ();
                        Gson gson = new Gson();
                        String json = gson.toJson(hsAllAns);
                       Log.d("SUBMIT","hs value :"+json);
                       // hsAllAns.clear();

                    }
                });
            }

        }


        public boolean isHasLoadButton() {
            return hasLoadButton;
        }

        public void setHasLoadButton(boolean hasLoadButton) {
            this.hasLoadButton = hasLoadButton;
            notifyDataSetChanged();
        }
        @Override
        public int getItemCount() {
           // return formList.size();
            if (hasLoadButton) {
                return formList.size() +1;
            } else {
                return formList.size();
            }
        }
        @Override
        public int getItemViewType(int position) {
            Log.d("sss",position+"a"+getItemCount());
            if (position >= formList.size()) {
                return LOAD_MORE;
            } else {
                return TITLE;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

在onBindViewHolder中为答案字段设置可见性时,应为其他字段设置可见性GONE。 例如,而不是:

headerHolder.answer.setVisibility(View.VISIBLE);

执行:

headerHolder.answer.setVisibility(View.VISIBLE);
headerHolder.cb.setVisibility(View.GONE);
headerHolder.toggleButton.setVisibility(View.GONE);

它可能看起来多余(您将视图隐藏在MyViewHolder的构造函数中),但RyclerView将重用您的viewholder实例。当它被回收用于其他类型的答案时,之前显示的视图将保持可见。