RecyclerViewAdapter 删除了滚动上的填充内容

发布于 2025-01-09 10:54:47 字数 9904 浏览 5 评论 0原文

我将 RecyclerViewAdapter 用于我的 json adaper.i 以编程方式在 RecyclerViewAdapter 中制作一些单选按钮、复选框和编辑文本,但是当我检查某些单选按钮或输入编辑文本并滚动超过 1 个屏幕时,当我回来时,所有填充内容都被删除;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

    public MyRecyclerViewAdapter(Context context, ArrayList<HashMap<String, Object>> hash_all) {

    }

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

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        hash_all.size();

       hash_get = hash_all.get(position);



        LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(
                LinearLayoutCompat.LayoutParams.MATCH_PARENT,
                LinearLayoutCompat.LayoutParams.WRAP_CONTENT
        );
        params.setMargins(0, 0, 0, 0);

        String s = hash_get.get("ModelShowOption").toString();
        String toggle = "";
        String direc = "";
        String type = "";
        String model = "";
        String rows = "";
        String resize = "";
        String[] split = s.split("\\|");

        String[] split2 = new String[0];
        for (int i = 0; i < split.length; i++) {
            String s2 = split[i];
            split2 = s2.split(":");
            if (split2[0].equals("isToggleButton")){
                toggle=split2[1];
            }else if (split2[0].equals("type")){
                type=split2[1];
            }else if (split2[0].equals("direction")){
                direc=split2[1];
            }else if (split2[0].equals("model")){
                model=split2[1];
            }else if (split2[0].equals("rows")){
                rows=split2[1];
            }else if (split2[0].equals("resize")){
                resize=split2[1];
            }

        }

        String title = hash_get.get("title").toString();

        String myValue =hash_get.get("MyValue").toString();
        String[] splitValue = myValue.split("\\|");

        RadioGroup[] rg;
        RadioButton[] rb;

        if (toggle.equals("true") && holder.row_list_title.getText().equals("")){
            holder.row_list_placeholder.setVisibility(View.GONE);
            holder.row_list_buttn.setVisibility(View.GONE);

            String multiValue = hash_get.get("MultiValue").toString();
            String[] splitMultiValue = multiValue.split(":");
            if (splitMultiValue[1].equals("1")){

            int p = 0;
            rg = new RadioGroup[1];
            rb = new RadioButton[splitValue.length];
            rg[0] = new RadioGroup(holder.row_list_placeholder.getContext());
            rg[0].setOrientation(RadioGroup.VERTICAL);
            for (int j = 0; j < splitValue.length; j++) {

                    String myValue2 = splitValue[j];
                    String[] splitValue2 = myValue2.split("=");
                    rb[j] = new RadioButton(getApplicationContext());
                    rg[p].addView(rb[j]);
                    rb[j].setId(j);
                    rb[j].setText(splitValue2[0]);

            }

            holder.linearLayoutCheckBox.addView(rg[p]);

            p++;

            holder.row_list_title.setText(title);

            if (rg[0].getCheckedRadioButtonId() == -1) {

            } else {
                for (int r = 0; r < splitValue.length; r++) {
                    if (rb[r].isChecked()) {
                        RadioButton id = (RadioButton) findViewById(rb[r].getId());
                        String radioText = id.getText().toString();

                    }
                }
            }

            }else{

                holder.row_list_title.setText(title);

                CheckBox[] ch;
                final int[] checkd = {0};
                List<String> checkvl = new ArrayList<>();
                ch = new CheckBox[splitValue.length];

                    for (int j = 0; j < splitValue.length; j++) {


                        String myValue2 = splitValue[j];
                        String[] splitValue2 = myValue2.split("=");
                        ch[j] = new CheckBox(getApplicationContext());
                        holder.linearLayoutCheckBox.addView(ch[j]);
                        ch[j].setId(j);
                        ch[j].setText(splitValue2[0]);

                    }


                for (int r = 0; r < splitValue.length; r++) {
                    int finalR = r;
                    int finalR1 = r;
                    ch[r].setOnCheckedChangeListener((buttonView, isChecked) -> {
                        if (isChecked) {
                            if (checkd[0] < Integer.parseInt(splitMultiValue[1])){


                                //checkvl.add(ch[finalR1].getText().toString());
                                //hash_all_string.get(position).remove("value");
                                //hash_all_string.get(position).put("value", String.valueOf(checkvl));

                                //CheckBox id = findViewById(ch[finalR].getId());
                                //String CheckBoxText = id.getText().toString();
                                //Log.e("CheckBoxString", CheckBoxText);
                                checkd[0] = checkd[0] +1;

                            }else{

                                ch[finalR].setChecked(false);

                            }

                        } else {
                            ch[finalR].setChecked(false);
                            checkd[0] = checkd[0] -1;

                            //checkvl.remove(ch[finalR1].getText().toString());
                            //hash_all_string.get(position).remove("value");
                            //hash_all_string.get(position).put("value", String.valueOf(checkvl));
                        }
                    });
                }
            }

        }else{
            if (myValue.equals("NULL")){
                holder.row_list_title.setText(title);
                holder.row_list_placeholder.setVisibility(View.VISIBLE);
                holder.row_list_buttn.setVisibility(View.GONE);
                holder.row_list_placeholder.setHint(hash_get.get("placeholder").toString());
                if (type.equals("Text")){
                    holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_TEXT);
                }else if(type.equals("Int")){
                    holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_NUMBER);
                }else if(type.equals("Float")){
                    holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                }
            }else {

                holder.row_list_placeholder.setVisibility(View.GONE);
                holder.row_list_buttn.setVisibility(View.VISIBLE);
                holder.row_list_title.setText(title);


                int selct = 0;

                    for (int j = 0; j < hash_all2.size(); j++) {

                        if (hash_all2.get(j).get("selected").equals("true")) {

                            selct = selct+1;

                        }
                    }

                    if (selct>0){
                        holder.row_list_buttn.setBackgroundColor(getResources().getColor(R.color.purple_500));
                        holder.row_list_buttn.setText("انتخاب شد");
                    }else{
                        holder.row_list_buttn.setText("انتخاب کنید");
                        holder.row_list_buttn.setBackgroundColor(getResources().getColor(R.color.teal_200));
                    }



                String multiValue = hash_get.get("MultiValue").toString();
                String[] splitMultiValue = multiValue.split(":");


                holder.row_list_buttn.setOnClickListener(v -> {

                        if (hash_all2.size() == 0) {

                            for (int j = 0; j < splitValue.length; j++) {
                                HashMap<String, String> hash_add2 = new HashMap<>();

                                String myValue2 = splitValue[j];
                                String[] splitValue2 = myValue2.split("=");

                                hash_add2.put("Id", String.valueOf(j));
                                hash_add2.put("name", String.valueOf(splitValue2[0]));
                                hash_add2.put("value", String.valueOf(splitValue2[1]));
                                hash_add2.put("selected", "false");
                                hash_add2.put("max", splitMultiValue[1]);

                                hash_all2.add(hash_add2);

                            }
                            adapt.notifyDataSetChanged();
                        }

                        alertDialogSelect(position);
                    });

            }
        }




    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.row_form, parent, false);
        return new ViewHolder(view);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView row_list_title;
        EditText row_list_placeholder;
        LinearLayout linearLayoutCheckBox;
        Button row_list_buttn;



        ViewHolder(View itemView) {
            super(itemView);

            row_list_title = itemView.findViewById(R.id.row_list_title);
            row_list_placeholder = itemView.findViewById(R.id.row_list_placeholder);
            linearLayoutCheckBox = itemView.findViewById(R.id.mainrow);
            row_list_buttn = itemView.findViewById(R.id.row_list_buttn);

        }
    }
}

