微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何防止项目以动态形式在滚动或键盘上重复?

如何解决如何防止项目以动态形式在滚动或键盘上重复?

大家好,我已经创建了区域调查项目。我已经在回收器视图中加载了问题,其中动态生成编辑文本、单选按钮、复选框、微调器(每个文件没有单独的布局文件)我使用了新的编辑文本并使用文本观察器来保持列表中的答案动态更新,以便它可以返回到主要活动,即从适配器类到活动类。我的问题是编辑文本、单选按钮等被复制,但在向上或向下滚动或向下滚动键盘后文本视图中的问题没有出现,如果操作员执行如在编辑文本中输入输入或更改微调器中的值然后向上滚动

注意:-文本视图是静态组件,动态输入添加到行布局文件中的线性布局中,文本视图和编辑文本都是线性布局的子组件,基于输入类型是否添加为线性布局的第二个子项>

Activity.class

 mlist=new ArrayList<>();
    mlist.clear();
    mlist=db.populatequestion();
    Log.d("size",String.valueOf(mlist.size()));
    Log.d("Question",mlist.get(0).getQuestionType());
    mRecyclerView=findViewById(R.id.rvForm);
    mLayoutManager = new linearlayoutmanager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    formsadapter =new formsadapter(mlist,this,mRecyclerView);
    mRecyclerView.setAdapter(formsadapter);

formsadapter.class

