新建一个PackActivity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.delivery.DeliveryActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/delivery_pager2"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/radioGroup"
app:layout_constraintTop_toTopOf="parent" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent">
<RadioButton
android:id="@+id/rbHome"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:gravity="center_horizontal"
android:text="首页"/>
<RadioButton
android:id="@+id/rbDiscovery"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:button="@null"
android:gravity="center_horizontal"
android:text="发现"/>
<RadioButton
android:id="@+id/rbContacts"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:button="@null"
android:gravity="center_horizontal"
android:text="通讯录"/>
<RadioButton
android:id="@+id/rbMe"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:button="@null"
android:gravity="center_horizontal"
android:text="我" />
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.pack_mes.ui.pack;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.daimajia.swipe.SwipeLayout;
import com.example.pack_mes.R;
import com.example.pack_mes.model.PackList;
import java.util.ArrayList;
import java.util.List;
public class PackActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<PackList> list = new ArrayList<>();
private SwipeLayout swipeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pack);
recyclerView = findViewById(R.id.recyclerView);
for (int i = 0; i < 50; i++) {
list.add(new PackList("最新发布消息:", "震惊、全美放松防疫···" + i));
}
recyclerView.setAdapter(new PackAdapter(list, recyclerView));
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
RecyclerView的单项布局: pack_item.xml
需要引入-implementation ‘com.daimajia.swipelayout:library:1.2.0’
<?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/pack_swipe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:drag_edge="right">
<LinearLayout
android:id="@+id/right_menu_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/details" />
<TextView
android:id="@+id/modify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/modify" />
<TextView
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/delete" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</com.daimajia.swipe.SwipeLayout>
RecycView的视图模型
PackViewHolder.java
package com.example.pack_mes.ui.pack;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.daimajia.swipe.SwipeLayout;
import com.example.pack_mes.R;
public class PackViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView text;
TextView delete;
TextView details;
TextView modify;
SwipeLayout swipe;
public PackViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
text = itemView.findViewById(R.id.text);
delete = itemView.findViewById(R.id.delete);
details = itemView.findViewById(R.id.details);
modify = itemView.findViewById(R.id.modify);
swipe = itemView.findViewById(R.id.pack_swipe);
}
}
RecyclerView视图适配器
package com.example.pack_mes.ui.pack;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.daimajia.swipe.SwipeLayout;
import com.example.pack_mes.R;
import com.example.pack_mes.model.PackList;
import java.util.List;
public class PackAdapter extends RecyclerView.Adapter<PackViewHolder> {
private List<PackList> list;
private RecyclerView recyclerView;
private SwipeLayout swipeLayout = null;
public PackAdapter(List<PackList> list, RecyclerView recyclerView) {
this.list = list;
this.recyclerView = recyclerView;
}
@NonNull
@Override
public PackViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// parent加载这个view的父布局。传入父布局pack_item才会填满父布局。
// 传入null则pack_item的match_parent找不到父布局则会失效
return new PackViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.pack_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull PackViewHolder holder, int position) {
PackList packList = list.get(position);
holder.title.setText(packList.getTitle());
holder.text.setText(packList.getText());
holder.delete.setOnClickListener(delete(position));
holder.swipe.addSwipeListener(removeSwipe(holder.swipe));
}
@Override
public int getItemCount() {
return list.size();
}
// 返回一个点击事件
private View.OnClickListener delete(int position) {
return view -> {
list.remove(position);
recyclerView.getAdapter().notifyDataSetChanged();
};
}
// 返回一个SwipeLayout滑动事件
private SwipeLayout.SwipeListener removeSwipe(SwipeLayout swipe) {
return new SwipeLayout.SwipeListener() {
@Override
public void onStartOpen(SwipeLayout layout) {
// 判断上次保存的对象是否为空、且不能等于本次操作的swipe对象
// 符合条件关闭上次打开的swipe对象
if (swipeLayout != null && swipeLayout != swipe) {
swipeLayout.close();
}
}
@Override
public void onOpen(SwipeLayout layout) {
swipeLayout = swipe; // 每次打开的对象都保存起来
}
@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 x, float y) {
}
};
}
}