1.ListView的使用
2.Adapter的概念及使用
3.RecyclerView
4.GirdView
5.Spinner
一、ListView的使用
1.1习题页面显示章节习题
1.2习题详情页面显示习题的题目及选项,选择选项后判断选项是否正确
```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);
}
});
}```
二、RecyclerView控件
2.1ListView是Android中最常用的控件之一,可以通过手指上下滑动,滚动视图显示内容,用法相对其他控件比较复杂,不过它并不是完全没有缺点的,比如说如果我们不使用一些技巧来提升它的运行效率,那么ListView的性能就会非常差,还有扩展性也不够好,它只能实现数据纵向滚动效果,如果想实现横向滚动,ListView是做不到的。
2.2为此,Andriod提供了一个更强大的滚动控件——RecyclerView,它可以说是一个增强版的ListVIew,不仅可以轻松实现和它一样的效果,还优化了它各种不足之处,目前官方更推荐使用RecyclerView!首先让Android Studio自动帮我们创建好活动想要使用这个控件,首先需要在项目的build.gradle中添加相应的依赖库
2.3然后修改fragement_recycler_view.xml中的代码
2.4接下来为RecyclerView准备一个适配器,新建RecyclerViewAdapter类,让适配器继承自RecyclerView.Adapter,并将泛型指定RecyclerViewAdapter.ViewHolder。代码如下:
public class RecyclerViewAdapter
private List
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
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();
}
}
三、效果图如下: