Recyclerview与复选框上滚动为了改变 [英] Recyclerview with checkboxes changes order on scroll

查看:160
本文介绍了Recyclerview与复选框上滚动为了改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有名字和复选框列表的recyclerview。当我在列表中勾选列表中的几个项目滚动,复选框的位置变化。尝试了很多,以摆脱它。任何人可以帮助?

I have a recyclerview with a list of names and checkboxes . When I scroll through the list by checking a few items in the list , the position of the checkboxes change . Tried a lot to get rid of it . Can anybody help ?

我有单独的模型和适配器类和我的父母欣赏到recyclerview是一个活动的顶部的片段。

I have separate model and adapter class and my parent view to the recyclerview is a fragment on top of an activity.

我也可以为参考提供code。

I can also provide code for reference .

谢谢!

Fragment.java

Fragment.java

recyclerView = (RecyclerView) getView().findViewById(R.id.my_recycler_view2);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(llm);


        try{
            data = new JSONObject(MainActivity.responseobject);
            //data.getJSONArray("Children");
            demoData = new ArrayList<Model>();
            for(int i=0;i<data.getJSONArray("Children").length();i++)
            {
                Model model = new Model();
                model.name = data.getJSONArray("Children").getJSONObject(i).getJSONObject("Child").getString("childName");
                model.zone = data.getJSONArray("Children").getJSONObject(i).getJSONObject("Child").getString("Zone");
                model.latitude = data.getJSONArray("Children").getJSONObject(i).getJSONObject("Child").getString("childLat");
                model.longitude= data.getJSONArray("Children").getJSONObject(i).getJSONObject("Child").getString("childLon");
                model.isChecked=MainActivity.checkedall?true:false;
                model.studentid = data.getJSONArray("Children").getJSONObject(i).getJSONObject("Child").getString("ParentID");
                demoData.add(model);
            }

            adapter = new RecyclerViewAdapterMessage(demoData,getActivity());
            recyclerView.setAdapter(adapter);

        }
        catch (Exception e){
            e.printStackTrace();
        }

Adapter.java

@Override
    public ListItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).
                inflate(R.layout.recyclerview_messageitem, viewGroup, false);

        return new ListItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final ListItemViewHolder viewHolder, int position) {

        final Model model = items.get(position);
        viewHolder.name.setText(String.valueOf(model.name));
        //viewHolder.itemView.setActivated(model.isChecked);
        viewHolder.select.setActivated(viewHolder.checkboxstate);
        viewHolder.checkboxstate = false;
        System.out.println(selectedItems.toString());
        viewHolder.select.setTag(model);
        if (MainActivity.checkedall) {
            for (int i = 0; i < items.size(); i++) {
                if (MainActivity.checkedall) {
                    viewHolder.select.setActivated(viewHolder.checkboxstate);
                    System.out.println("ALL CHECKED INSIDE AVA");
                }
            }
        }

        viewHolder.select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                Model contact = (Model) cb.getTag();
                contact.isChecked = true;
               /* if(){

                }else{

                }*/
                if(cb.isChecked()){
                    TwoFragment.sid=TwoFragment.sid+","+contact.studentid;
                    //contact.studentid = contact.studentid;
                    //notifyDataSetChanged();
                    Toast.makeText(v.getContext(), "Clicked on Checkbox: " + contact.name + " is " + cb.isChecked() +"    "+contact.studentid, Toast.LENGTH_LONG).show();
                 }
                }
        });
        //model.studentid




    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return items.size();
    }
    public Model getItem(int position){
        return items.get(position);
    }

    public final static class ListItemViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        CheckBox select;
        boolean checkboxstate;

        public ListItemViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.person_name);
            select=(CheckBox) itemView.findViewById(R.id.person_zone);
        }
    }
    public List<Model> getStudentist() {
        return items;
    }

我所提供的code将片段类和适配器类。

I have provided the code to the fragment class and the adapter class.

推荐答案

我不明白为什么你onBindViewHolder这样做。

I can't understand why you doing this in onBindViewHolder.

if (MainActivity.checkedall) {
        for (int i = 0; i < items.size(); i++) {
            if (MainActivity.checkedall) {
                viewHolder.select.setActivated(viewHolder.checkboxstate);
                System.out.println("ALL CHECKED INSIDE AVA");
            }
        }
    }

本上选择的所有视图点击,不要更改 model.isChecked 此值设置为true并调用 adapter.notifyDataSetChanged(); 。也没必要这样 checkboxstate 在ViewHolder

for this on click of select all view, do change model.isChecked this value to true and just call adapter.notifyDataSetChanged();. also no need of this checkboxstate in ViewHolder

和有关选中复选框

viewHolder.select.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // Just Do this
            model.isChecked = viewHolder.select.isChecked();

            if(viewHolder.select.isChecked()){
                TwoFragment.sid=TwoFragment.sid+","+contact.studentid;

                Toast.makeText(v.getContext(), "Clicked on Checkbox: " + model.name + " is " + viewHolder.select.isChecked() +"    "+model.studentid, Toast.LENGTH_LONG).show();
             }
            }
    });

最后

if(model.isChecked)
    viewHolder.select.setChecked(true);
else
    viewHolder.select.setChecked(false)

这篇关于Recyclerview与复选框上滚动为了改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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