习题模块知识点

一、ListView

习题页面显示章节习题

习题详情页面显示习题的题目及选项,选择选项后判断选项是否正确

publicvoidonBindViewHolder(@NonNullfinalExerciseDetailAdapter.ViewHolderholder,finalintposition){//1.获取数据ExerciseDetaildetail=details.get(position);//2.给控件赋值holder.subject.setText(detail.getSubject());holder.tvA.setText(detail.getA());holder.tvB.setText(detail.getB());holder.tvC.setText(detail.getC());holder.tvD.setText(detail.getD());holder.ivA.setImageResource(R.drawable.ic_exercise_a);holder.ivB.setImageResource(R.drawable.ic_exercise_b);holder.ivC.setImageResource(R.drawable.ic_exercise_c);holder.ivD.setImageResource(R.drawable.ic_exercise_d);//3.设置监听,并处理事件(根据选项判断答案是否正确,显示相应的图标)holder.ivA.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){Stringpos=String.valueOf(position);if(selectedPods.contains(pos)){selectedPods.remove(pos);}else{selectedPods.add(pos);}onSelectListener.onSelectA(position,holder.ivA,holder.ivB,holder.ivC,holder.ivD);}});holder.ivB.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){Stringpos=String.valueOf(position);if(selectedPods.contains(pos)){selectedPods.remove(pos);}else{selectedPods.add(pos);}onSelectListener.onSelectB(position,holder.ivA,holder.ivB,holder.ivC,holder.ivD);}});holder.ivC.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){Stringpos=String.valueOf(position);if(selectedPods.contains(pos)){selectedPods.remove(pos);}else{selectedPods.add(pos);}onSelectListener.onSelectC(position,holder.ivA,holder.ivB,holder.ivC,holder.ivD);}});holder.ivD.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){Stringpos=String.valueOf(position);if(selectedPods.contains(pos)){selectedPods.remove(pos);}else{selectedPods.add(pos);}onSelectListener.onSelectD(position,holder.ivA,holder.ivB,holder.ivC,holder.ivD);}});}

二、Adapter和AdapterView

根据记录数,加载每个view

@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){ViewHolderholder;//1.加载xml布局,只需要加载一次if(convertView==null){holder=newViewHolder();convertView=LayoutInflater.from(context).inflate(R.layout.item_exercise,null);//2.获取控件holder.tvtvOder=convertView.findViewById(R.id.tv_order);holder.tvTitle=convertView.findViewById(R.id.tv_title);holder.tvSubTitle=convertView.findViewById(R.id.tv_sub_title);convertView.setTag(holder);}else{holder=(ViewHolder)convertView.getTag();}//3.加载数据finalExerciseexercise=getItem(position);if(exercise!=null){holder.tvtvOder.setText(String.valueOf(position+1));holder.tvtvOder.setBackgroundResource(R.drawable.circle);holder.tvTitle.setText(exercise.getTitle());holder.tvSubTitle.setText(exercise.getSubTitle());convertView.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){}});}returnconvertView;}

三、适配器

初始化控件、获取控件、设置布局

publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){// 1. 初始化数据initData();// 2. 获取控件Viewview=inflater.inflate(R.layout.fragment_recycler_view,container,false);RecyclerViewrecyclerView=view.findViewById(R.id.recycle_view);// 3. 设置布局和分割线LinearLayoutManagermanager=newLinearLayoutManager(container.getContext());manager.setOrientation(LinearLayoutManager.VERTICAL);recyclerView.setLayoutManager(manager);recyclerView.addItemDecoration(newDividerItemDecoration(container.getContext(),DividerItemDecoration.VERTICAL));// 4. 创建适配器RecyclerViewAdapteradapter=newRecyclerViewAdapter(exercises);// 5. 设置适配器recyclerView.setAdapter(adapter);// 6. 设置监听器adapter.setOnItemClickListener(newRecyclerViewAdapter.OnItemClickListener(){@OverridepublicvoidonItemClick(Viewview,intposition){Exerciseexercise=exercises.get(position);//跳转到相应的章节习题Intentintent=newIntent(getContext(),ExerciseDetailActivity.class);intent.putExtra("id",exercise.getId());//用于识别是哪个xml文件intent.putExtra("title",exercise.getTitle());//用于设置详情的标题栏getContext().startActivity(intent);}@OverridepublicvoidonItemLongClick(Viewview,intposition){}});returnview;}

四、RecyclerView控件

为RecyclerView准备一个适配器,新建RecyclerViewAdapter类,让适配器继承自RecyclerView.Adapter,并将泛型指定RecyclerViewAdapter.ViewHolder。

代码如下:

publicclassRecyclerViewAdapterextendsRecyclerView.Adapter{privateListexercises;staticclassViewHolderextendsRecyclerView.ViewHolder{TextViewtvtvOder,tvTitle,tvSubTitle;publicViewHolder(Viewview){super(view);tvtvOder=view.findViewById(R.id.tv_order);tvTitle=view.findViewById(R.id.tv_title);tvSubTitle=view.findViewById(R.id.tv_sub_title);}}publicRecyclerViewAdapter(ListexerList){exercises=exerList;}@NonNull@OverridepublicViewHolderonCreateViewHolder(@NonNullViewGroupparent,intviewType){Viewview=LayoutInflater.from(parent.getContext()).inflate(R.layout.item_exercise,parent,false);ViewHolderholder=newViewHolder(view);//设置Item点击的监听器view.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){}});returnholder;}@OverridepublicvoidonBindViewHolder(@NonNullfinalViewHolderholder,finalintposition){Exerciseexercise=exercises.get(position);holder.tvTitle.setText(exercise.getTitle());holder.tvtvOder.setText(String.valueOf(position+1));holder.tvSubTitle.setText(exercise.getSubTitle());//        //设置圆角背景的颜色//        GradientDrawable drawable=(GradientDrawable) holder.tvtvOder.getBackground();//        drawable.setColor(Color.parseColor(exercise.getBgColor()));//设置监听if(itemClickLisrener!=null){holder.itemView.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){itemClickLisrener.onItemClick(holder.itemView,position);}});holder.itemView.setOnLongClickListener(newView.OnLongClickListener(){@OverridepublicbooleanonLongClick(Viewv){itemClickLisrener.onItemLongClick(holder.itemView,position);returntrue;}});}}privateOnItemClickLisreneritemClickLisrener;publicvoidsetOnItemClickListener(OnItemClickLisrenerlistener){this.itemClickLisrener=listener;}//回调事件的接口publicinterfaceOnItemClickLisrener{voidonItemClick(Viewview,intposition);voidonItemLongClick(Viewview,intpositon);}@OverridepublicintgetItemCount(){returnexercises.size();}}

五、运行效果图

习题页面.png

你可能感兴趣的:(习题模块知识点)