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

2018-02-09 12:44:48来源:oschina作者:bluecoffee人点击

分享

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


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布局代码:


<?xml version="1.0" encoding="utf-8"?>
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();
}

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台