RecyclerView中的可点击图标

时间:2016-10-13 20:28:46

标签: java android user-interface

我在这里理解模式时遇到了一些麻烦。我有一个视图和一个混合太多的适配器。我的逻辑在哪里出错?

尝试使图标与可点击标记类似。如果选中设置为true,则再次检查为false。 但是,即使将其设置为true /或false,视图也不会自动刷新。

public class MyPojo {

String title;
String desc;
int image;
boolean isChecked;

我的适配器类〜

  @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    TextView title =  (TextView) holder.mView.findViewById(R.id.textView);
    TextView desc =  (TextView) holder.mView.findViewById(R.id.textView2);
    final ImageView image =  (ImageView) holder.mView.findViewById(R.id.imageView);

            title.setText(pojos.get(position).getTitle());
            desc.setText(pojos.get(position).getDesc());
    if (!(pojos.get(position).isChecked())) {
        image.setImageResource(pojos.get(position).getImage());
    } else {
        image.setImageResource(R.mipmap.ic_launcher);
    }

    image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            image.setImageResource(R.mipmap.ic_launcher);
            //todo Where do I set my is clicked ?
            if (pojos.get(position).isChecked) {
                pojos.get(position).setChecked(false);
                //image.setImageResource(R.mipmap.ic_launcher);

            }else{
                pojos.get(position).setChecked(true);
                //image.setImageResource(pojos.get(position).getImage());


            }

        }
    });

}

2 个答案:

答案 0 :(得分:0)

您需要在setOnclickListener()课程中设置ViewHolder而不是onBindViewHolder()

答案 1 :(得分:0)

recyclelerView中的多个onClick事件:

 public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{

        public ImageView iconImageView;
        public TextView iconTextView;

        public MyViewHolder(final View itemView) {
            super(itemView);

            iconImageView = (ImageView) itemView.findViewById(R.id.myRecyclerImageView);
            iconTextView = (TextView) itemView.findViewById(R.id.myRecyclerTextView);
// set click event
            itemView.setOnClickListener(this);
            iconTextView.setOnClickListener(this);
// set long click event
            iconImageView.setOnLongClickListener(this);
        }

        // onClick Listener for view
        @Override
        public void onClick(View v) {

            if (v.getId() == iconTextView.getId()){
                Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
            }
        }


        //onLongClickListener for view
        @Override
        public boolean onLongClick(View v) {

            final AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
            builder.setTitle ("Hello Dialog")
                    .setMessage ("LONG CLICK DIALOG WINDOW FOR ICON " + String.valueOf(getAdapterPosition()))
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });

            builder.create().show();
            return true;
        }
    }

要获取单击的项目,您将匹配视图ID,即v.getId()== yourViewItem.getId()