java - 在RecyclerView中滚动后,EditText丢失内容

我有一个问题,我已经有几个星期没能解决了。我有一个应用程序,添加两个编辑文本和一个复选框按钮点击。但是,每当我在edittext中键入一些内容,然后向下滚动时,当我向上滚动时,内容就会消失。我想不出解决办法。我已经找到了其他一些非常相似的帖子,但是我已经尝试了所有我能找到的解决方案,但是似乎还没有一个能帮到我。下面是我发现的几个类似的帖子:
EditText loses content on scroll in ListView
Saving EditText content in RecyclerView
我最近对我的程序做了一些修改,试图修复这个问题。我向main活动添加了一个arraylist并将其传递给适配器。我还制作了两个自定义文本监听器,其中ontextchanged,我将字符串添加到arraylist。然后,在onBindViewHolder中,我更新文本监听器中的位置,然后将文本设置为ArrayList中该位置的字符串。然而,这也带来了一些奇怪的错误。现在,当我向edittext添加内容并向下滚动时,它会将其添加到多个edittext中,甚至更改其中一些文本中的内容。我不知道该怎么解决这个问题。
另外,在另一个注意事项上,我已经启用了这个应用程序,可以通过拖放和滑动来关闭它。所以如果你有办法的话,也请考虑到这一点。谢谢您!
主要活动

import android.content.ClipData;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ArrayList<ListItems> itemsList;
    private RecyclerView mRecyclerView;
    private MyRecyclerAdapter adapter;
    private List<String> courseStrings = new ArrayList<>();
    private List<String> creditStrings =  new ArrayList<>();

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

        courseStrings.add("");
        creditStrings.add("");

        // Toast variable in order to fix toast queueing issue.
        final Toast toast = Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT);

        // For the recycler view.
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

        itemsList = new ArrayList<>();
        adapter = new MyRecyclerAdapter(MainActivity.this, itemsList, courseStrings, creditStrings);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        // For the addCourse button.
        final Button addCourse = (Button) findViewById(R.id.addCourse);
        addCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.createListItem(new ListItems(null, null, false), toast);
                toast.setText("New course added");
                toast.show();
            }
        });

        final Button clearAll = (Button) findViewById(R.id.clearAll);
        clearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // In order to clear the list.
                if (itemsList.size() == 0) {
                    toast.setText("All courses have already been cleared.");
                    toast.show();
                } else {
                    adapter.clearAdapter();
                    toast.setText("All courses have been cleared.");
                    toast.show();
                }
            }
        });

        // For the drag and drop/swipe to dismiss.
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(
                new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,
                        ItemTouchHelper.LEFT) {
                    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                        final int fromPos = viewHolder.getAdapterPosition();
                        final int toPos = target.getAdapterPosition();
                        Collections.swap(itemsList, fromPos, toPos);
                        adapter.notifyItemMoved(fromPos, toPos);
                        return true;
                    }
                    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                       adapter.onItemDismiss(viewHolder.getAdapterPosition());
                    }
                });
        itemTouchHelper.attachToRecyclerView(mRecyclerView);
    } // End of onCreate
} // End of MainActivity

