如何在RecyclerView中的CardView上使用OnClickListner?

时间:2017-01-04 23:47:01

标签: android android-recyclerview android-cardview

我尝试做这样的事情: enter image description here

我的问题,当我点击我的ImageView时,我的CardView已展开,但第n CardView也展开了,其中展开了CardView我点击了。我不明白为什么我的onClick方法也适用于其他CardView

我的适配器

public class CardsViewAdapter extends RecyclerView.Adapter<CardsViewAdapter.ViewHolder> {
    private Game[] mDataset;
    int rotationAngle = 0;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public ImageView imageView;
        public LinearLayout test2;
        public TextView test3;
        boolean isPopupVisible;

        public ViewHolder(View v) {
            super(v);
            mTextView = (TextView) v.findViewById(R.id.text_cards);
            imageView = (ImageView) v.findViewById(R.id.item_description_game_more);
            test2 = (LinearLayout) v.findViewById(R.id.popup_layout);
            test3 = (TextView) v.findViewById(R.id.test_view);
            isPopupVisible = false;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public CardsViewAdapter(Game[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public CardsViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cards_resume_game, parent, false);
        // set the view's size, margins, paddings and layout parameters
        //...

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        //TODO : complete
        final int pos = position;
        holder.mTextView.setText(String.valueOf(mDataset[position].getId_game()));
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("POS","actual pos = "+pos);
                holder.test3.setText("Position : "+pos);
                // Perform action on click
                if (holder.isPopupVisible) {
                    holder.isPopupVisible = false;

                    ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation",rotationAngle, rotationAngle + 180);
                    anim.setDuration(500);
                    anim.start();
                    rotationAngle += 180;
                    rotationAngle = rotationAngle%360;

                    CardsAnimationHelper.collapse(holder.test2);

                } else {
                    holder.isPopupVisible = true;

                    ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation",rotationAngle, rotationAngle + 180);
                    anim.setDuration(500);
                    anim.start();
                    rotationAngle += 180;
                    rotationAngle = rotationAngle%360;

                    CardsAnimationHelper.expand(holder.test2);
                }
            }
        });
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

3 个答案:

答案 0 :(得分:2)

我认为你已经有了Game课程。只需在名为expanded的类中添加新属性即可。

public Class Game {
    public int id;
    public String description;

    // Add an extra attribute to track the checked status
    // By default, set the checked status to false
    public boolean expanded = false;
}

现在,由于您现在拥有跟踪展开状态的属性,因此您可以轻松地执行此类操作。

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    final int pos = position;
    holder.mTextView.setText(String.valueOf(mDataset[position].id));

    // Set the expanded or collapsed mode here
    if(mDataset[position].expanded) expandView(holder.test2);
    else collapseView(holder.test2);

    // Now set the onClickListener like this
    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.test3.setText("Position : "+pos);

            // Animate the imageView here
            animateImageView(holder.imageView);

            // Toggle the expanded attribute value
            if(mDataset[position].expanded) mDataset[position].expanded = false;
            else mDataset[position].expanded = true;

            // Now call notifyDataSetChanged to make the change to effect
            refreshList();
        }
    });
}

// Extra functions inside your adapter class to improve readability
private void collapseView(View view) {
    CardsAnimationHelper.collapse(view);
}

private void expandView(View view) {
    CardsAnimationHelper.expand(view);
}

private void animateImageView(ImageView imageView) {
    ObjectAnimator anim = ObjectAnimator.ofFloat(imageView, "rotation",rotationAngle, rotationAngle + 180);
    anim.setDuration(animationDuration);
    anim.start();
    rotationAngle += 180;
    rotationAngle = rotationAngle % 360;
}

long animationDuration = 500;

private void refreshList() {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            notifyDataSetChanged();
        }
    }, animationDuration);
}

答案 1 :(得分:0)

发生这种情况的原因是因为列表中的第n个项目正在重新使用您展开的视图。这就是视图持有者所使用的,重用视图而不是每次都创建视图。

为了解决这个问题,请确保再次检查视图的扩展状态onBindViewHolder,而不仅仅是onClick。

答案 2 :(得分:0)

将PopupVisible作为变量添加到Game类中,并且onBindViewHolder仅在game.getPopupVisible为true时展开cardview:

public class CardsViewAdapter extends RecyclerView.Adapter<CardsViewAdapter.ViewHolder> {
    private Game[] mDataset;
    int rotationAngle = 0;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public ImageView imageView;
        public LinearLayout test2;
        public TextView test3;

        public ViewHolder(View v) {
            super(v);
            mTextView = (TextView) v.findViewById(R.id.text_cards);
            imageView = (ImageView) v.findViewById(R.id.item_description_game_more);
            test2 = (LinearLayout) v.findViewById(R.id.popup_layout);
            test3 = (TextView) v.findViewById(R.id.test_view);

        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public CardsViewAdapter(Game[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public CardsViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cards_resume_game, parent, false);
        // set the view's size, margins, paddings and layout parameters
        //...

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        //TODO : complete
        final int pos = position;
        Game game=mDataset[position];
        holder.mTextView.setText(String.valueOf(mDataset[position].getId_game()));
        if (game.getPopupVisible()) {
                    ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation",rotationAngle, rotationAngle + 180);
                    anim.setDuration(500);
                    anim.start();
                    rotationAngle += 180;
                    rotationAngle = rotationAngle%360;
                    CardsAnimationHelper.collapse(holder.test2);}
        else....
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("POS","actual pos = "+pos);
                holder.test3.setText("Position : "+pos);
                // Perform action on click
                if (game.getPopupVisible()) {
                    game.setPopupVisible(false);

                    ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation",rotationAngle, rotationAngle + 180);
                    anim.setDuration(500);
                    anim.start();
                    rotationAngle += 180;
                    rotationAngle = rotationAngle%360;

                    CardsAnimationHelper.collapse(holder.test2);

                } else {
                   game.setPopupVisible(true);

                    ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation",rotationAngle, rotationAngle + 180);
                    anim.setDuration(500);
                    anim.start();
                    rotationAngle += 180;
                    rotationAngle = rotationAngle%360;

                    CardsAnimationHelper.expand(holder.test2);
                }
            }
        });
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}