滑动即可从recyclerView中删除或编辑项目

时间:2019-05-21 01:05:41

标签: android android-recyclerview android-swipe

我正在创建一个简单的滑动来删除或编辑recyclerView中的项目,但是出现错误:

java.lang.IllegalStateException:指定的子代已经有一个父代。您必须先在孩子的父母上调用removeView()。

///我在YouTube上找到了此代码 //我已经搜索了所有相同的问题,但没有找到解决方法

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);
    }
}

1 个答案:

答案 0 :(得分:-1)

您可以使用具有相同目的的该库

// android回收站刷卡

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

或请发布该错误的日志