简体   繁体   中英

RecyclerView first item is always selected

I tried every solution from all sites, but I can't fix this problem. As shown in the code I'm trying to add onClickListener to the RecyclerView, but the first item is always checked. I also tried to make the default checkedPosition to -1 , but the first item of the list is unselectable. All other items work properly except for the first item.

public class JsonListAdapter extends RecyclerView.Adapter <RecyclerView.ViewHolder> {
    private static final int TYPE = -1;
    private final List <Object> listRecyclerItem;
    private int checkedPosition = 0;

    public JsonListAdapter(Context context, List <Object> listRecyclerItem) {
        this.context = context;
        this.listRecyclerItem = listRecyclerItem;
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder {

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            //findViewById here
        }
    }

    @NonNull@Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        switch (i) {
        case TYPE:

        default:
            //LayoutInflater here
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        int viewType = getItemViewType(i);
        switch (viewType) {
        case TYPE:
        default:
            //add data to list here
        }

        row_linear.setOnClickListener(view - >{
            checkedPosition = i;
            notifyDataSetChanged();
        });
        radioButton.setOnClickListener(view - >{
            checkedPosition = i;
            notifyDataSetChanged();
        });
        if (checkedPosition == i) {
            radioButton.setChecked(true);
        } else {
            radioButton.setChecked(false);
        }
    }
}

when you get your list, initiate the list items with with extra Boolean , and create getter and setter:

private boolean isChecked = false;

public boolean getIsChecked() {
    return isChecked;
}

public void setIsChecked(boolean checked) {
    isChecked = checked;
}

Then in your adapter modify your condition to check both:

if (listRecyclerItem.get(i).getIsChecked() && checkedPosition == i)

Next, you'll need to uncheck an item as soon as another item is checked (assuming you're making it single select):

        radioButton.setOnClickListener(view- > {

            listRecyclerItem.get(i).setIsChecked(false); // to uncheck previous item
            checkedPosition=i;
            listRecyclerItem.get(i).setIsChecked(true); // to check current item

            notifyDataSetChanged();
            });

            if (listRecyclerItem.get(i).isChecked&&checkedPosition==i) {
                radioButton.setChecked(true);
            } else {
                radioButton.setChecked(false);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM