依赖:implementation “com.daimajia.swipelayout:library:1.2.0@aar”
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.daimajia.swipe.SwipeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/swipe">
<LinearLayout
android:tag="right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:orientation="vertical">
<Button
android:layout_gravity="right"
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/delBtn"
android:text="删除"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000ff0">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="一条显示内容" />
</LinearLayout>
</com.daimajia.swipe.SwipeLayout>
</LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private SwipeLayout swipeLayout;
private Button mBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化基本控件
swipeLayout = findViewById(R.id.swipe);
mBtn = findViewById(R.id.delBtn);
mBtn.setOnClickListener(this);
//设置滑动布局
swipeLayout.addDrag(SwipeLayout.DragEdge.Right,swipeLayout.findViewWithTag("right"));
//设置滑动模式laydown->在顶部布局下;pullout->在顶部布局的右侧
swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"顶部被点击",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onClick(View v) {
swipeLayout.close();
Toast.makeText(this,"删除按钮被点击",Toast.LENGTH_SHORT).show();
}
}