android 滑动删除按钮,简单方式实现列表滑动显示删除按钮RecyclerView+HorizontalScrollView...

首先定义一个自定义组件:

import android.content.Context;

import android.util.AttributeSet;

import android.util.DisplayMetrics;

import android.util.Log;

import android.util.TypedValue;

import android.view.MotionEvent;

import android.widget.HorizontalScrollView;

import android.widget.LinearLayout;

/**

* Created by zhangda on 2018/1/28.

*/

public class MyRecyclerViewItem extends HorizontalScrollView {

public static boolean isLeft=true;//在左边

public MyRecyclerViewItem(Context context) {

super(context);

init();

}

public MyRecyclerViewItem(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

public MyRecyclerViewItem(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init();

}

private int buttonWidth;

private DisplayMetrics scale;

private void init() {

scale = getContext().getResources().getDisplayMetrics();

buttonWidth = dp2px(100);// 布局的宽度

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

super.onLayout(changed, l, t, r, b);

Log.i(getClass().getSimpleName(), l+"-"+t+"-"+r+"-"+b);

//调整布局

LinearLayout linearLayout= (LinearLayout) getChildAt(0);

LinearLayout linearLayoutLeft = (LinearLayout) linearLayout.getChildAt(0);

LinearLayout linearLayoutRight = (LinearLayout) linearLayout.getChildAt(1);

linearLayout.layout(linearLayout.getLeft(), linearLayout.getTop(), linearLayout.getLeft()+getMeasuredWidth()+ buttonWidth, linearLayout.getBottom());

linearLayoutLeft.layout(linearLayoutLeft.getLeft(), linearLayoutLeft.getTop(),linearLayoutLeft.getLeft()+getMeasuredWidth(), linearLayoutLeft.getBottom());

linearLayoutRight.layout(linearLayoutLeft.getLeft()+getMeasuredWidth(), linearLayoutLeft.getTop(), linearLayoutLeft.getLeft()+getMeasuredWidth()+ buttonWidth, linearLayoutLeft.getBottom());

}

//恢复状态

public void reset(){

isLeft=true;

scrollTo(0,0);

}

@Override

public boolean onTouchEvent(MotionEvent ev) {

if (ev.getAction()== MotionEvent.ACTION_DOWN)

{

Log.i(getClass().getSimpleName(), "down");

return true;

}

if (ev.getAction()==MotionEvent.ACTION_CANCEL || ev.getAction()== MotionEvent.ACTION_UP)

{

Log.i(getClass().getSimpleName(), "up");

int range=70;

if (isLeft)

{

if (getScrollX()>range)

{

isLeft=false;

smoothScrollTo(buttonWidth, 0);

}else {

smoothScrollTo(0,0);

}

}else {

if (getScrollX()< (buttonWidth-range))

{

isLeft=true;

smoothScrollTo(0,0);

}else {

smoothScrollTo(buttonWidth, 0);

}

}

return true;

}

Log.i(getClass().getSimpleName(), "end");

return super.onTouchEvent(ev);

}

private int dp2px(float dpValue){

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, scale);

}

}

RecyclerView 列表布局代码:

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layout_height="match_parent" />

对应item布局代码:

android:id="@+id/scroll_item"

android:scrollbars="none"

android:focusable="true"

android:layout_width="match_parent"

android:layout_height="50dp">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="2dp"

android:orientation="horizontal">

android:id="@+id/content_layout"

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="50dp">

android:id="@+id/item1"

android:text="鱼香肉丝"

android:textSize="18sp"

android:gravity="center"

android:background="@color/colorWhite"

android:textColor="@color/colorFontColor"

android:layout_weight="1"

android:layout_width="match_parent"

android:layout_height="50dp" />

android:id="@+id/item2"

android:text="15:30"

android:textSize="18sp"

android:gravity="center"

android:background="@color/colorWhite"

android:textColor="@color/colorFontColor"

android:layout_weight="1"

android:layout_width="match_parent"

android:layout_height="50dp" />

android:background="@color/colorDelete"

android:gravity="center"

android:orientation="vertical"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:id="@+id/deleteSample"

android:text="删除"

android:textColor="#ffffff"

android:textSize="22sp"

android:layout_width="match_parent"

android:layout_height="match_parent" />

其中item的点击事件和删除按钮的点击时间在RecyclerView的Adapter的onBindViewHolder方法中来写,如下:

@Override

public void onBindViewHolder(final Hodler holder, final int position) {

holder.item1.setWidth(screenWidth/2);

holder.item2.setWidth(screenWidth/2);

holder.deleteSample.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Toast.makeText(KeepSampleListActivity.this, "删除:"+position, Toast.LENGTH_LONG).show();

}

});

holder.content_layout.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

if(holder.recyclerViewItem.isLeft){

Intent i = new Intent();

i.setClass(KeepSampleListActivity.this, SampleDetailActivity.class);

startActivity(i);

}else{

holder.recyclerViewItem.reset();

}

}

});

//恢复状态

holder.recyclerViewItem.reset();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值