请帮助。我尝试了堆栈中的许多代码但不起作用

holder.setIsRecyclable(false); setHasStableIds(true);

这对我不起作用

i use RecyclerViewAdapter for my json adaper.i make some radiobutton and check box and edittext in RecyclerViewAdapter programically but when i check some radio button or type in edittext and scroll more than 1 screen when i come back all my fill content are removed;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

    public MyRecyclerViewAdapter(Context context, ArrayList<HashMap<String, Object>> hash_all) {

    }

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

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        hash_all.size();

       hash_get = hash_all.get(position);



        LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(
                LinearLayoutCompat.LayoutParams.MATCH_PARENT,
                LinearLayoutCompat.LayoutParams.WRAP_CONTENT
        );
        params.setMargins(0, 0, 0, 0);

        String s = hash_get.get("ModelShowOption").toString();
        String toggle = "";
        String direc = "";
        String type = "";
        String model = "";
        String rows = "";
        String resize = "";
        String[] split = s.split("\\|");

        String[] split2 = new String[0];
        for (int i = 0; i < split.length; i++) {
            String s2 = split[i];
            split2 = s2.split(":");
            if (split2[0].equals("isToggleButton")){
                toggle=split2[1];
            }else if (split2[0].equals("type")){
                type=split2[1];
            }else if (split2[0].equals("direction")){
                direc=split2[1];
            }else if (split2[0].equals("model")){
                model=split2[1];
            }else if (split2[0].equals("rows")){
                rows=split2[1];
            }else if (split2[0].equals("resize")){
                resize=split2[1];
            }

        }

        String title = hash_get.get("title").toString();

        String myValue =hash_get.get("MyValue").toString();
        String[] splitValue = myValue.split("\\|");

        RadioGroup[] rg;
        RadioButton[] rb;

        if (toggle.equals("true") && holder.row_list_title.getText().equals("")){
            holder.row_list_placeholder.setVisibility(View.GONE);
            holder.row_list_buttn.setVisibility(View.GONE);

            String multiValue = hash_get.get("MultiValue").toString();
            String[] splitMultiValue = multiValue.split(":");
            if (splitMultiValue[1].equals("1")){

            int p = 0;
            rg = new RadioGroup[1];
            rb = new RadioButton[splitValue.length];
            rg[0] = new RadioGroup(holder.row_list_placeholder.getContext());
            rg[0].setOrientation(RadioGroup.VERTICAL);
            for (int j = 0; j < splitValue.length; j++) {

                    String myValue2 = splitValue[j];
                    String[] splitValue2 = myValue2.split("=");
                    rb[j] = new RadioButton(getApplicationContext());
                    rg[p].addView(rb[j]);
                    rb[j].setId(j);
                    rb[j].setText(splitValue2[0]);

            }

            holder.linearLayoutCheckBox.addView(rg[p]);

            p++;

            holder.row_list_title.setText(title);

            if (rg[0].getCheckedRadioButtonId() == -1) {

            } else {
                for (int r = 0; r < splitValue.length; r++) {
                    if (rb[r].isChecked()) {
                        RadioButton id = (RadioButton) findViewById(rb[r].getId());
                        String radioText = id.getText().toString();

                    }
                }
            }

            }else{

                holder.row_list_title.setText(title);

                CheckBox[] ch;
                final int[] checkd = {0};
                List<String> checkvl = new ArrayList<>();
                ch = new CheckBox[splitValue.length];

                    for (int j = 0; j < splitValue.length; j++) {


                        String myValue2 = splitValue[j];
                        String[] splitValue2 = myValue2.split("=");
                        ch[j] = new CheckBox(getApplicationContext());
                        holder.linearLayoutCheckBox.addView(ch[j]);
                        ch[j].setId(j);
                        ch[j].setText(splitValue2[0]);

                    }


                for (int r = 0; r < splitValue.length; r++) {
                    int finalR = r;
                    int finalR1 = r;
                    ch[r].setOnCheckedChangeListener((buttonView, isChecked) -> {
                        if (isChecked) {
                            if (checkd[0] < Integer.parseInt(splitMultiValue[1])){


                                //checkvl.add(ch[finalR1].getText().toString());
                                //hash_all_string.get(position).remove("value");
                                //hash_all_string.get(position).put("value", String.valueOf(checkvl));

                                //CheckBox id = findViewById(ch[finalR].getId());
                                //String CheckBoxText = id.getText().toString();
                                //Log.e("CheckBoxString", CheckBoxText);
                                checkd[0] = checkd[0] +1;

                            }else{

                                ch[finalR].setChecked(false);

                            }

                        } else {
                            ch[finalR].setChecked(false);
                            checkd[0] = checkd[0] -1;

                            //checkvl.remove(ch[finalR1].getText().toString());
                            //hash_all_string.get(position).remove("value");
                            //hash_all_string.get(position).put("value", String.valueOf(checkvl));
                        }
                    });
                }
            }

        }else{
            if (myValue.equals("NULL")){
                holder.row_list_title.setText(title);
                holder.row_list_placeholder.setVisibility(View.VISIBLE);
                holder.row_list_buttn.setVisibility(View.GONE);
                holder.row_list_placeholder.setHint(hash_get.get("placeholder").toString());
                if (type.equals("Text")){
                    holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_TEXT);
                }else if(type.equals("Int")){
                    holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_NUMBER);
                }else if(type.equals("Float")){
                    holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                }
            }else {

                holder.row_list_placeholder.setVisibility(View.GONE);
                holder.row_list_buttn.setVisibility(View.VISIBLE);
                holder.row_list_title.setText(title);


                int selct = 0;

                    for (int j = 0; j < hash_all2.size(); j++) {

                        if (hash_all2.get(j).get("selected").equals("true")) {

                            selct = selct+1;

                        }
                    }

                    if (selct>0){
                        holder.row_list_buttn.setBackgroundColor(getResources().getColor(R.color.purple_500));
                        holder.row_list_buttn.setText("انتخاب شد");
                    }else{
                        holder.row_list_buttn.setText("انتخاب کنید");
                        holder.row_list_buttn.setBackgroundColor(getResources().getColor(R.color.teal_200));
                    }



                String multiValue = hash_get.get("MultiValue").toString();
                String[] splitMultiValue = multiValue.split(":");


                holder.row_list_buttn.setOnClickListener(v -> {

                        if (hash_all2.size() == 0) {

                            for (int j = 0; j < splitValue.length; j++) {
                                HashMap<String, String> hash_add2 = new HashMap<>();

                                String myValue2 = splitValue[j];
                                String[] splitValue2 = myValue2.split("=");

                                hash_add2.put("Id", String.valueOf(j));
                                hash_add2.put("name", String.valueOf(splitValue2[0]));
                                hash_add2.put("value", String.valueOf(splitValue2[1]));
                                hash_add2.put("selected", "false");
                                hash_add2.put("max", splitMultiValue[1]);

                                hash_all2.add(hash_add2);

                            }
                            adapt.notifyDataSetChanged();
                        }

                        alertDialogSelect(position);
                    });

            }
        }




    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.row_form, parent, false);
        return new ViewHolder(view);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView row_list_title;
        EditText row_list_placeholder;
        LinearLayout linearLayoutCheckBox;
        Button row_list_buttn;



        ViewHolder(View itemView) {
            super(itemView);

            row_list_title = itemView.findViewById(R.id.row_list_title);
            row_list_placeholder = itemView.findViewById(R.id.row_list_placeholder);
            linearLayoutCheckBox = itemView.findViewById(R.id.mainrow);
            row_list_buttn = itemView.findViewById(R.id.row_list_buttn);

        }
    }
}

please help.i tryed many code from stack but not working

holder.setIsRecyclable(false);
setHasStableIds(true);

this is not working for me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝對不後悔。 2025-01-16 10:54:47

在 RecyclerView 中,当删除视图的滚动状态并通过滚动创建新视图时,

您必须在 radioButton 或 edittext 内容更改时更改数据,并在模型中设置新数据。

In RecyclerView when scrolling state of view removed and creating new view by scroll,

you must change your data when radioButton or edittext content changed, and set new data in your model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文