Reputation: 1100
I have recycler view, which contains questions and answer fields .The answer fields are created programmatically as per the answer type. If the answer is "description" type then I am providing a edittext and if "true/false" type then toggle and etc .But while scrolling down and up all the dynamically created answer fields are overlapping.
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;
}
}
}
Upvotes: 0
Views: 674
Reputation: 5780
When you are setting visibility for your answer fields in onBindViewHolder you should set visibility GONE for other fields. For example, instead of:
headerHolder.answer.setVisibility(View.VISIBLE);
Do:
headerHolder.answer.setVisibility(View.VISIBLE);
headerHolder.cb.setVisibility(View.GONE);
headerHolder.toggleButton.setVisibility(View.GONE);
It may seem redundant (you are hiding the views in a constructor of MyViewHolder) but RyclerView will reuse your viewholder instances. When it's recycled for other type of answer, previously displayed view will remain visible.
Upvotes: 1