在Android RecyclerView中实施多项选择 [英] Implementing multi selection in Android RecyclerView

查看:61
本文介绍了在Android RecyclerView中实施多项选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些关于Multi/Single选择的帮助.在此处找到了我想要的东西,因为它很简单. 我正在使用一个GridLayoutManager适配器中有90多个项目,一个CardView具有一个TextView和一个ImageView,同时使用了

I need some help with Multi/Single selection. Found what I was looking for here, because of its simplicity. I'm using a GridLayoutManager I have over 90 items in my adapter, a CardView with a TextView and an ImageView, while using the procedure described in the post.

当我向下滚动时选择一项或多项时,其他项似乎"被选中,因为背景复制了,但未选中它们. 尝试将setOnClickListeneronBindViewHolder以及MyViewHolder类中的内容都放置在它们中,我得到的行为相同.向下滚动时,似乎选择了其他项目. 在适配器中使用了notifyItemChanged(position)notifyDataSetChanged(),尽管setSelected正常工作,但背景完全没有改变. 另外,我在RecyclerView设置中使用了setHasFixedSize(true).

When I select one, or more than one item, as I scroll down, other items "seems" to be selected because the background replicates, but they are not selected. Tried placing the setOnClickListener, in onBindViewHolder and also in MyViewHolder class and in both of them I get the same behaviour. When scrolling down other items seems to be selected. Used notifyItemChanged(position) and notifyDataSetChanged() in the adapter, but the background does not change at all, although the setSelected works properly. Also I used setHasFixedSize(true) in the RecyclerView setup.

onBindViewHolder

@Override
public void onBindViewHolder(MyViewHolder myViewHolder, final int position) {

    PatternImages currentPattern = patternImages.get(position);
    myViewHolder.setData(currentPattern, position);
    myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            v.setSelected(!v.isSelected());
            if (v.isSelected()) {
                v.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimaryHighLight));

            } else {
                v.setBackgroundColor(Color.WHITE);

            }
            notifyItemChanged(position);
        }
    });
}

型号

public class PatternImages {

    private int imageId, imageName;
    private boolean isSelected;

    public PatternImages(int imageId, int imageName, boolean isSelected) {

        this.imageId = imageId;
        this.imageName = imageName;
        this.isSelected = isSelected;
    }
    public int getImageId() {

        return imageId;
    }
    public void setImageId(int imageId) {

        this.imageId = imageId;
    }
    public int getImageName() {

        return imageName;
    }
    public void setImageName(int imageName) {

        this.imageName = imageName;
    }
    public boolean isSelected() {

        return isSelected;
    }
    public void setSelected(boolean selected) {

        isSelected = selected;
    }

RecyclerView设置

RecyclerView Setup

 private void setUpPatternsRecyclerView() {

    RecyclerView recyclerPatternsView = (RecyclerView) findViewById(R.id.pattern_image_recycler_view);
    PatternImageAdapter adapter = new PatternImageAdapter(this, patternImages);
    recyclerPatternsView.setAdapter(adapter);
    ColumnQty columnQty = new ColumnQty(this, R.layout.item_image_pattern_cardview);
    GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), columnQty.calculateNoOfColumns());
    recyclerPatternsView.setHasFixedSize(true);
    recyclerPatternsView.setLayoutManager(gridLayoutManager);
    recyclerPatternsView.setItemAnimator(new DefaultItemAnimator());
    recyclerPatternsView.addItemDecoration(new GridSpacing(columnQty.calculateSpacing()));

}

setData方法

public void setData(PatternImages currentPattern, int position) {

    this.position = position;
    patternName.setText(context.getString(currentPattern.getImageName()));
    patternName.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/ElMessiri-SemiBold.ttf"));
    patternImage.setBackgroundResource(currentPattern.getImageId());
    if (position == 0 || position == 1) {
        animationDrawable = (AnimationDrawable) patternImage.getBackground();
        animationDrawable.start();
    }


}

推荐答案

顾名思义,RecyclerView回收视图.这意味着,一旦视图从屏幕上滚动出来,便可以重复使用.

A RecyclerView as its name suggests, recycles views. That means that once a view scrolls off screen, it can be reused.

在重用视图之前,它仍然包含自上次使用以来的所有设置.例如,如果它包含TextView,则该TextView仍将其Text属性设置为上次显示时的值.

Before a view is reused, it still contains all of the settings from the last time it was used. For example, if it contains a TextView, that TextView will still have its Text property set to whatever it was the last time it was displayed.

某些项目似乎"被选中的原因是,您从屏幕上滚动出来的所选视图现在已被重新使用,而您尚未取消选中它们.

The reason that some items "seem" to be selected is because your selected views that have scrolled off screen are now being reused and you have not unselected them.

在您的OnBindViewHolder方法中,您需要将所有视图重置"为默认值.在这种情况下,将关闭"用于显示视图的任何方法.

In your OnBindViewHolder method, you need to "reset" all the views back to their defaults. In this case that would be to "turn off" whatever method you use to make a view appear selected.

例如:

@Override
public void onBindViewHolder(MyViewHolder myViewHolder, final int position) {

    final PatternImages currentPattern = patternImages.get(position);

    myViewHolder.setData(currentPattern, position);
    myViewHolder.itemView.setBackgroundColor(currentPattern.isSelected() ?R.color.Red: R.color.WHITE); // choose your colors

    myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            currentPattern.setSelected(!currentPattern.isSelected())
            notifyItemChanged(position);
        }
    });
}

基本上,每次绑定时,都会根据模型中的相关属性将背景色设置为选定或未选定状态.

Essentially, every time you bind, you set the background colour to the selected or non selected state based on the relevant property in your model.

这篇关于在Android RecyclerView中实施多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