习题模块所运用知识集合

  • ListView的使用
  • Adapter的概念及使用
  • RecyclerView
  • GirdView
  • Spinner

RecyclerView控件

  • ListView是Android中最常用的控件之一,可以通过手指上下滑动,滚动视图显示内容,用法相对其他控件比较复杂,不过它并不是完全没有缺点的,比如说如果我们不使用一些技巧来提升它的运行效率,那么ListView的性能就会非常差,还有扩展性也不够好,它只能实现数据纵向滚动效果,如果想实现横向滚动,ListView是做不到的。
  • 为此,Andriod提供了一个更强大的滚动控件——RecyclerView,它可以说是一个增强版的ListVIew,不仅可以轻松实现和它一样的效果,还优化了它各种不足之处,目前官方更推荐使用RecyclerView!
    首先让Android Studio自动帮我们创建好活动
    *想要使用这个控件,首先需要在项目的build.gradle中添加相应的依赖库才行


    image.png
  • 然后修改fragement_recycler_view.xml中的代码


    image.png
  • 接下来为RecyclerView准备一个适配器,新建RecyclerViewAdapter类,让适配器继承自RecyclerView.Adapter,并将泛型指定RecyclerViewAdapter.ViewHolder。代码如下:
public class RecyclerViewAdapter extends RecyclerView.Adapter {
    private List 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 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();
    }

}
  • 适配器准备好以后,我们就开始使用RecyclerView了,修改代码如下图:
public class ExerciseDetailActivity extends AppCompatActivity implements ExerciseDetailAdapter.OnSelectListener {
   private int id;
   private String title;
   private List details;

   private RecyclerView lvDetails;
   private ExerciseDetailAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_recycler_view);
        initData();
        initView();
    }
public void initData(){
      id =getIntent().getIntExtra("id",0);
      title=getIntent().getStringExtra("title");

      details=new ArrayList<>();
      try{
          InputStream is=getResources().getAssets().open("chapter"+id+".xml");
          details= IOUtils.getXmlContents(is);
          is.close();
      }catch (Exception e){
          e.printStackTrace();
      }

}
public void initView(){
    lvDetails=findViewById(R.id.recycle_view);
    LinearLayoutManager manager=new LinearLayoutManager(this);
    lvDetails.setLayoutManager(manager);
    adapter=new ExerciseDetailAdapter(details,this);
    lvDetails.setAdapter(adapter);

    //增加标题
//    TextView title=new TextView(this);
//    title.setTextColor(Color.parseColor("#000000"));
//    title.setText("一,选择题");
//    title.setPadding(10,15,0,0);
//    lvExercises.addHeaderView(title);

}
public void onSelectA(int position, ImageView ivA,ImageView ivB,ImageView ivC,ImageView ivD){
   ExerciseDetail detail=details.get(position);
   if(detail.getAnswer() !=1){
       detail.setSelect(1);
   }else{
       detail.setSelect(0);
   }
   switch (detail.getAnswer()){
       case 1:
           ivA.setImageResource(R.drawable.ic_exercise_answer_right);
           break;
       case 2:
           ivB.setImageResource(R.drawable.ic_exercise_answer_error);
           ivA.setImageResource(R.drawable.ic_exercise_answer_right);
           break;
       case 3:
           ivC.setImageResource(R.drawable.ic_exercise_answer_error);
           ivA.setImageResource(R.drawable.ic_exercise_answer_right);
           break;
       case 4:
           ivD.setImageResource(R.drawable.ic_exercise_answer_error);
           ivA.setImageResource(R.drawable.ic_exercise_answer_right);
           break;
   }

}

    @Override
    public void onSelectB(int position, ImageView ivA, ImageView ivB, ImageView ivC, ImageView ivD) {
        ExerciseDetail detail=details.get(position);
        if(detail.getAnswer() !=2){
            detail.setSelect(2);
        }else{
            detail.setSelect(0);
        }
        switch (detail.getAnswer()){
            case 1:
                ivA.setImageResource(R.drawable.ic_exercise_answer_error);
                ivB.setImageResource(R.drawable.ic_exercise_answer_right);
                break;
            case 2:
                ivB.setImageResource(R.drawable.ic_exercise_answer_right);
                break;
            case 3:
                ivC.setImageResource(R.drawable.ic_exercise_answer_error);
                ivB.setImageResource(R.drawable.ic_exercise_answer_right);
                break;
            case 4:
                ivD.setImageResource(R.drawable.ic_exercise_answer_error);
                ivB.setImageResource(R.drawable.ic_exercise_answer_right);
                break;
        }
    }

    @Override
    public void onSelectC(int position, ImageView ivA, ImageView ivB, ImageView ivC, ImageView ivD) {
        ExerciseDetail detail=details.get(position);
        if(detail.getAnswer() !=3){
            detail.setSelect(3);
        }else{
            detail.setSelect(0);
        }
        switch (detail.getAnswer()){
            case 1:
                ivA.setImageResource(R.drawable.ic_exercise_answer_right);
                ivB.setImageResource(R.drawable.ic_exercise_answer_error);
                break;
            case 2:
                ivA.setImageResource(R.drawable.ic_exercise_answer_error);
                ivC.setImageResource(R.drawable.ic_exercise_answer_right);
                break;
            case 3:
                ivC.setImageResource(R.drawable.ic_exercise_answer_right);
                break;
            case 4:
                ivD.setImageResource(R.drawable.ic_exercise_answer_error);
                ivC.setImageResource(R.drawable.ic_exercise_answer_right);
                break;
        }
    }

    @Override
    public void onSelectD(int position, ImageView ivA, ImageView ivB, ImageView ivC, ImageView ivD) {
        ExerciseDetail detail=details.get(position);
        if(detail.getAnswer() !=4){
            detail.setSelect(4);
        }else{
            detail.setSelect(0);
        }
        switch (detail.getAnswer()){
            case 1:
                ivA.setImageResource(R.drawable.ic_exercise_answer_error);
                ivD.setImageResource(R.drawable.ic_exercise_answer_right);
                break;
            case 2:
                ivA.setImageResource(R.drawable.ic_exercise_answer_error);
                ivD.setImageResource(R.drawable.ic_exercise_answer_right);
                break;
            case 3:
                ivB.setImageResource(R.drawable.ic_exercise_answer_error);
                ivD.setImageResource(R.drawable.ic_exercise_answer_right);
                break;
            case 4:
                ivD.setImageResource(R.drawable.ic_exercise_answer_right);
                break;
        }
    }
}
  • 现在可以运行一下程序了,效果如图:


    ListView运行效果.png
  • 可以看到,RecyclerView实现了和ListView几乎一模一样的效果,虽然代码量没有减少,但是逻辑上清晰了很多,RecyclerView还可以实现横向滚动和瀑布流布局哦,在后面的课程中会学习到GirdView。

你可能感兴趣的:(习题模块所运用知识集合)