public class StuExamExciseFillBlankAdapter extends RecyclerView.Adapter<StuExamExciseFillBlankAdapter.MyViewHolder> {
private int positon;
private final Context context;
private final Fragment fragment;
private onTextChangeListener mTextListener;
private List<ExamQuestionDetailBean.DataBean.AnswerListBean> examQuestionAnswers=new ArrayList<>();
public StuExamExciseFillBlankAdapter(Context context, Fragment fragment) {
this.context=context;
this.fragment = fragment;
}
public void setData(List<ExamQuestionDetailBean.DataBean.AnswerListBean> answerListBeanList) {
this.examQuestionAnswers=answerListBeanList;
notifyDataSetChanged();
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_stu_exam_fill_blank_list, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
positon = position;
holder.txtFillName.setText("填空题"+(position+1)+":");
//通过设置tag,防止position紊乱
holder.edit_answer.setTag(positon);
holder.edit_answer.setTextColor(context.getResources().getColor(R.color.color_ff333333));
if (holder instanceof MyViewHolder) {
//1、为了避免TextWatcher在第2步被调用,提前将他移除。
if (((MyViewHolder) holder).edit_answer.getTag() instanceof TextWatcher) {
((MyViewHolder) holder).edit_answer.removeTextChangedListener((TextWatcher) (((MyViewHolder) holder).edit_answer.getTag()));
}
// 第2步:移除TextWatcher之后,设置EditText的Text。
((MyViewHolder) holder).edit_answer.setText(examQuestionAnswers.get(position).getContent());
TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if(holder.edit_answer.hasFocus()){//判断当前EditText是否有焦点在
//通过接口回调将数据传递到Activity中
mTextListener.onTextChanged(holder.getLayoutPosition(),holder.edit_answer.getText().toString());
}
}
};
//设置EditText的焦点监听器判断焦点变化,当有焦点时addTextChangedListener,失去焦点时removeTextChangedListener
holder.edit_answer.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
holder.edit_answer.addTextChangedListener(watcher);
}else{
holder.edit_answer.removeTextChangedListener(watcher);
}
}
});
}
}
@Override
public int getItemCount() {
return examQuestionAnswers==null?0:examQuestionAnswers.size();
}
@Override
public int getItemViewType(int position){
return position;
}
class MyViewHolder extends RecyclerView.ViewHolder{
@BindView(R.id.txtFillName)
TextView txtFillName;//填空题一
@BindView(R.id.edit_answer)
EditText edit_answer;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
}
}
public interface onTextChangeListener{
void onTextChanged(int position,String content);
}
//设置自定义接口成员变量
public void setOnTextChangeListener(onTextChangeListener onTextChangeListener){
this.mTextListener=onTextChangeListener;
}
}
下面为回调的方法:
public List<ExamQuestionDetailBean.DataBean.AnswerListBean> answerListBeanList=new ArrayList<>();
exciseFillBlankAdapter.setOnTextChangeListener(new StuExamExciseFillBlankAdapter.onTextChangeListener() {
@Override
public void onTextChanged(int position, String content) {
answerListBeanList.get(position).setContent(content);
LogUtil.i("ContentResult","==="+position+"----"+content);
String examQuestionId = answerListBeanList.get(position).getQuestionId();
Intent intent = new Intent();
intent.putExtra("answerListBeanList", (Serializable) answerListBeanList);
intent.putExtra("examQuestionId",examQuestionId);
intent.putExtra("num",testId);
intent.putExtra("type", 4);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Constants.REFRESH_STUDENT_MOCK_FILL_BLANK_ANSWER_DATA);
context.sendBroadcast(intent);
}
});
|