Problem of forgetting when scrolling the android check box

Asked 2 years ago, Updated 2 years ago, 31 views

If you check the check box that appears in the list and scroll down, A certain check box will be checked and released for a moment. (Animation)

Every time you scroll, you set the data and then update it to the data note. There is no problem with the check value itself, but every time I scroll, the checked part and the above phenomenon occur, so I forget The phenomenon occurs.

Running on 6.0.1 Nexus. Please advise me if there is a solution or link to the same phenomenon. I'm looking for it, but it's not coming out well.

android

2022-09-22 21:38

3 Answers

We did a simple test on Nexus 6P (6.0.1). I couldn't find the problem you mentioned. I attached the test code and the result video. I think it would be better to create a minimum sample that works normally with the code you are working on and compare it with the code that is causing the problem to find the problem.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler);
        recyclerView.setAdapter(new Adapter(createModels()));
    }

    private List<Model> createModels() {
        List<Model> models = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            models.add(new Model("Title " + i, new Random().nextBoolean()));
        }
        return models;
    }

    static class Adapter extends RecyclerView.Adapter<ViewHolder> {

        List<Model> models;

        Adapter(List<Model> models) {
            this.models = models;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null));
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.bind(models.get(position));
        }

        @Override
        public int getItemCount() {
            return models.size();
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView textView;
        CheckBox checkBox;

        ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.title);
            checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);
        }

        void bind(Model model) {
            textView.setText(model.title);
            checkBox.setChecked(model.checked);
        }

    }

    static class Model {

        String title;
        boolean checked;

        Model(String title, boolean checked) {
            this.title = title;
            this.checked = checked;
        }
    }
}

Result video

" target="_blank">


2022-09-22 21:38

Are you using ListView? If ListView is being used, it seems to be a phenomenon caused by view recycling. When using the ViewHolder pattern, you must set the data in the ViewHolder each time within the getView() function. Otherwise, the view may be reused at scroll time and the view may not match the actual data.

If I'm using a different view than ListView and I'm mistaken, please upload the relevant code to help me understand the current situation.


2022-09-22 21:38

Thank you for your reply. There is no problem with the data itself because we use the recycling view and set the data in the view holder every time. I'm sorry for the wrong explanation.

Scroll down

Suppose you have a list as shown above. The list you see on the screen is from 1 to 3 above If you scroll down, you'll see number 1 to 3 below. There is a phenomenon that the check goes in and out of the bottom number one momentarily. I think an animation has been added according to the version. I didn't have one before

I don't know if it's explained properly.
The code is extremely simple. It's just a set with data binding.


2022-09-22 21:38

If you have any answers or tips

Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656

© 2024 OneMinuteCode. All rights reserved.