1

I'm creating a simple swipe to delete or edit an item in recyclerView but I have an error :

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

//I've found this code on Youtube //I've search in all the same problems and I didn't found a solution

note_item.xml

      <?xml version="1.0" encoding="utf-8"?>
   <com.daimajia.swipe.SwipeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:leftEdgeSwipeOffset="0dp"
app:rightEdgeSwipeOffset="0dp">
 <android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <TextView
        android:id="@+id/text_view_heure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_view_info"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="7dp"
        android:layout_marginEnd="19dp"
        android:layout_marginRight="19dp"
        android:text="Heure"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="@color/a"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/text_view_date"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_view_heure"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="-26dp"
        android:layout_marginEnd="140dp"
        android:layout_marginRight="148dp"
        android:layout_toLeftOf="@id/text_view_heure"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Date"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="@color/a"
        android:textSize="18dp" />

    <TextView
        android:id="@+id/text_view_info"
        android:layout_width="366dp"
        android:layout_height="34dp"

        android:text="Information" />

</RelativeLayout>


<LinearLayout
    android:id="@+id/bottom_wraper"
    android:layout_width="match_parent"
    android:layout_height="68dp"
    android:orientation="horizontal"
    android:weightSum="2">

    <TextView
        android:id="@+id/mod"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#0076a5"
        android:gravity="center"
        android:text="Modifier"
        android:textColor="#fff" />

    <TextView
        android:id="@+id/sup"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ff0000"
        android:gravity="center"
        android:text="Supprimer"
        android:textColor="#fff" />

</LinearLayout>


  </android.support.v7.widget.CardView>
 </com.daimajia.swipe.SwipeLayout>

NotesActivity.java

public class NotesActivity extends AppCompatActivity {
private  FloatingActionButton btn_add_note;
public static final int ADD_NOTE_REQUEST = 1;
private RecyclerView recyclerView;
private List<Note> notes = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notes);
    btn_add_note = (FloatingActionButton) findViewById(R.id.btn_add_note);
    btn_add_note.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(NotesActivity.this, 
   NewNoteActivity.class);
            startActivityForResult(intent,ADD_NOTE_REQUEST);
        }
    });
     recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    Note note1 = new Note("20/05/2019","23:50","Je t'aime my liitle boy");
    Note note2 = new Note("21/05/2019","23:50","Je t'aime my liitle 
    girl");
    notes.add(note1); notes.add(note2);
    final RecyclerSwipeViewAdapter adapter = new 
     RecyclerSwipeViewAdapter();

    ((RecyclerSwipeViewAdapter) adapter).setMode(Attributes.Mode.Single);
    adapter.setNotes(notes);
    recyclerView.setAdapter(adapter);


      }

RecyclerSwipeViewAdapter.java

 public class RecyclerSwipeViewAdapter extends 
 RecyclerSwipeAdapter<RecyclerSwipeViewAdapter.SimpleViewHolder> {

private List<Note> notes = new ArrayList<>();

@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item, parent, false);
    return new SimpleViewHolder(view);
}

@Override
public void onBindViewHolder(SimpleViewHolder holder, int position) {
    Note currentNote = notes.get(position);
    holder.info.setText(currentNote.getInformation());
    holder.date.setText(currentNote.getDate());
    holder.heure.setText(String.valueOf(currentNote.getHeure()));
    holder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
    holder.swipeLayout.addDrag(SwipeLayout.DragEdge.Right, 
    holder.swipeLayout.findViewById(R.id.bottom_wraper));
    holder.swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
        @Override
        public void onStartOpen(SwipeLayout layout) {

        }

        @Override
        public void onOpen(SwipeLayout layout) {

        }

        @Override
        public void onStartClose(SwipeLayout layout) {

        }

        @Override
        public void onClose(SwipeLayout layout) {

        }

        @Override
        public void onUpdate(SwipeLayout layout, int leftOffset, int 
          topOffset) {

        }

        @Override
        public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

        }
    });

    holder.swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });



}

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

@Override
public int getSwipeLayoutResourceId(int position) {
    return R.id.swipe;
}

public void setNotes (List<Note> notes)
{
    this.notes = notes ;

}

public class SimpleViewHolder extends RecyclerView.ViewHolder {
    public SwipeLayout swipeLayout;
    private TextView info;
    private TextView heure;
    private TextView date;
    public TextView supprimer;
    public TextView modifier;

    public SimpleViewHolder(View itemView)
    {
        super(itemView);
        swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
        info = itemView.findViewById(R.id.text_view_info);
        heure = itemView.findViewById(R.id.text_view_heure);
        date = itemView.findViewById(R.id.text_view_date);
        modifier = (TextView) itemView.findViewById(R.id.mod);
        supprimer = (TextView) itemView.findViewById(R.id.sup);
    }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Monir
  • 53
  • 2
  • 9
  • 1
    here is the links which help you [Link-1](https://medium.com/@zackcosborn/step-by-step-recyclerview-swipe-to-delete-and-undo-7bbae1fce27e) [Link-2](https://codeburst.io/android-swipe-menu-with-recyclerview-8f28a235ff28) Happy learning... – Arbaz.in May 21 '19 at 08:29
  • I've copy the same code I've edit some instruction but I have one probleme the swipe doesn't stop so I can't clicked on the button could you help me on this ? Thank fot those links – Monir May 21 '19 at 23:15
  • It function only on the right swipe – Monir May 22 '19 at 01:04
  • try this it will fulfill your requirements [Link](https://codeburst.io/android-swipe-menu-with-recyclerview-8f28a235ff28) – Arbaz.in May 22 '19 at 03:53

1 Answers1

0

You can use this library which serves the same purpose

//android recycler swipe

implementation  'com.github.alexandrius:accordion-swipe-layout:0.5.0' 

or please post the logcat of that error

Mohan Sai Manthri
  • 2,808
  • 1
  • 11
  • 26
  • if you give information to user should it in to comment section not in answer – Arbaz.in May 22 '19 at 03:54
  • I have given an answer because my reputation is too low to add a comment and I just want to help him, I hope you understand the situation. – Mohan Sai Manthri May 22 '19 at 05:02
  • And @Arbaz.in If you have any idea about coroutineWorker and Work manager, please help me with my problem. [link](https://stackoverflow.com/questions/56220613/my-app-freezes-when-i-make-a-network-request-in-background-thread-using-corouti) – Mohan Sai Manthri May 22 '19 at 05:09