我的recyclerview页面包含问题和答案字段。根据问题类型,我需要显示来自json的答案字段(例如,对于“描述”类型edittext,“true / false”类型然后切换按钮等。我是什么尝试过我已经在布局中创建了所有类型的答案字段并使其可见性变得很高,并且根据json(答案类型)我使它们变得可见。根据这种方法,如果我在recyclerview项目中有多个edittext,则插入的值是重叠的,输入的滚动值正在被清除。所以帮助我如何设置组件以及如何检索插入的答案。
MY recyclerview适配器类:
public class CheckSuiteFormAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<AuditToday> formList;
Context context;
FrameLayout frameLayout;
private boolean hasLoadButton = true;
private final int TITLE = 0;
private final int LOAD_MORE = 1;
MyViewHolder headerHolder;
CustomWatcher textWatcher;
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView question_no, question ;
public EditText answer;
public Button btn_attach,btn_submit;
public CheckBox cb1,cb2,cb3,cb4,cb5,cb6;
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);
btn_submit = (Button) view.findViewById(R.id.btn_submit);
//alignment settings for checkboxes start ------->
FrameLayout.LayoutParams params4 = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
params4.setMargins(60,100,0,0);
FrameLayout.LayoutParams params5 = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
params5.setMargins(240,100,0,0);
FrameLayout.LayoutParams params6 = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
params6.setMargins(400,100,0,0);
FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
params1.setMargins(60,40,0,0);
FrameLayout.LayoutParams params2 = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
params2.setMargins(240,40,0,0);
FrameLayout.LayoutParams params3 = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
params3.setMargins(400,40,0,0);
//alignment settings for checkboxes end ------->
//alignment for toggle button
FrameLayout.LayoutParams params_toggle = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
params_toggle.setMargins(60,100,0,0);
cb1 = new CheckBox(context);
cb1.setText("option1" );
cb1.setLayoutParams(params1);
frameLayout.addView(cb1);
cb2 = new CheckBox(context);
cb2.setText("option2" );
cb2.setLayoutParams(params2);
frameLayout.addView(cb2);
cb3 = new CheckBox(context);
cb3.setText("option3" );
cb3.setLayoutParams(params3);
frameLayout.addView(cb3);
cb4 = new CheckBox(context);
cb4.setText("option4" );
cb4.setLayoutParams(params4);
frameLayout.addView(cb4);
cb5 = new CheckBox(context);
cb5.setText("option5" );
cb5.setLayoutParams(params5);
frameLayout.addView(cb5);
cb6 = new CheckBox(context);
cb6.setText("option6" );
cb6.setLayoutParams(params6);
frameLayout.addView(cb6);
toggleButton = new ToggleButton(context);
toggleButton.setLayoutParams (params_toggle);
frameLayout.addView(toggleButton);
//visibility based on values
answer.setVisibility(View.GONE);
toggleButton.setVisibility(View.GONE);
//text_date.setVisibility(View.GONE);
cb1.setVisibility(View.GONE);
cb2.setVisibility(View.GONE);
cb3.setVisibility(View.GONE);
cb4.setVisibility(View.GONE);
cb5.setVisibility(View.GONE);
cb6.setVisibility(View.GONE);
}
}
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);
}
}
HashMap<Integer,String> hsAllAns = new HashMap<>();
public class CustomWatcher implements TextWatcher {
private int position;
public CustomWatcher(int position) {
this.position = position;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// int position = (int)editText.getTag(R.id.id_ans_text);
Log.d("SUB", position+" "+s);
hsAllAns.put(position,s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if(holder instanceof MyViewHolder) {
headerHolder = (MyViewHolder) holder;
AuditToday auditToday = formList.get(position);
headerHolder.question_no.setText(auditToday.getQuestion_no());
headerHolder.question.setText(auditToday.getQuestion());
String ans_type = auditToday.getAnswer();
textWatcher = new CustomWatcher(position);
if (ans_type.equals("desc")) {
showDescriptionField();
} else if (ans_type.equals("chk")) {
showCheckBoxField();
} else if (ans_type.equals("num")) {
headerHolder.answer.setVisibility(View.VISIBLE);
headerHolder.cb1.setVisibility(View.GONE);
headerHolder.cb2.setVisibility(View.GONE);
headerHolder.cb3.setVisibility(View.GONE);
headerHolder.cb4.setVisibility(View.GONE);
headerHolder.cb5.setVisibility(View.GONE);
headerHolder.cb6.setVisibility(View.GONE);
headerHolder.toggleButton.setVisibility(View.GONE);
headerHolder.answer.setInputType(InputType.TYPE_CLASS_NUMBER);
headerHolder.answer.setTag(position);
headerHolder.answer.addTextChangedListener(textWatcher);
} else if(ans_type.equals("toggle")){
headerHolder.toggleButton.setVisibility(View.VISIBLE);
headerHolder.cb1.setVisibility(View.GONE);
headerHolder.cb2.setVisibility(View.GONE);
headerHolder.cb3.setVisibility(View.GONE);
headerHolder.cb4.setVisibility(View.GONE);
headerHolder.cb5.setVisibility(View.GONE);
headerHolder.cb6.setVisibility(View.GONE);
headerHolder.answer.setVisibility(View.GONE);
}
} 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 ();
Log.d("Results :"," " +new Gson().toJson(hsAllAns) );
}
});
}
}
public void showDescriptionField(){
headerHolder.answer.setVisibility(View.VISIBLE);
headerHolder.answer.addTextChangedListener(textWatcher);
headerHolder.cb1.setVisibility(View.GONE);
headerHolder.cb2.setVisibility(View.GONE);
headerHolder.cb3.setVisibility(View.GONE);
headerHolder.cb4.setVisibility(View.GONE);
headerHolder.cb5.setVisibility(View.GONE);
headerHolder.cb6.setVisibility(View.GONE);
headerHolder.toggleButton.setVisibility(View.GONE);
headerHolder.answer.setInputType(InputType.TYPE_CLASS_TEXT);
}
public void showCheckBoxField(){
headerHolder.cb1.setVisibility(View.VISIBLE);
headerHolder.cb2.setVisibility(View.VISIBLE);
headerHolder.cb3.setVisibility(View.VISIBLE);
headerHolder.cb4.setVisibility(View.VISIBLE);
headerHolder.cb5.setVisibility(View.VISIBLE);
headerHolder.cb6.setVisibility(View.VISIBLE);
headerHolder.answer.setVisibility(View.GONE);
headerHolder.toggleButton.setVisibility(View.GONE);
}
@Override
public int getItemCount() {
// return formList.size();
if (hasLoadButton) {
return formList.size() +1;
} else {
return formList.size();
}
}
@Override
public int getItemViewType(int position) {
if (position >= formList.size()) {
return LOAD_MORE;
} else {
return TITLE;
}
}
}