android 实现答题功能
一、效果
二、实现思路
1、界面实现
实现起来其实不难,首先我们可以看到,界面是由答题进度、题目、选项ABCD组成,现在就是要考虑实现方式,答题进度可以使用Textview+progressbar实现,题目直接使用Textview,选项ABCD使用listview或者recyview,这里选择后者recyview。
2、题目切换逻辑实现
ABCD单选多选可以用使用对象标记不同状态,题目替换可以使用Handler切换到集合下一个数据, ABCD选项列表也是重新赋值。
三、代码
1、题目布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
</data>
<LinearLayout
android:layout_width="@dimen/dp_640"
android:layout_height="@dimen/dp_400"
android:background="@color/pageBgColor"
android:orientation="vertical">
<!-- 返回模块 -->
<include
android:id="@+id/titleLyout"
layout="@layout/chart_top_title_layout" />
<!-- 内容 -->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_300"
android:paddingTop="@dimen/dp_15">
<TextView
android:id="@+id/questionTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_30"
android:layout_marginRight="@dimen/dp_30"
android:gravity="center|left"
android:text=""
android:textColor="@color/white"
android:textSize="@dimen/sp_20"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/ryList"
android:layout_width="@dimen/dp_385"
android:layout_height="wrap_content"
android:layout_below="@id/questionTitle"
android:layout_marginLeft="@dimen/dp_30"
android:layout_marginTop="@dimen/dp_10" />
<!-- 再听一遍 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="@dimen/dp_60"
android:layout_marginRight="@dimen/dp_30"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="@+id/repeat_tts"
android:layout_width="@dimen/dp_60"
android:layout_height="@dimen/dp_60"
android:background="@drawable/voice_bd_th"
android:gravity="center">
<ImageView
android:layout_width="@dimen/dp_30"
android:layout_height="@dimen/dp_25"
android:src="@drawable/voice_pic" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_5"
android:text="再听一遍"
android:textColor="@color/white"
android:textSize="@dimen/sp_14" />
</LinearLayout>
<TextView
android:id="@+id/tvBack"
android:layout_width="@dimen/dp_100"
android:layout_height="@dimen/dp_30"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="@dimen/dp_240"
android:layout_marginBottom="@dimen/dp_15"
android:background="@drawable/start_screening_next_qs"
android:gravity="center"
android:visibility="gone"
android:text="上一题"
android:textColor="@color/white"
android:textSize="@dimen/sp_14" />
<TextView
android:id="@+id/tvNext"
android:layout_width="@dimen/dp_100"
android:layout_height="@dimen/dp_30"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="@dimen/dp_120"
android:layout_marginBottom="@dimen/dp_15"
android:background="@drawable/start_screening_next_qs"
android:gravity="center"
android:text="下一题"
android:textColor="@color/white"
android:textSize="@dimen/sp_14" />
</RelativeLayout>
<!-- 底部进度布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_60"
android:orientation="vertical">
<TextView
android:id="@+id/tvCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_20"
android:layout_marginTop="@dimen/dp_5"
android:text="0/12"
android:textColor="@color/white"
android:textSize="@dimen/sp_14" />
<ProgressBar
android:id="@+id/progress"
style="@style/screeningSeekBar"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_5"
android:layout_marginLeft="@dimen/dp_10"
android:layout_marginTop="@dimen/dp_10"
android:layout_marginRight="@dimen/dp_5"
android:max="50"
android:progress="10" />
</LinearLayout>
</LinearLayout>
</layout>
2、abcd答案列表布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
</data>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_10">
<!-- screnning_question_item -->
<TextView
android:id="@+id/tvContent"
android:layout_width="@dimen/dp_160"
android:layout_height="@dimen/dp_40"
android:background="@drawable/unselect_screnning_qs_item"
android:gravity="center"
android:text="-"
android:textSize="@dimen/sp_14" />
</RelativeLayout>
</layout>
3、主界面java代码
package csu.xiaoya.robotApp.ui.fragment_healthy_housekeeper.healthy_screen.screeningcn;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import com.google.gson.Gson;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;
import java.util.OptionalInt;
import java.util.stream.IntStream;
import csu.xiaoya.robotApp.R;
import csu.xiaoya.robotApp.XiaoYaApp;
import csu.xiaoya.robotApp.base.BaseActivity;
import csu.xiaoya.robotApp.databinding.ScreeningContentLayoutBinding;
import csu.xiaoya.robotApp.glob.GlobalConstant;
import csu.xiaoya.robotApp.ui.activity.homepage.familydIm.FamilyDoctorActivity;
import csu.xiaoya.robotApp.ui.fragment_healthy_housekeeper.healthy_screen.HealthyScreeningActivity;
import csu.xiaoya.robotApp.ui.fragment_healthy_housekeeper.healthy_screen.screenbean.ReplySuccessBean;
import csu.xiaoya.robotApp.ui.fragment_healthy_housekeeper.healthy_screen.screenbean.ScreeningContentBean;
import csu.xiaoya.robotApp.ui.fragment_healthy_housekeeper.healthy_screen.screenbean.SubmitSreeningBean;
import csu.xiaoya.robotApp.ui.fragment_healthy_housekeeper.healthy_screen.screening_finish.ScreeningFinishPage;
import csu.xiaoya.robotApp.ui.pagemanage.beans.JumpBean;
import csu.xiaoya.robotApp.ui.pagemanage.constant.JumpPageCode;
import csu.xiaoya.robotApp.util.ActivityUtil;
import csu.xiaoya.robotApp.util.GlideUtil;
import csu.xiaoya.robotApp.util.HandlerUtils;
import csu.xiaoya.robotApp.util.dialog.LoginDialogUtil;
import csu.xiaoya.robotApp.util.log.Logger;
/**
* 健康
* 筛查内容
*/
public class ScreeningContentPage extends BaseActivity<ScreeningContentLayoutBinding, ScreeningContentViewmodle> implements HandlerUtils.OnReceiveMessageListener {
// handler
private HandlerUtils.HandlerHolder myHandler;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityUtil.getInstance().addActivity(ScreeningContentPage.this);
}
private int selectModuleId = 0;
private int poolCodeType = 0;
@Override
protected void handleIntent(Intent intent) {
super.handleIntent(intent);
selectModuleId = Integer.parseInt(intent.getSerializableExtra("selectModuleId").toString());
poolCodeType = Integer.parseInt(intent.getSerializableExtra("poolCodeType").toString());
Logger.d("======健康筛查内容=====" + selectModuleId);
}
@Override
protected int getLayoutResId() {
return R.layout.screening_content_layout;
}
@Override
protected void initViewModel() {
}
@Override
public void handlerMessage(Message msg) {
// 刷新列表
Logger.d("======没有更多消息======" + questionPosition);
}
@Override
protected void bindViewModel() {
mViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ScreeningContentViewmodle.class);
}
@Override
protected void init() {
// 标记为健康筛查答题模式
GlobalConstant.asrModule = 1;
myHandler = new HandlerUtils.HandlerHolder(this);
initTitleLayout();
mViewModel.getQuestionList(selectModuleId + "");
// 初始化适配器
initAdapter();
initDataListner();
}
/**
* 答案选择
* 适配器
*/
private ScreeningQuestionAdapater sqAdapter;
private ScreeningContentBean sqAdapterBean;
private void initAdapter() {
sqAdapter = new ScreeningQuestionAdapater();
mDataBinding.ryList.setAdapter(sqAdapter);
mDataBinding.ryList.setLayoutManager(new GridLayoutManager(this, 2));
}
/**
* 数据
* 监听
*/
private int questionPosition = 0;
private void initDataListner() {
// 切换下一题
mDataBinding.tvNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isCheck = false;//答案是否选择
if (questionPosition <= sqAdapterBean.getData().getQuestionList().size()) {
if (questionPosition == 0) {
questionPosition = 0;
}
for (int i = 0; i < sqAdapterBean.getData().getQuestionList().get(questionPosition).getAnswerList().size(); i++) {
if (sqAdapterBean.getData().getQuestionList().get(questionPosition).getAnswerList().get(i).getStatus().equals("1")) {
isCheck = true;
break;
}
}
if (sqAdapterBean.getData().getQuestionList().get(questionPosition).getQuestionType().equals("2")) {
isCheck = true;
}
// 已选种答案可以切换下一题
if (isCheck) {
// 选择效果切换
nextQuestion();
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ScreeningContentPage.this, "至少选择一个答案", Toast.LENGTH_SHORT).show();
}
});
}
}
}
});
// 问答数据集合
mViewModel.getScreeningContentBeanLivewDt().observe(this, new Observer<ScreeningContentBean>() {
@Override
public void onChanged(ScreeningContentBean screeningContentBean) {
questionPosition = 0;
sqAdapterBean = screeningContentBean;
sqAdapter.adapaterAddData(screeningContentBean.getData().getQuestionList().get(0).getAnswerList());
//
if (XiaoYaApp.isAIUiFail()) {
Toast.makeText(ScreeningContentPage.this, "语音未授权", Toast.LENGTH_SHORT).show();
} else {
if (sqAdapterBean.getData().getQuestionList().get(0).getQuestionType().equals("2")) {
XiaoYaApp.setOnlyPlayTTs(true);
}
// 语音播报
if (XiaoYaApp.getAiuiRecognizerService() != null) {
XiaoYaApp.getAiuiRecognizerService().playTts("" + screeningContentBean.getData().getQuestionList().get(0).getQuestionText());
}
}
// 数据显示
mDataBinding.questionTitle.setText("" + screeningContentBean.getData().getQuestionList().get(0).getQuestionText());
// 计数
mDataBinding.tvCount.setText("0/" + screeningContentBean.getData().getQuestionList().size());
mDataBinding.progress.setMax(screeningContentBean.getData().getQuestionList().size());
mDataBinding.progress.setProgress(0);
sqAdapter.notifyDataSetChanged();
// Logger.LogD("健康筛查内容", "==获取题库==" + new Gson().toJson(screeningContentBean));
}
});
// 答案列表选择监听
sqAdapter.setOnItemClickListner(new ScreeningQuestionAdapater.OnItemClickListner() {
@Override
public void onItemClick(View view, int postion) {
answerQt(postion);
}
@Override
public void onItemLongClick(View view, int postion) {
}
});
// 上一个
mDataBinding.tvBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
backQuestion();
}
});
// 重复播放tts
mDataBinding.repeatTts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (XiaoYaApp.isAIUiFail()) {
Toast.makeText(ScreeningContentPage.this, "语音未授权", Toast.LENGTH_SHORT).show();
} else {
if (sqAdapterBean.getData().getQuestionList().get(questionPosition).getQuestionType().equals("2")) {
XiaoYaApp.setOnlyPlayTTs(true);
}
// 语音播报
if (XiaoYaApp.getAiuiRecognizerService() != null) {
XiaoYaApp.getAiuiRecognizerService().playTts("" + sqAdapterBean.getData().getQuestionList().get(questionPosition).getQuestionText());
}
}
}
});
// 提交完成
mViewModel.getMtReplySuccess().observe(this, new Observer<ReplySuccessBean>() {
@Override
public void onChanged(ReplySuccessBean replySuccessBean) {
if (replySuccessBean != null) {
Intent intent = new Intent(ScreeningContentPage.this, ScreeningFinishPage.class);
startActivity(intent);
finish();
LoginDialogUtil.getInstance().closeLoadingDialog();
} else {
LoginDialogUtil.getInstance().closeLoadingDialog();
Toast.makeText(ScreeningContentPage.this, "提交失败", Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* 回答问题
*/
private void answerQt(int postion) {
if (questionPosition <= sqAdapterBean.getData().getQuestionList().size()) {
if (questionPosition >= 0) {
mDataBinding.tvBack.setVisibility(View.VISIBLE);
} else {
mDataBinding.tvBack.setVisibility(View.GONE);
}
// 选择效果切换
if (Integer.parseInt(sqAdapterBean.getData().getQuestionList().get(questionPosition).getQuestionType()) == 1) {//多选);
if (sqAdapterBean.getData().getQuestionList().get(questionPosition).getAnswerList().get(postion).getStatus().equals("0")) {
sqAdapterBean.getData().getQuestionList().get(questionPosition).getAnswerList().get(postion).setStatus("1");
} else {
sqAdapterBean.getData().getQuestionList().get(questionPosition).getAnswerList().get(postion).setStatus("0");
}
runOnUiThread(new Runnable() {
@Override
public void run() {
sqAdapter.notifyDataSetChanged();
}
});
} else {
// 单选移除已选
for (int i = 0; i < sqAdapterBean.getData().getQuestionList().get(questionPosition).getAnswerList().size(); i++) {
sqAdapterBean.getData().getQuestionList().get(questionPosition).getAnswerList().get(i).setStatus("0");
}
// 重新选择
if (sqAdapterBean.getData().getQuestionList().get(questionPosition).getAnswerList().get(postion).getStatus().equals("0")) {
sqAdapterBean.getData().getQuestionList().get(questionPosition).getAnswerList().get(postion).setStatus("1");
} else {
sqAdapterBean.getData().getQuestionList().get(questionPosition).getAnswerList().get(postion).setStatus("0");
}
runOnUiThread(new Runnable() {
@Override
public void run() {
sqAdapter.notifyDataSetChanged();
}
});
nextQuestion();
}
}
}
/**
* 临时缓存
*/
private SubmitSreeningBean submitSreeningBean = new SubmitSreeningBean();
// 问题集合
private List<SubmitSreeningBean.QuestionList> questionList = new ArrayList<>();
private void saveScreeningDataList(ScreeningContentBean sqAdBean) {
questionList.clear();
submitSreeningBean.setPoolCode(poolCodeType);
for (int x = 0; x < sqAdBean.getData().getQuestionList().size(); x++) {
// 问题对象赋值
SubmitSreeningBean.QuestionList qsBean = new SubmitSreeningBean.QuestionList();
qsBean.setCreateBy(sqAdBean.getData().getQuestionList().get(x).getCreateBy());
qsBean.setUpdateBy(sqAdBean.getData().getQuestionList().get(x).getUpdateBy());
qsBean.setUpdateTime(sqAdBean.getData().getQuestionList().get(x).getUpdateTime());
qsBean.setQuestionId(sqAdBean.getData().getQuestionList().get(x).getQuestionId());
qsBean.setGroupId(sqAdBean.getData().getQuestionList().get(x).getGroupId());
qsBean.setQuestionType(sqAdBean.getData().getQuestionList().get(x).getQuestionType());
qsBean.setQuestionStemType(sqAdBean.getData().getQuestionList().get(x).getQuestionStemType());
qsBean.setQuestionText(sqAdBean.getData().getQuestionList().get(x).getQuestionText());
qsBean.setSort(sqAdBean.getData().getQuestionList().get(x).getSort());
qsBean.setStatus(sqAdBean.getData().getQuestionList().get(x).getStatus());
qsBean.setDelFlag(sqAdBean.getData().getQuestionList().get(x).getDelFlag());
// c存答案
List<SubmitSreeningBean.QuestionList.AnswerListDTO> answerListDTO = new ArrayList<>();
for (int y = 0; y < sqAdBean.getData().getQuestionList().get(x).getAnswerList().size(); y++) {
if (sqAdBean.getData().getQuestionList().get(x).getAnswerList().get(y).getStatus().equals("1")) {
// 答案对象赋值
SubmitSreeningBean.QuestionList.AnswerListDTO aswerBean = new SubmitSreeningBean.QuestionList.AnswerListDTO();
aswerBean.setCreateTime(sqAdBean.getData().getQuestionList().get(x).getAnswerList().get(y).getCreateTime());
aswerBean.setUpdateTime(sqAdBean.getData().getQuestionList().get(x).getAnswerList().get(y).getUpdateTime());
aswerBean.setAnswerId(sqAdBean.getData().getQuestionList().get(x).getAnswerList().get(y).getAnswerId());
aswerBean.setQuestionId(sqAdBean.getData().getQuestionList().get(x).getAnswerList().get(y).getQuestionId());
aswerBean.setAnswerType(sqAdBean.getData().getQuestionList().get(x).getAnswerList().get(y).getAnswerType());
aswerBean.setAnswerText(sqAdBean.getData().getQuestionList().get(x).getAnswerList().get(y).getAnswerText());
aswerBean.setAnswerScore(sqAdBean.getData().getQuestionList().get(x).getAnswerList().get(y).getAnswerScore());
aswerBean.setSort(sqAdBean.getData().getQuestionList().get(x).getAnswerList().get(y).getSort());
aswerBean.setStatus(sqAdBean.getData().getQuestionList().get(x).getAnswerList().get(y).getStatus());
aswerBean.setDelFlag(sqAdBean.getData().getQuestionList().get(x).getAnswerList().get(y).getDelFlag());
answerListDTO.add(aswerBean);
}
}
// 有时题目为提示类型,是则不提交
if (answerListDTO.size() > 0) {
qsBean.setAnswerList(answerListDTO);
// 单个问题数据保存
questionList.add(qsBean);
}
}
submitSreeningBean.setQuestionList(questionList);
// 提交给服务端
mViewModel.submitQuestionList(submitSreeningBean);
// Logger.LogD("questionList", "======健康筛查00=====" + new Gson().toJson(submitSreeningBean));
}
/**
* 上一题
*/
private void backQuestion() {
if (questionPosition > 0) {
if (questionPosition > 1) {
mDataBinding.tvBack.setVisibility(View.VISIBLE);
} else {
mDataBinding.tvBack.setVisibility(View.GONE);
}
// 统计显示当前进度
runOnUiThread(new Runnable() {
@Override
public void run() {
mDataBinding.tvCount.setText(questionPosition + "/" + sqAdapterBean.getData().getQuestionList().size());
mDataBinding.progress.setProgress(questionPosition);
sqAdapter.notifyDataSetChanged();
}
});
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
questionPosition = questionPosition - 1;
runOnUiThread(new Runnable() {
@Override
public void run() {
mDataBinding.questionTitle.setText("" + sqAdapterBean.getData().getQuestionList().get(questionPosition).getQuestionText());
sqAdapter.adapaterAddData(sqAdapterBean.getData().getQuestionList().get(questionPosition).getAnswerList());
sqAdapter.notifyDataSetChanged();
}
});
}
}, 200);
}
}
/**
* 切换
* 下一题
*/
private void nextQuestion() {
// 统计显示当前进度
runOnUiThread(new Runnable() {
@Override
public void run() {
mDataBinding.tvCount.setText(questionPosition + 1 + "/" + sqAdapterBean.getData().getQuestionList().size());
mDataBinding.progress.setProgress(questionPosition + 1);
}
});
if (questionPosition < sqAdapterBean.getData().getQuestionList().size() - 1) {//切换题目
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (questionPosition + 1 >= sqAdapterBean.getData().getQuestionList().size()) {
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mDataBinding.questionTitle.setText("" + sqAdapterBean.getData().getQuestionList().get(questionPosition + 1).getQuestionText());
sqAdapter.adapaterAddData(sqAdapterBean.getData().getQuestionList().get(questionPosition + 1).getAnswerList());
sqAdapter.notifyDataSetChanged();
}
});
questionPosition = questionPosition + 1;
voiceTTs();
}
}, 300);
} else {
LoginDialogUtil.getInstance().showLoadingDialog(this);
saveScreeningDataList(sqAdapterBean);
}
}
/**
* 问答题目
* 语音播报
*/
private void voiceTTs() {
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
// 语音播报
if (XiaoYaApp.isAIUiFail()) {
Toast.makeText(ScreeningContentPage.this, "语音未授权", Toast.LENGTH_SHORT).show();
} else {
if (sqAdapterBean.getData().getQuestionList().get(questionPosition).getQuestionType().equals("2")) {
XiaoYaApp.setOnlyPlayTTs(true);
}
// 语音播报
if (XiaoYaApp.getAiuiRecognizerService() != null) {
XiaoYaApp.getAiuiRecognizerService().playTts("" + sqAdapterBean.getData().getQuestionList().get(questionPosition).getQuestionText());
}
}
}
}, 800);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void asrQuestioneAnswer(JumpBean jump) {
for (int i = 0; i < sqAdapterBean.getData().getQuestionList().get(questionPosition).getAnswerList().size(); i++) {
if (jump.getConn().contains(sqAdapterBean.getData().getQuestionList().get(questionPosition).getAnswerList().get(i).getAnswerText())) {
Logger.d("==========" + jump.getConn() + "==" + questionPosition);
answerQt(i);
}
}
}
@Override
protected void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}
@Override
protected void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 标记为页面跳转模式
GlobalConstant.asrModule = 0;
}
/**
* 初始化
* 健康
* 标题布局
*/
private void initTitleLayout() {
mDataBinding.titleLyout.tvTitle.setText("返回");
mDataBinding.titleLyout.familyHead.setVisibility(View.GONE);
mDataBinding.titleLyout.backArrow.setVisibility(View.VISIBLE);
mDataBinding.titleLyout.bluth.setVisibility(View.INVISIBLE);
GlideUtil.loadImageWithImage(mDataBinding.titleLyout.backArrow, R.drawable.back_arrow);
//返回上一步
mDataBinding.titleLyout.backArrowMd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
4、abcd是列表
package csu.xiaoya.robotApp.ui.fragment_healthy_housekeeper.healthy_screen.screeningcn;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import csu.xiaoya.robotApp.R;
import csu.xiaoya.robotApp.ui.fragment_healthy_housekeeper.healthy_screen.screenbean.ScreeningContentBean;
/**
* 健康筛查a b c d选择
* 选项适配器
*/
public class ScreeningQuestionAdapater extends RecyclerView.Adapter<ScreeningQuestionAdapater.ViewHodler> {
private List<ScreeningContentBean.DataDTO.QuestionListDTO.AnswerListDTO> dataList;
public ScreeningQuestionAdapater() {
dataList = new ArrayList<>();
}
/**
* 点击就监听
*/
public interface OnItemClickListner {
void onItemClick(View view, int postion);
void onItemLongClick(View view, int postion);
}
private OnItemClickListner onItemClickListner;
/**
* 点击监听
*/
public void setOnItemClickListner(OnItemClickListner listner) {
this.onItemClickListner = listner;
}
public void adapaterAddData(List<ScreeningContentBean.DataDTO.QuestionListDTO.AnswerListDTO> dataList) {
this.dataList.clear();
this.dataList.addAll(dataList);
}
@Override
public ViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.question_item_layout, parent, false);
ViewHodler hodler = new ViewHodler(view);
return hodler;
}
@Override
public void onBindViewHolder(final ViewHodler holder, final int position) {
if (dataList.get(position).getStatus().equals("0")) {
holder.textView.setBackgroundResource(R.drawable.unselect_screnning_qs_item);
} else {
holder.textView.setBackgroundResource(R.drawable.select_screnning_qs_item);
}
holder.textView.setText(dataList.get(position).getAnswerText());
//设置点击响应
if (onItemClickListner != null) {
//点击
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//获取当前屏幕上的位置,避免插入后position不对
int layoutPosition = holder.getLayoutPosition();
onItemClickListner.onItemClick(holder.itemView, layoutPosition);
}
});
//长按
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
int layoutPosition = holder.getLayoutPosition();
onItemClickListner.onItemLongClick(holder.itemView, layoutPosition);
return false;
}
});
}
}
@Override
public int getItemCount() {
return dataList != null ? dataList.size() : 0;
}
/**
* 插入数据
*/
public void addData(int pos) {
// dataList.add(pos);
dataList.add("Insert One");
// notifyItemInserted(pos);
}
/**
* 移除数据
*/
public void deleData(int pos) {
dataList.remove(pos);
notifyItemRemoved(pos);
}
/**
* viewHolder
*/
public static class ViewHodler extends RecyclerView.ViewHolder {
TextView textView;
public ViewHodler(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.tvContent);
}
}
}
5、handler工具
package csu.xiaoya.robotApp.util;
import android.os.Handler;
import android.os.Message;
import java.lang.ref.WeakReference;
import csu.xiaoya.robotApp.util.log.Logger;
public class HandlerUtils {
private HandlerUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
public static class HandlerHolder extends Handler {
WeakReference<OnReceiveMessageListener> mListenerWeakReference;
/**
* 使用必读:推荐在Activity或者Activity内部持有类中实现该接口,不要使用匿名类,可能会被GC
*
* @param listener 收到消息回调接口
*/
public HandlerHolder(OnReceiveMessageListener listener) {
mListenerWeakReference = new WeakReference<>(listener);
}
@Override
public void handleMessage(Message msg) {
Logger.d("==没有更多消息=="+(mListenerWeakReference != null)+"===="+(mListenerWeakReference.get() != null));
if (mListenerWeakReference != null && mListenerWeakReference.get() != null) {
mListenerWeakReference.get().handlerMessage(msg);
}
}
}
/**
* 收到消息回调接口
*/
public interface OnReceiveMessageListener {
void handlerMessage(Message msg);
}
}