Swipe left to display more, or use listView to quickly realize left swipe to delete layout
renderings
Simple to use
Warehouse address GitHub - IHoveYou/LeftSlideView: A useful left-swipe delete control, supports left-swipe layout, and supports direct preview
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.IHoveYou:LeftSlideView:v1.1'
}
LeftSlideHorizontalScrollView is a sliding control View inherited from HorizontalScrollView
Two arbitrary Views are wrapped in the LeftSlideView control. The left View is the content area, and the right View is the left-sliding display area.
The view can be of any type or ViewGroup, with a high degree of freedom and any layout can be placed
The effect can be previewed directly
<com.example.leftslideview.LeftSlideHorizontalScrollView
android:scrollbars="none"
android:id="@+id/leftSlideHorizontalScrollView"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="40dp"
>
<com.example.leftslideview.LeftSlideView
android:id="@+id/leftSlideView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:paddingHorizontal="15dp"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="match_parent">
<TextView
android:gravity="center_vertical"
android:text="23333"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:id="@+id/llLeftSlidButton"
android:background="@color/teal_700"
android:layout_width="50dp"
android:layout_height="match_parent"/>
</com.example.leftslideview.LeftSlideView>
</com.example.leftslideview.LeftSlideHorizontalScrollView>
LeftSlideHorizontalScrollView 的API
/**
* 触摸回调
*/
interface MoveClickListener {
/**
* 移动中回调
* @param leftSlideHorizontalScrollView
* @param moveX x轴移动距离
*/
fun onMoveClickListener(leftSlideHorizontalScrollView: LeftSlideHorizontalScrollView?, moveX: Int)
/**
* 移动结束
* @param leftSlideHorizontalScrollView
* @param isRollOut 当前状态 true 展开 false 收起
*/
fun onUpClickListener(leftSlideHorizontalScrollView: LeftSlideHorizontalScrollView?, isRollOut: Boolean)
}
//设置滑动监听
void setMoveClickListener(MoveClickListener onListener);
//手动初始化布局 执行关闭无动画
void initScrollow();
//手动设置按钮展开状态 true 展开 false关闭
void setRollOut(boolen isRollOut)
//获取当前布局显示状态 展开状态 true 展开 false关闭
boolean getRollOut()
The list layout slides left to expand the control class LeftSlideUtils
addView Add a View that needs to be linked. Only one view can be expanded at the same time.
Realization principle
LeftSlideHorizontalScrollView 继承 HorizontalScrollView
Listen to the user's finger movement event to record the user's click position, movement direction, and calculate the movement distance after releasing it to determine whether to expand
LeftSlideView inherits ViewGrop
Rewrite the onMeasure method to calculate the actual available width of the control in the method, set the content layout width to the actual width, and obtain the layout width of the right button, the actual available position + the available width on the right = the actual overall width of LeftSlideView, cooperate with LeftSlideHorizontalScrollView to achieve left and right sliding, through When clicked, get the layout width of the button to calculate the expansion and collapse animation.
Relay the onLayout method of LeftSlideView to rearrange the sub-controls, place the content area on the left display position, and place the button area on the right folding position. The specific code implementation can be downloaded from the code warehouse for viewing and learning.