MyRecyclerAdapter公司
import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.CustomRowViewHolder> {

     private ArrayList<ListItems> itemsList;
     private Context mContext;
     private List<String> courseStrings;
     private List<String> creditStrings;


    public MyRecyclerAdapter(Context context, ArrayList<ListItems> itemsList, List<String> courseStrings, List<String> creditStrings){
        this.itemsList = itemsList;
        this.mContext = context;
        this.courseStrings = courseStrings;
        this.creditStrings = creditStrings;
    }

     @Override
     public MyRecyclerAdapter.CustomRowViewHolder onCreateViewHolder(final ViewGroup viewGroup, int position) {
         View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.new_course_row, null);
         final CustomRowViewHolder holder = new CustomRowViewHolder(v, new CoursesCustomTextListener(), new CreditsCustomTextListener());
         holder.creditsText.setInputType(InputType.TYPE_CLASS_NUMBER);
         holder.checkBox.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 if(holder.checkBox.isChecked()) {
                     holder.courseText.setEnabled(false);
                     holder.courseText.setFocusable(false);
                     holder.courseText.setInputType(InputType.TYPE_NULL);
                     holder.creditsText.setEnabled(false);
                     holder.creditsText.setFocusable(false);
                     holder.creditsText.setInputType(InputType.TYPE_NULL);
                 } else {
                     holder.courseText.setEnabled(true);
                     holder.courseText.setFocusable(true);
                     holder.courseText.setFocusableInTouchMode(true);
                     holder.courseText.setInputType(InputType.TYPE_CLASS_TEXT);
                     holder.creditsText.setEnabled(true);
                     holder.creditsText.setFocusable(true);
                     holder.creditsText.setFocusableInTouchMode(true);
                     holder.creditsText.setInputType(InputType.TYPE_CLASS_NUMBER);
                 } // End if else
             }
         });
         return holder;
     } // End of onCreateViewHolder

     @Override
     public void onBindViewHolder(CustomRowViewHolder holder, final int position) {
         ListItems listItem = itemsList.get(position);
         int focusedItem = 0;
         holder.itemView.setSelected(focusedItem == position);
         holder.getLayoutPosition();

         holder.creditsCustomTextListener.updatePosition(position);
         holder.creditsCustomTextListener.updatePosition(position);

         holder.courseText.setText(courseStrings.get(position));
         holder.creditsText.setText(creditStrings.get(position));

         // Set listener to check box.
         holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                 itemsList.get(position).setIsComplete(b);
             }
         });
         holder.checkBox.setChecked(  itemsList.get(position).getIsComplete());
     } // End of onBindViewHolder

     public void clearAdapter() {
         itemsList.clear();
         notifyDataSetChanged();
     } // End of clearAdapter

     public int getItemCount() {
         return(null != itemsList ? itemsList.size() : 0);
     } // End of getItemCount

     public void onItemDismiss(int position) {
         itemsList.remove(position);
         notifyItemRemoved(position);
         notifyItemRangeChanged(position, itemsList.size());
     } // End of onItemDismiss

     public void createListItem(ListItems listItem, Toast toast) {
         itemsList.add(listItem);
         int position = itemsList.indexOf(listItem);
         notifyItemInserted(position);
     } // End of createListItem

     ///////////////////////////////////// CustomRowViewHolder /////////////////////////////////////

     public static class CustomRowViewHolder extends RecyclerView.ViewHolder {

         public EditText courseText;
         public EditText creditsText;
         public CheckBox checkBox;
         public RelativeLayout relativeLayout;
         public CoursesCustomTextListener coursesCustomTextListener;
         public CreditsCustomTextListener creditsCustomTextListener;

         public CustomRowViewHolder(View view, CoursesCustomTextListener coursesCustomTextListener, CreditsCustomTextListener creditsCustomTextListener) {
             super(view);
             this.coursesCustomTextListener = coursesCustomTextListener;
             this.creditsCustomTextListener = creditsCustomTextListener;
             this.courseText = (EditText) view.findViewById(R.id.course);
             this.creditsText = (EditText) view.findViewById(R.id.credits);
             this.checkBox = (CheckBox) view.findViewById(R.id.complete);
             this.relativeLayout = (RelativeLayout) view.findViewById(R.id.relLayout);
             this.courseText.addTextChangedListener(coursesCustomTextListener);
             this.creditsText.addTextChangedListener(creditsCustomTextListener);
         }
     }

     ////////////////////////////////// CoursesCustomTextListener //////////////////////////////////

     private class CoursesCustomTextListener implements TextWatcher {
         private int position;

         public void updatePosition(int position) {
             this.position = position;
         }
         @Override
         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
             // No operation to perform.
         }

         @Override
         public void onTextChanged(CharSequence s, int start, int before, int count) {
             courseStrings.add(s.toString());
         }

         @Override
         public void afterTextChanged(Editable s) {
             // No operation to perform.
         }
     }

     ////////////////////////////////// CreditsCustomTextListener //////////////////////////////////

     private class CreditsCustomTextListener implements TextWatcher {
         private int position;

         public void updatePosition(int position) {
             this.position = position;
         }
         @Override
         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
             // No operation to perform.
         }

         @Override
         public void onTextChanged(CharSequence s, int start, int before, int count) {
             creditStrings.add(s.toString());
         }

         @Override
         public void afterTextChanged(Editable s) {
             // No operation to perform.
         }
     }
} // End of MyRecyclerAdapter