public class formsadapter extends RecyclerView.Adapter<formsadapter.ViewHolder> {
private List<FormQData> mlists;
private List<formanswer> formanswers;
private Context mContext1;
private RecyclerView mRecyclerV1;
public class ViewHolder extends RecyclerView.ViewHolder{
    public LinearLayout linearLayout;
    public TextView Question;
    public View layout;
    public ViewHolder(View v) {
        super(v);
        layout=v;
        Question=v.findViewById(R.id.tvQuestion);
        linearLayout=v.findViewById(R.id.llform);
        formanswers = new ArrayList<>();
    }
}

public formsadapter(List<FormQData> myDataset,Context context,RecyclerView recyclerView) {
    mlists = myDataset;
    mContext1 = context;
    mRecyclerV1 = recyclerView;
}
public formsadapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
    LayoutInflater inflater = LayoutInflater.from(
            parent.getContext());
    View v =inflater.inflate(R.layout.row_form,parent,false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}
public void onBindViewHolder(formsadapter.ViewHolder holder,final int position){
    final FormQData fb=mlists.get(position);
    formanswer fa=new formanswer();
    holder.Question.setText(fb.getQuestionContent());
    LinearLayout.LayoutParams mainparams1 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
    mainparams1.gravity= Gravity.RIGHT;
    if(fb.getQuestionType().equals("textBox")){
        EditText et=new EditText(mContext1);
        et.setBackgroundResource(R.drawable.Boxwithcircularcoloredborders);
        et.setTextColor(mContext1.getResources().getColor(R.color.colorPrimary));
        et.setPadding(15,15,15);
        et.addTextChangedListener(new TextWatcher() {
            @Override public void beforeTextChanged(CharSequence charSequence,int i,int i1,int i2) {}
            @Override public void onTextChanged(CharSequence charSequence,int i2) {}
            @Override
            public void afterTextChanged(Editable editable) {
                fa.setQuestionid(fb.getQuestionid());
                fa.setAnswer(et.getText().toString());
                if(formanswers.contains(fa)){
                    formanswers.set(formanswers.indexOf(fa),fa);
                }
                else
                    formanswers.add(fa);
            }
        });
        holder.linearLayout.addView(et,1);
    }
    else if(fb.getQuestionType().equals("radioBox")){
        RadioGroup rg=new RadioGroup(mContext1);
        rg.setorientation(RadioGroup.HORIZONTAL);
        String[] arrOfStr = fb.getQuestionoptions().split(":");
        RadioButton[] radiobutton = new RadioButton[arrOfStr.length];
        for(int i=0;i<arrOfStr.length;i++){
            radiobutton[i]=new RadioButton(mContext1);
            radiobutton[i].setText(arrOfStr[i]);
            radiobutton[i].setLayoutParams(mainparams1);
            radiobutton[i].setBackgroundResource(R.drawable.Boxwithcircularcoloredborders);
            radiobutton[i].setTextColor(mContext1.getResources().getColor(R.color.colorPrimary));
            radiobutton[i].setTypeface(Typeface.DEFAULT_BOLD);
            radiobutton[i].setPadding(15,15);
            int finalI = i;
            radiobutton[i].setonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    fa.setQuestionid(fb.getQuestionid());
                    fa.setAnswer(radiobutton[finalI].getText().toString());
                    if(formanswers.contains(fa)){
                        formanswers.set(formanswers.indexOf(fa),fa);
                    }
                    else
                        formanswers.add(fa);
                }
            });
            rg.addView(radiobutton[i],i);
        }
        holder.linearLayout.addView(rg,1);
    }
    else if(fb.getQuestionType().equals("checkBox")){
        List<String> answer=new ArrayList<>();
        RadioGroup rg=new RadioGroup(mContext1);
        rg.setorientation(RadioGroup.HORIZONTAL);
        String[] arrOfStr = fb.getQuestionoptions().split(":");
        CheckBox[] checkBoxes = new CheckBox[arrOfStr.length];
        for(int i=0;i<arrOfStr.length;i++){
            checkBoxes[i]=new CheckBox(mContext1);
            checkBoxes[i].setText(arrOfStr[i]);
            checkBoxes[i].setLayoutParams(mainparams1);
            checkBoxes[i].setBackgroundResource(R.drawable.Boxwithcircularcoloredborders);
            checkBoxes[i].setTextColor(mContext1.getResources().getColor(R.color.colorPrimary));
            checkBoxes[i].setTypeface(Typeface.DEFAULT_BOLD);
            checkBoxes[i].setPadding(15,15);
            int finalI = i;
            checkBoxes[i].setonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    fa.setQuestionid(fb.getQuestionid());
                    if(checkBoxes[finalI].isChecked()){
                        String temp=checkBoxes[finalI].getText().toString();
                        if(answer.contains(temp)){
                            answer.set(answer.indexOf(temp),temp);
                        }else {
                            answer.add(checkBoxes[finalI].getText().toString());
                        }
                    }else{
                        answer.remove(checkBoxes[finalI].getText().toString());
                    }
                    fa.setAnswer(answer.toString());
                    if(formanswers.contains(fa)){
                        formanswers.set(formanswers.indexOf(fa),fa);
                    }
                    else
                        formanswers.add(fa);
                }
            });
            rg.addView(checkBoxes[i],1);
    }
    else if(fb.getQuestionType().equals("dropdown")){
        Spinner spinner=new Spinner(mContext1);
        fa.setQuestionid(fb.getQuestionid());
        List<String> arrOfStr=new ArrayList<>(Arrays.asList(fb.getQuestionoptions().split(":")));
        arrOfStr.add(0,"Select A Option");
        ArrayAdapter<String> spinnerAdapter=new ArrayAdapter<>(mContext1,R.layout.row_dropdownsinglecelltv,R.id.tvsinglecell2,arrOfStr);
        spinnerAdapter.setDropDownViewResource(R.layout.row_dropdownsinglecelltv);
        spinner.setPopupBackgroundDrawable(mContext1.getResources().getDrawable(R.drawable.Boxcircularcorner));
        spinner.setAdapter(spinnerAdapter);
        spinner.setonItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView,View view,long l) {
                String spinnerValue=String.valueOf(adapterView.getSelectedItem());
                if (!spinnerValue.equals("Select A Option")){
                    //Log.d("value","upload");
                    fa.setAnswer(spinnerValue);
                    if(formanswers.contains(fa))
                        formanswers.set(formanswers.indexOf(fa),fa);
                    else
                        formanswers.add(fa);
                }else{
                    if(formanswers.contains(fa))
                        formanswers.remove(fa);
                }

            }

            @Override
            public void onnothingSelected(AdapterView<?> adapterView) {

            }
        });
        holder.linearLayout.addView(spinner,1);
    }
    else {
        TextView et=new TextView(mContext1);
        et.setText("invalid question type");
        holder.linearLayout.addView(et,1);
    }
}
public int getItemCount(){
    return  mlists.size();
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public int getItemViewType(int position) {
    return position;
}
public List<formanswer> formanswers(){
    return formanswers;
}

}

row_form.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<LinearLayout
    android:id="@+id/llform"
    android:background="@drawable/Boxwithcoloredborder"
    android:layout_width="match_parent"
    android:padding="15dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">



    <TextView
        android:id="@+id/tvQuestion"
        android:padding="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        android:textStyle="bold"
        android:text="TextView" />

</LinearLayout>
</LinearLayout>

这是示例视频

https://imgur.com/a/hi3Vb5a

解决方法

我找到了解决方案,看来问题完全不同。问题是回收器视图没有被正确回收,它的 viewCachesize 需要增加到列表大小

您可以检查您的 recyclerview 是否正在被回收利用

@Override
    public void onViewRecycled(@NonNull formsadapter.ViewHolder holder) {
   Log.d("recyclerview","recyclerview is recycled");
    }

如果出现日志,那么你解决这个问题添加

holder.setIsRecyclable(false);

在 onbindviewholder 或添加

mRecyclerView.setItemViewCacheSize(mlist.size()+1);

在主要活动本身 显然只是使用listview本身而不是recyclerview,那么这些行不需要添加

链接:- RecyclerView not recycling views if the view count is small

How can I detect recycling from within a recyclerView adapter,so I can perform an action before it gets recycled?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?