一、ListView
- 习题页面显示章节习题
- 习题详情页面显示习题的题目及选项,选择选项后判断选项是否正确
public void onBindViewHolder(@NonNull final ExerciseDetailAdapter.ViewHolder holder, final int position) {
//1.获取数据
ExerciseDetail detail = 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(new View.OnClickListener() {
@Override
public void onClick(View v) {
String pos=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(new View.OnClickListener(){
@Override
public void onClick(View view) {
String pos=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(new View.OnClickListener(){
@Override
public void onClick(View view) {
String pos=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(new View.OnClickListener(){
@Override
public void onClick(View view) {
String pos=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
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//1.加载xml布局,只需要加载一次
if(convertView ==null){
holder=new ViewHolder();
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.加载数据
final Exercise exercise=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(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
return convertView;
}
三、适配器
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 1. 初始化数据
initData();
// 2. 获取控件
View view = inflater.inflate(R.layout.fragment_recycler_view, container, false);
RecyclerView recyclerView = view.findViewById(R.id.recycle_view);
// 3. 设置布局和分割线
LinearLayoutManager manager = new LinearLayoutManager(container.getContext());
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
recyclerView.addItemDecoration(new DividerItemDecoration(container.getContext(),
DividerItemDecoration.VERTICAL));
// 4. 创建适配器
RecyclerViewAdapter adapter = new RecyclerViewAdapter(exercises);
// 5. 设置适配器
recyclerView.setAdapter(adapter);
// 6. 设置监听器
adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Exercise exercise = exercises.get(position);
//跳转到相应的章节习题
Intent intent = new Intent(getContext(), ExerciseDetailActivity.class);
intent.putExtra("id", exercise.getId());//用于识别是哪个xml文件
intent.putExtra("title", exercise.getTitle());//用于设置详情的标题栏
getContext().startActivity(intent);
}
@Override
public void onItemLongClick(View view, int position) {
}
});
return view;
}
四、RecyclerView控件
- 为RecyclerView准备一个适配器,新建RecyclerViewAdapter类,让适配器继承自RecyclerView.Adapter,并将泛型指定RecyclerViewAdapter.ViewHolder。
代码如下:
public class RecyclerViewAdapter<V extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<Exercise> exercises;
static class ViewHolder extends RecyclerView.ViewHolder{
TextView tvtvOder,tvTitle,tvSubTitle;
public ViewHolder(View view){
super(view);
tvtvOder= view.findViewById(R.id.tv_order);
tvTitle= view.findViewById(R.id.tv_title);
tvSubTitle= view.findViewById(R.id.tv_sub_title);
}
}
public RecyclerViewAdapter(List<Exercise> exerList){
exercises=exerList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_exercise , parent ,false);
ViewHolder holder = new ViewHolder(view);
//设置Item点击的监听器
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
return holder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
Exercise exercise= 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(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemClickLisrener.onItemClick(holder.itemView,position);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
itemClickLisrener.onItemLongClick(holder.itemView,position);
return true;
}
});
}
}
private OnItemClickLisrener itemClickLisrener;
public void setOnItemClickListener(OnItemClickLisrener listener){
this.itemClickLisrener=listener;
}
//回调事件的接口
public interface OnItemClickLisrener{
void onItemClick(View view,int position);
void onItemLongClick(View view,int positon);
}
@Override
public int getItemCount() {
return exercises.size();
}
}
五、运行效果图