• RecyclerView 的点击事件


    Fruit.Class  FruitAdapter.Class   activity_main.Xml和地址里的一样

    http://blog.csdn.net/cccrunner/article/details/70502494


    FruitAdapter.Class

    package com.example.deligence.demo10;
    
    public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{
       List<Fruit> mfruitList;
    
        static class ViewHolder extends RecyclerView.ViewHolder {
            View fruitView; //保存子项最外层布局的实例
            ImageView fruitImage;
            TextView fruitName;
            public ViewHolder(View View) {
                super(View);
                fruitView = View;
                fruitImage = (ImageView) View.findViewById(R.id.fruit_image);
                fruitName = (TextView) View.findViewById(R.id.fruit_name);
            }
        }
    
        public FruitAdapter(List<Fruit> mfruitList) {
            this.mfruitList = mfruitList;
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit,parent,false);
            final ViewHolder holder = new ViewHolder(view);
            //最外层布局注册了监听事件
            holder.fruitView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition(); //先从点击事件中获得用户点击的position
                    Fruit fruit = mfruitList.get(position);     //然后通过position拿到Fruit的实例
                    Toast.makeText(v.getContext(),"You click the View"+fruit.getFruitName(),Toast.LENGTH_SHORT).show();
                    //再使用toast弹出两种不同的内容以示区别
                }
            });
            //imageview注册了监听事件
            holder.fruitImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    Fruit fruit = mfruitList.get(position);
                    Toast.makeText(v.getContext(),"You click the View"+fruit.getFruitName(),Toast.LENGTH_SHORT).show();
                }
            });
    
            return holder;
        }
    
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            Fruit fruit = mfruitList.get(position);
            holder.fruitImage.setImageResource(fruit.getFruitImageId());
            holder.fruitName.setText(fruit.getFruitName());
        }
    
        @Override
        public int getItemCount() {
            return mfruitList.size();
        }
    
    
    }
    



  • 相关阅读:
    sql中的不常见查询
    sql中的常见报错;
    wcf_first
    均方值-数学期望-方差
    转:模块度(Modularity)与Fast Newman算法讲解与代码实现
    转:社区发现评估指标-NMI
    转:模块度(Modularity)与Fast Newman算法讲解与代码实现
    转:聚类评价指标
    转:聚类︱python实现 六大 分群质量评估指标(兰德系数、互信息、轮廓系数)
    转:用K-Means聚类分析做客户分群
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11782034.html
Copyright © 2020-2023  润新知