Swipe Recycler View会删除滑动的项目,但会再次添加最后一项

时间:2018-01-30 09:22:32

标签: java android android-activity android-recyclerview swipe

我有一项活动,在创建时我创建了五个文件,然后读取这些文件并添加到我的recyclerview。我还在我的recyclerview中添加了滑动功能。刷新项目时,它会删除该位置的回收站视图的单元格,并删除我创建的文件。我面临的问题是假设我的recyclerview中有五个项目1,2,3,4和5,我刷了删除1,包含1的文件被删除但是我的recyclerview 5在我刷卡时再次添加1. 2,3和4也会发生同样的事情.5一旦细胞被刷过,它就会不断添加。

以下是我的代码

public class MyRecordingsActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecordingsAdapter recordingsAdapter;
    private ArrayList<Recording>  recordingArrayList;
    private File userRecordingFile;
    private static final String USER_MIX_DIR = "UserMix";
    private ArrayList<String> arrayList;


    ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0,
            ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
            Toast.makeText(getApplicationContext(),"Removed"+viewHolder.getAdapterPosition(),Toast.LENGTH_LONG).show();
            final int position = viewHolder.getAdapterPosition();

            recordingsAdapter.notifyItemRemoved(position);

            File file= new File(getExternalCacheDir() +"/"+ USER_MIX_DIR);
            file=new File(file.getAbsolutePath(),"/Recording" + String.valueOf(position-1) + ".txt");
            file.delete();
        }
    };

    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);

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

        for(int i=0;i<5;i++){
            try{
                userRecordingFile = new File(createRecordingFiles(), "Recording"+i+".txt");
                FileWriter writer = new FileWriter(userRecordingFile);
                writer.append("DEF"+i+"__HIJ "+i+"__\n");
              //  writer.append("R.drawable.ic_launcher\nDEF"+i+"\nHIJ "+i);
                writer.flush();
                writer.close();
            }
            catch (Exception e){
                e.printStackTrace();
            }

        }

        getSupportActionBar().hide();

        recordingArrayList=new ArrayList<>();
        recyclerView=findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        recyclerView.setHasFixedSize(true);
        itemTouchHelper.attachToRecyclerView(recyclerView);
        readFiles();

        recordingArrayList.clear();

       // if(arrayList.get(0)!=null && arrayList.get(1)!=null){
            recordingArrayList.add(new Recording(R.drawable.ic_launcher,arrayList.get(0),arrayList.get(1)));
        //}
       // if(arrayList.get(2)!=null && arrayList.get(3)!=null){
            recordingArrayList.add(new Recording(R.drawable.ic_launcher,arrayList.get(2),arrayList.get(3)));
       // }
       // if(arrayList.get(4)!=null && arrayList.get(5)!=null){
            recordingArrayList.add(new Recording(R.drawable.ic_launcher,arrayList.get(4),arrayList.get(5)));
        //}
        //if(arrayList.get(6)!=null && arrayList.get(7)!=null){
            recordingArrayList.add(new Recording(R.drawable.ic_launcher,arrayList.get(6),arrayList.get(7)));
        //}
        //if(arrayList.get(8)!=null && arrayList.get(9)!=null){
            recordingArrayList.add(new Recording(R.drawable.ic_launcher,arrayList.get(8),arrayList.get(9)));
        //}

        recordingsAdapter=new RecordingsAdapter(recordingArrayList);
        recyclerView.setAdapter(recordingsAdapter);
    }



    public  File createRecordingFiles() {

        File dirRoot = getExternalCacheDir();

        File workDir = new File(dirRoot, USER_MIX_DIR);
        Log.i("Hello", "createRecordingFiles: "+workDir);
        if (!workDir.exists()) {
            workDir.mkdirs();

        }
        return workDir;
    }


    public void readFiles(){
        StringBuilder text = new StringBuilder();
        BufferedReader br=null;
        try {
            File dirRoot = getExternalCacheDir();
            File workDir = new File(dirRoot, USER_MIX_DIR);

            for(int i=0;i<5;i++){
                File file = new File(workDir,"Recording"+i+".txt");

                br = new BufferedReader(new FileReader(file));
                String line;
                while ((line = br.readLine()) != null) {
                    text.append(line);

                  //  text.append('\n');
                    arrayList=new ArrayList<>(Arrays.asList(text.toString().split("__")));
                   // Toast.makeText(this, ""+arrayList.size(), Toast.LENGTH_SHORT).show();

                }
            }
            br.close() ;
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

如果您不从附加到适配器的列表中删除该项,则调用Adapter.notifyItemRemoved(x)将不会执行任何操作。

final int position = viewHolder.getAdapterPosition();
recordingArrayList.remove(position);
recordingsAdapter.notifyItemRemoved(position);