列表项
import java.util.ArrayList;

public class ListItems {
    private String course;
    private String credits;
    private Boolean complete;

    public ListItems(String mCourse, String mCredits, Boolean mComplete) {
        course = mCourse;
        credits = mCredits;
        complete = mComplete;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    public String getCredits() {
        return credits;
    }

    public void setCredits(String credits) {
        this.credits = credits;
    }

    public Boolean getIsComplete() {
        return complete;
    }

    public void setIsComplete(Boolean complete) {
        this.complete = complete;
    }
} // End of ListItems

新课程.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relLayout"
    android:layout_margin="5dp">

    <EditText
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:id="@+id/course"
        android:hint="Enter Course ID"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="16sp"
        android:maxLines="1" />

    <EditText
        android:layout_width="115dp"
        android:layout_height="wrap_content"
        android:id="@+id/credits"
        android:hint="Enter Credits"
        android:layout_alignBottom="@+id/course"
        android:textSize="16sp"
        android:layout_toRightOf="@+id/course"
        android:maxLines="1" />

    <CheckBox
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Check if complete"
        android:id="@+id/complete"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignBottom="@+id/course"
        android:textSize="13sp"
        android:paddingBottom="4dp"
        android:paddingTop="4dp" />

</RelativeLayout>

活动\主.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingRight="10dp"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:id="@+id/rl"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Course"
        android:id="@+id/addCourse"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:textColor="#FFF"
        android:background="@drawable/my_button"
        android:textSize="18dp"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear All"
        android:id="@+id/clearAll"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:textColor="#FFF"
        android:background="@drawable/my_button"
        android:textSize="18dp"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"/>

    <android.support.v7.widget.RecyclerView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/recycler_view"
        android:layout_below="@+id/addCourse"
        android:layout_marginTop="10dp"
        />
</RelativeLayout>

提前感谢您的帮助!我真的需要一个解决办法!
编辑:我注意到了,假设我在第一个edittext中输入“a aa aaaa”,然后它会在列表的下一个edittext中添加“a”,然后在该edittext后面添加“aa”,然后添加“aaa”,“aaaa”,直到它达到“aaaaaaaa”。然后会有一些空的edittext,然后它会重新开始。这是一个视频:Here
编辑X2:已经有一段时间没做过这个了,也没得到任何答案。甚至还有这个职位的“风滚草”徽章!有人有什么想法吗?


最佳答案:

虽然我不太确定,但我相信这是因为回收者的观点重新创造了它的位置。对于您的自定义编辑文本,您正在添加字符串`@override
public void ontextchanged(字符序列s,int start,int before,int count){
courseStrings.add(s.toString());
}
对于两个编辑文本。
在Recyler视图中,您实现了.courseText.addtextChangedListener(courseCustomTextListener);
所以每次你滚动onbind方法的时候都会被调用,而且随着位置的改变,这个方法也必须被调用。因此它必须再生。我要求你先试试

holder.courseText.setTag(position); 

在onBindViewHolder()中。
分别为两个文本尝试。如果确实有效,请尝试
holder.view.setTag(position);

译文:来源   文章分类: java android android-edittext android-recyclerview

相关文章:

java - 分析软件—跟进操作—这叫什么?

java - Log4J 2查找值在加载/存在之前已在配置中使用

java - 在Spring REST集成测试中使用开发人员HSQLDB

java - 如何正确添加监听器以滑行

java - 如何访问存储在Docker容器中的'src / main / resources / someFolder'中的应用程序资源文件?

java - 使用带有Java 8流的交互式调试器的问题

java - 对象可以在OOP中定义新的同级或子类吗?为什么或者为什么不?

java - 在插入MySQL表期间编写特殊异常消息以尝试捕获

java - 延迟(未加载)集合上的JPA调用方法在分离时无法按预期在Eclipselink中运行

java - HSQLDB内部:Hibernate和Integer与Long ID