Android 左滑删除
左滑功能参考作者:https://blog.youkuaiyun.com/sinat_37585505/article/details/84652667 。我们先来看看效果。
我们再来看看布局.
布局代码仅供参考,这是我写来测试用的。下面的布局是适配器一行的显示的代码。
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hsv"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/btn_textview_select"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/marginSize"
>
<com.xie.com.imoocmusic.views.CircleImageView
android:id="@+id/iv_icon"
android:layout_width="56dp"
android:layout_height="56dp"
android:src="@mipmap/img1"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginLeft="@dimen/marginSize">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="西瓜"
android:textColor="@color/titleColor"
android:textSize="@dimen/titleSize"
android:textStyle="bold"/>
<TextView
android:layout_marginTop="@dimen/marginSize"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="[离 线]"
android:textColor="@color/infoColor"
android:textSize="@dimen/infoSize" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/delete"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:gravity="center"
android:text="Delete"
android:textColor="@color/white"
/>
</LinearLayout>
</HorizontalScrollView >
而左滑功能的主要功能是在adapter中的onBindViewHolder里面实现的。
下面代码仅供参考,很多时候我们需要根据自己的需求实现自己特有的功能。
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Glide.with(mContext)
.load(R.mipmap.img1)
.into(holder.imageView);
//list用来记录itemView是否滑开,避免recyclerview复用出现bug
if (list.contains(position + "")) {
holder.itemView.scrollTo(holder.delete.getWidth(), 0);
} else {
holder.itemView.scrollTo(0, 0);
}
//holder.text.setText(items[position]);
//将item要展示的内容宽度设置为屏幕宽度,删除按钮则会隐藏在屏幕右侧
ViewGroup.LayoutParams layoutParams = holder.text.getLayoutParams();
layoutParams.width = width;
holder.hsv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
//获取滑动的距离
int scrollX = holder.hsv.getScrollX();
//获取右侧删除按钮的宽度
int width = holder.delete.getWidth();
//如果滑动距离超过了删除按钮宽度的1/2,则将删除按钮滑出,否则隐藏
if (scrollX >= width / 2) {
Log.e("xiejinbo","以显示:"+ JSON.toJSONString(list));
holder.itemView.scrollTo(width, 0);
//滑动的item记录下来
list.add(position + "");
} else {
holder.itemView.scrollTo(0, 0);
if (list.contains(position + "")) {
//记录中移除
list.remove(position + "");
}
}
break;
case MotionEvent.ACTION_MOVE:
break;
}
//注意此处返回false
return false;
}
});
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"删除:"+position,Toast.LENGTH_SHORT).show();
holder.itemView.scrollTo(0, 0);
if (list.contains(position + "")) {
//记录中移除
list.remove(position + "");
}
}
});
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"点击好友头像:"+position,Toast.LENGTH_SHORT).show();
}
});
}
目前这个左滑功能还有几处尚未完善的功能。如:当下一个左滑出现时,上一个左滑应该消息;当触摸除了左滑Item之外的区域的时候,左滑应该消失等等。这些功能就需要完善自己的代码逻辑,和左滑本身的功能没有联系了。