Android RecyclerView的EditText在输入数据并向下滚动后交换其位置

时间:2015-12-03 10:05:18

标签: java android android-edittext android-recyclerview

我有一个RecyclerView,其中包含TextViewEditTextTextView RecyclerView EditText对我来说非常好,但对于{ {1}},如果我在第一个单元格中输入值并继续输入其他单元格并滚动上面输入的值将不存在。

public class Assesment_MarkcardAdapter extends RecyclerView.Adapter<Assesment_MarkcardAdapter.ContactViewHolder> {

    private String[] mDataset;

    public Assesment_MarkcardAdapter(String[] myDataset)
    {
        mDataset = myDataset;
    }

    private List<Assesment_cardinfo> contactList;
    private static String LOG_TAG = "Assesment_MarkcardAdapter";


    public Assesment_MarkcardAdapter(List<Assesment_cardinfo> contactList) {
        this.contactList = contactList;
    }


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

    @Override
    public void onBindViewHolder(ContactViewHolder contactViewHolder, int i)
    {

        mDataset = new String[contactList.size()];
        Assesment_cardinfo ci = contactList.get(i);
        contactViewHolder.studentname.setText(ci.getStudentname());
        contactViewHolder.studentid.setText(ci.getStudentid());
        contactViewHolder.grade.setText(ci.getGrade());
        contactViewHolder.improvement.setText(ci.getImprovements());
        contactViewHolder.grade.setTag(ci);
        contactViewHolder.improvement.setTag(ci);
        contactViewHolder.myCustomEditTextListener.updatePosition(i);
        contactViewHolder.myCustomEditTextListener2.updatePosition2(i);
        //contactViewHolder.grade.setText(mDataset[i]);
    }

    @Override
    public Assesment_MarkcardAdapter.ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
    {
        View itemView = LayoutInflater.
                from(viewGroup.getContext()).
                inflate(R.layout.assesment_markcard, viewGroup, false);

        return new ContactViewHolder(itemView , new MyCustomEditTextListener(),new MyCustomEditTextListener2());
    }

    public static class ContactViewHolder extends RecyclerView.ViewHolder {

        public MyCustomEditTextListener myCustomEditTextListener;
        public MyCustomEditTextListener2 myCustomEditTextListener2;

        protected TextView studentname;
        protected TextView studentid;
        protected EditText grade;
        protected EditText improvement;


        public ContactViewHolder(View v, MyCustomEditTextListener myCustomEditTextListener, MyCustomEditTextListener2 myCustomEditTextListener2 ) {
            super(v);
            studentname = (TextView) v.findViewById(R.id.txt_student_name);
            studentid = (TextView) v.findViewById(R.id.txt_student_id);
            grade = (EditText) v.findViewById(R.id.edtxt_grade);
            this.myCustomEditTextListener = myCustomEditTextListener;
            grade.addTextChangedListener(myCustomEditTextListener);
            improvement = (EditText) v.findViewById(R.id.edtxt_improvement);
            this.myCustomEditTextListener2 = myCustomEditTextListener2;
            improvement.addTextChangedListener(myCustomEditTextListener2);

        }

    }
    private class MyCustomEditTextListener implements TextWatcher
    {
        private int position;

        public void updatePosition(int position)
        {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
        {
            // no op
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3)
        {
            contactList.get(position).setGrade(charSequence.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {
            // no op
        }
    }

    private class MyCustomEditTextListener2 implements TextWatcher
    {
        private int position;

        public void updatePosition2(int position)
        {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
        {
            // no op
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3)
        {
            contactList.get(position).setImprovements(charSequence.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {
            // no op
        }
    }
    public List<Assesment_cardinfo> getStudentLists()
    {
        return contactList;
    }
}

3 个答案:

答案 0 :(得分:1)

onBindViewHolder()

中添加这两行
contactViewHolder.grade.setText(ci.getGrade());
contactViewHolder.improvement.setText(ci.getImprovement());

设置侦听器之后,这意味着在

之后
contactViewHolder.myCustomEditTextListener.updatePosition(i);
contactViewHolder.myCustomEditTextListener2.updatePosition2(i);

这两行代码。

这肯定会起作用。这是因为您没有将文本设置为EditText,并且在回收后,文本已经消失。所以,试试这个,它肯定会起作用。

答案 1 :(得分:1)

我遇到了同样的问题。

holder.editQty.addTextChangedListener

当您将Textwatcher添加到已添加到侦听器数组的Edittext时,这是不好的做法。

您必须使用set null删除所有旧侦听器。

 holder.editQty.addTextChangedListener(null);
 holder.editQty.addTextChangedListener(new TextWatcher())

它将解决问题。

答案 2 :(得分:0)

将输入字段放在回收站视图中真的很糟糕。尝试重新构建您的UI设计。 Recycler视图应始终具有只读字段。即使这样,您也希望实现此功能,尝试动态创建视图并将其添加到rooot布局中。 例如,有一个XML文件,其中线性布局将是您的根。再创建一个XML文件,并继续将此模板添加到根视图中。

可能是这样的 这将是你的根布局instear od recycler view

<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

//Create a template layout.You need to add the property i am just skippint it
<LinearLayout>
<TextView/>
<EditText/>


// In you activity write a method to create a view dynamically from template and add it to your root Layout. 

LinearLayout rootLayout=(LinearLayout)findViewById(R.id.container);

createChilds(rootLayout);

private void createChilds(LinearLayout rootLayout)
{
LinearLayout  childContainer=(LinearLayout)LayoutInflater.from(context).inflate(R.layout.template_layout,null);
TextView tv=childContainer.findViewById(R.id.textviewid);
EditText et=childContainer.findViewById(R.id.edittextviewid);

// you can register for textwatcher for edit text if you want

//Add it to roolayout
rootLayout.addView(childContainer);
}