一、效果
二、实现思路
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><LinearLayoutandroid:layout_width="@dimen/dp_640"android:layout_height="@dimen/dp_400"android:background="@color/pageBgColor"android:orientation="vertical"><!-- 返回模块 --><includeandroid:id="@+id/titleLyout"layout="@layout/chart_top_title_layout" /><!-- 内容 --><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="@dimen/dp_300"android:paddingTop="@dimen/dp_15"><TextViewandroid: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.RecyclerViewandroid: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" /><!-- 再听一遍 --><LinearLayoutandroid: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"><LinearLayoutandroid: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"><ImageViewandroid:layout_width="@dimen/dp_30"android:layout_height="@dimen/dp_25"android:src="@drawable/voice_pic" /></LinearLayout><TextViewandroid: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><TextViewandroid: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" /><TextViewandroid: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><!-- 底部进度布局 --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="@dimen/dp_60"android:orientation="vertical"><TextViewandroid: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" /><ProgressBarandroid: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><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="@dimen/dp_10"><!-- screnning_question_item --><TextViewandroid: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 {// handlerprivate HandlerUtils.HandlerHolder myHandler;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);ActivityUtil.getInstance().addActivity(ScreeningContentPage.this);}private int selectModuleId = 0;private int poolCodeType = 0;@Overrideprotected void handleIntent(Intent intent) {super.handleIntent(intent);selectModuleId = Integer.parseInt(intent.getSerializableExtra("selectModuleId").toString());poolCodeType = Integer.parseInt(intent.getSerializableExtra("poolCodeType").toString());Logger.d("======健康筛查内容=====" + selectModuleId);}@Overrideprotected int getLayoutResId() {return R.layout.screening_content_layout;}@Overrideprotected void initViewModel() {}@Overridepublic void handlerMessage(Message msg) {// 刷新列表Logger.d("======没有更多消息======" + questionPosition);}@Overrideprotected void bindViewModel() {mViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ScreeningContentViewmodle.class);}@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic void run() {Toast.makeText(ScreeningContentPage.this, "至少选择一个答案", Toast.LENGTH_SHORT).show();}});}}}});// 问答数据集合mViewModel.getScreeningContentBeanLivewDt().observe(this, new Observer<ScreeningContentBean>() {@Overridepublic 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() {@Overridepublic void onItemClick(View view, int postion) {answerQt(postion);}@Overridepublic void onItemLongClick(View view, int postion) {}});// 上一个mDataBinding.tvBack.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {backQuestion();}});// 重复播放ttsmDataBinding.repeatTts.setOnClickListener(new View.OnClickListener() {@Overridepublic 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>() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void run() {mDataBinding.tvCount.setText(questionPosition + "/" + sqAdapterBean.getData().getQuestionList().size());mDataBinding.progress.setProgress(questionPosition);sqAdapter.notifyDataSetChanged();}});myHandler.postDelayed(new Runnable() {@Overridepublic void run() {questionPosition = questionPosition - 1;runOnUiThread(new Runnable() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void run() {if (questionPosition + 1 >= sqAdapterBean.getData().getQuestionList().size()) {return;}runOnUiThread(new Runnable() {@Overridepublic 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() {@Overridepublic 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);}}}@Overrideprotected void onResume() {super.onResume();EventBus.getDefault().register(this);}@Overrideprotected void onStop() {super.onStop();EventBus.getDefault().unregister(this);}@Overrideprotected 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() {@Overridepublic 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);}@Overridepublic 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;}@Overridepublic 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() {@Overridepublic void onClick(View view) {//获取当前屏幕上的位置,避免插入后position不对int layoutPosition = holder.getLayoutPosition();onItemClickListner.onItemClick(holder.itemView, layoutPosition);}});//长按holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View view) {int layoutPosition = holder.getLayoutPosition();onItemClickListner.onItemLongClick(holder.itemView, layoutPosition);return false;}});}}@Overridepublic 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);}@Overridepublic 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);}}