嗨,我试图在我的recyclerview中实现onclick操作,但它不起作用

时间:2015-10-28 10:45:15

标签: android android-recyclerview recycler-adapter

  

您好我在我的回收站视图中尝试onclick,在我的原始项目中实现onclick之前,我创建了用于测试回收器视图及其时钟的虚拟项目。我的虚拟项目工作正常,我可以单击回收器视图然后可以将细节显示到另一个activity.But我在我的原始项目中使用相同的代码,但我试图点击回收器视图上的项目,没有响应。我发现我的原始项目中的错误   log cat:/ RecyclerView:没有连接适配器;跳过布局   我的虚拟项目代码是

public class RVAdapter
    extendsRecyclerView.Adapter<RVAdapter.PersonViewHolder> {     public
    List<Person> persons= Collections.emptyList(); //     private
    Context mContext;  private LayoutInflater inflater;



            ImageLoader mImageLoader = AppController.getInstance().getImageLoader();

            private static Context context;


            public RVAdapter(Context context, List<Person> persons) {
                this.persons = persons;
                  this.context = context;
                inflater = LayoutInflater.from(context);
            }

            @Override
            public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview,
        parent, false);
                PersonViewHolder pvh = new PersonViewHolder(v);


                return pvh;
            }

            @Override
            public void onBindViewHolder(PersonViewHolder holder, int postions) {

                Person person = persons.get(postions);
                holder.phoneName.setText(person.getKitchen_accesries());
                holder.phonePrice.setText(person.getKitchen_price());
                holder.itemView.setTag(person);

           if (mImageLoader == null)
                   mImageLoader = AppController.getInstance().getImageLoader();
              holder.thumbnail.setImageUrl(person.getThumbnail(), mImageLoader);


            }


            @Override
            public int getItemCount() {
                if (persons != null) {
                    return persons.size();
                }
                return 0;
            }


            @Override
            public void onAttachedToRecyclerView(RecyclerView recyclerView) {
                super.onAttachedToRecyclerView(recyclerView);
            }

            public static class PersonViewHolder extends RecyclerView .ViewHolder
                    implements View
                    .OnClickListener  {
                CardView cv;
                TextView phoneName;
                TextView phonePrice;
                public List<Person> persons;


                NetworkImageView thumbnail;

                PersonViewHolder(View itemView) {
                    super(itemView);

                    cv = (CardView) itemView.findViewById(R.id.card_view);
                    phoneName = (TextView) itemView.findViewById(R.id.phone_name);
                    phonePrice = (TextView) itemView.findViewById(R.id.phone_price);
                    thumbnail = (NetworkImageView) itemView.findViewById(R.id.thumbnail);
                    itemView.setOnClickListener(this); //
                }

                @Override
                public void onClick(View view ) {

                    Intent intent = new Intent(context,ViewActivity.class);

                    Person person =(Person)view.getTag();

                    String strUrl= person.getThumbnail();
                    String product_name=person.getKitchen_accesries();
                    String product_price=person.getKitchen_price();
                    intent.putExtra("TV",product_name);
                    intent.putExtra("Price",product_price);
                    intent.putExtra("Image",strUrl);
                    context.startActivity(intent);

                }


            }


        }

我的原始项目代码是:

        public class Mobile_recycle_Adapter extends        RecyclerView.
Adapter   <Mobile_recycle_Adapter.PersonViewHolder>   {
                List<Details> mobile_details= Collections.emptyList();
                private LayoutInflater inflater;
                 private static Context context;

    ImageLoader mImageLoader = AppController.getInstance().getImageLoader();


 public Mobile_recycle_Adapter( Context context,List<Details>
mobile_details) {
        this.mobile_details = mobile_details;
     this.context = context;
     inflater = LayoutInflater.from(context);
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview,
parent, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder holder, int i) {

         Details mob_details=mobile_details.get(i);
        holder.phoneName.setText(mob_details.getPhone_name());
        holder.phonePrice.setText(mob_details.getPhone_price());


//
        if (mImageLoader == null)
            mImageLoader = AppController.getInstance().getImageLoader();
        holder.thumbnail.setImageUrl(mob_details.getPhone_image(), mImageLoader);
    }

    @Override
    public int getItemCount() {
        if (mobile_details != null) {
            return mobile_details.size();
        }
        return 0;
    }



    public static class PersonViewHolder extends RecyclerView.ViewHolder implements View
            .OnClickListener{
        CardView cv;
        TextView phoneName;
        TextView phonePrice;
      NetworkImageView thumbnail;

        PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.card_view);
            phoneName = (TextView) itemView.findViewById(R.id.phone_name);
            phonePrice = (TextView) itemView.findViewById(R.id.phone_price);
            thumbnail=(NetworkImageView) itemView.findViewById(R.id.thumbnail);
        }
        @Override
        public void onClick(View view ) {

            Intent intent = new Intent(context,Details_view.class);

            Details person =(Details)view.getTag();

            String strUrl= person.getPhone_image();
            String product_name=person.getPhone_name();
            String product_price=person.getPhone_price();
            intent.putExtra("Phone",product_name);
            intent.putExtra("Price",product_price);
            intent.putExtra("Image",strUrl);
            context.startActivity(intent);

        }
    }

2 个答案:

答案 0 :(得分:1)

您缺少在您的原始项目代码中编写itemView.setOnClickListener(this);

您应该像在虚拟项目中那样将此行添加到PersonViewHolder()构造函数

答案 1 :(得分:0)

在您的构造函数cv.setOnClickListener(this);

中添加PersonViewHolder