android 侧滑删除

  • Post author:
  • Post category:其他




侧滑删除

布局:

<com.zhcy.healthmanager.view.ui.MemorandumLabelView xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="43dp"
 android:layout_marginTop="10dp"
 android:layout_marginBottom="25dp"
 android:background="@android:color/white">

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/white">
 <TextView
 android:id="@+id/tv_memorandum_delete"
 android:layout_width="40dp"
 android:layout_height="30dp"
 android:layout_toRightOf="@+id/rl_memorandum_label"
 android:gravity="center"
 android:layout_centerVertical="true"
 android:background="@drawable/tv_memorandum_remind_delete"
 android:text="删 除"
 android:textColor="@color/white"
 android:textSize="13dp" />
 <TextView
 android:id="@+id/tv_interval"
 android:layout_width="15dp"
 android:layout_height="43dp"
 android:layout_toRightOf="@id/tv_memorandum_delete"

 />

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/rl_memorandum_label"
 android:layout_width="match_parent"
 android:layout_height="match_parent"

 android:background="@color/white">

 <TextView
 android:id="@+id/tv_co"
 android:layout_width="6dp"
 android:layout_height="43dp"
 android:layout_marginLeft="35dp"
 android:background="@color/colorPrimary" />

 <TextView
 android:id="@+id/tv_memorandum_title"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="35dp"
 android:layout_toRightOf="@id/tv_co"
 android:maxEms="13"
 android:singleLine="true"
 android:ellipsize="end"
 android:textSize="14sp"
 android:textColor="@color/gray6" />

 <ImageView
 android:id="@+id/iv_memorandum_clock"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@id/tv_memorandum_title"
 android:layout_alignLeft="@id/tv_memorandum_title"
 android:layout_marginTop="13dp"
 android:layout_toRightOf="@id/tv_co"
 android:src="@drawable/clock_" />

 <TextView
 android:id="@+id/tv_memorandum_date"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@id/tv_memorandum_title"
 android:layout_marginLeft="5dp"
 android:layout_marginTop="9dp"
 android:layout_toRightOf="@id/iv_memorandum_clock"
 android:text="456"
 android:textSize="14sp"
 android:textColor="@color/gray6" />

 <TextView
 android:id="@+id/tv_memorandum_time"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@id/tv_memorandum_title"
 android:layout_marginLeft="5dp"
 android:layout_marginTop="9dp"
 android:layout_toRightOf="@id/tv_memorandum_date"
 android:text="789"
 android:textSize="14sp"
 android:textColor="@color/gray6" />
 </RelativeLayout>



 </RelativeLayout>
</com.zhcy.healthmanager.view.ui.MemorandumLabelView> 

这个布局最外层包裹的是一个自定义view,view里面确定可以滑动‘。。。。,包括布局结束的位置以删除按钮为尾(注意这里,最后结尾肯定是删除按钮顶着最后,滑动完之后停留可能只显示半个按钮,具体可以自己在view里面改动)。现在奉上自定义view:

public class MemorandumLabelView extends HorizontalScrollView {
private TextView mTextView_Delete;//删除按钮

 private TextView mTextInterval;

private int mScrollWidth;//记录滚动条可以滚动的距离

 private Boolean once = false;//在onMeasure中只执行一次的判断

 private Boolean isOpen = false;//记录按钮菜单是否打开,默认关闭false

 private IonSlidingButtonListener mIonSlidingButtonListener;//自定义的接口,用于传达滑动事件等


 /**
 * 1.构造方法
 */
 public MemorandumLabelView(Context context) {
super(context, null);
 }

public MemorandumLabelView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
 }

public MemorandumLabelView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setOverScrollMode(OVER_SCROLL_NEVER);
 }


//2.在onMeasure中先取得作为“设置”、“删除”按钮的TextView
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (!once) {
mTextView_Delete = (TextView) findViewById(R.id.tv_memorandum_delete);
mTextInterval = (TextView)findViewById(R.id.tv_interval);
once = true;
 }
 }


//3.在onLayout中使Item在每次变更布局大小时回到初始位置,并且获取滚动条的可移动距离
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (changed) {
this.scrollTo(0, 0);

//获取水平滚动条可以滑动的范围,即右侧“设置”、“删除”按钮的总宽度
 mScrollWidth = mTextView_Delete.getWidth()+mTextInterval.getWidth() ;
 }
 }


//4.滑动监听,按滑动的距离大小控制菜单开关
 @Override
 public boolean onTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN://按下
 case MotionEvent.ACTION_MOVE://移动
 mIonSlidingButtonListener.onDownOrMove(this);
break;
case MotionEvent.ACTION_UP://松开
 case MotionEvent.ACTION_CANCEL:
 changeScrollx();
return true;
default:
break;
 }
return super.onTouchEvent(ev);
 }


/**
 * 5.
 * @param l
 * @param t
 * @param oldl
 * @param oldt
 */
 @Override
 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);

//改变view的在x轴方向的位置
 mTextView_Delete.setTranslationX(1);
 }


/**
 * 6.按滚动条被拖动距离判断关闭或打开菜单
 * getScrollX() view的左上角相对于母视图的左上角的X轴偏移量
 * smoothScrollTo(x, y); 参数:相对于ScrollView左上角的位置来说,你要移动的位置
 */
 public void changeScrollx() {
if (getScrollX() >= (mScrollWidth / 2)) {
this.smoothScrollTo(mScrollWidth, 0);
isOpen = true;
mIonSlidingButtonListener.onMenuIsOpen(this);
 } else {
this.smoothScrollTo(0, 0);
isOpen = false;
 }
 }

/**
 * 7.打开菜单
 */
 public void openMenu() {
if (isOpen) {
return;
 }
this.smoothScrollTo(mScrollWidth, 0);//相对于原来没有滑动的位置x轴方向偏移了mScrollWidth,y轴方向没有变化。
 isOpen = true;
mIonSlidingButtonListener.onMenuIsOpen(this);
 }

/**
 * 8.关闭菜单
 */
 public void closeMenu() {
if (!isOpen) {
return;
 }
this.smoothScrollTo(0, 0);//相对于原来没有滑动的位置,x轴方向、y轴方向都没有变化,即回到原来的位置了。
 isOpen = false;
 }


/**
 * 9.接口定义及注册方法
 */
 public void setSlidingButtonListener(IonSlidingButtonListener listener) {
mIonSlidingButtonListener = listener;
 }

public interface IonSlidingButtonListener {

//该方法在Adapter中实现
 void onMenuIsOpen(View view);//判断菜单是否打开

 void onDownOrMove(MemorandumLabelView leftSlideView);//滑动或者点击了Item监听
 }

}

设置内容布局的宽为屏幕宽度的工具类:

public class MemorandumUtils {
public static int dp2px(Context context, float dp)
 {
return (int ) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
 }

/**
 * 获得屏幕宽度
 *
 * @param context
 * @return
 */
 public static int getScreenWidth(Context context)
 {
 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE );
 DisplayMetrics outMetrics = new DisplayMetrics();
 wm.getDefaultDisplay().getMetrics( outMetrics);
return outMetrics .widthPixels ;
 }
}

适配器:


public class NowTimeTimeIntervalRecyclerViewAdapter extends RecyclerView.Adapter<NowTimeTimeIntervalRecyclerViewAdapter.ViewHolder> {
private List<NowTimeTimeInterval> lists = null;
private Context context;

public NowTimeTimeIntervalRecyclerViewAdapter(Context context, List<NowTimeTimeInterval> lists) {
this.context = context;
this.lists = lists;
 }

@Override
 public NowTimeTimeIntervalRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 View view = LayoutInflater.from(context).inflate(R.layout.nowtime_timeinterval_son, parent, false);
 ViewHolder holder = new ViewHolder(view);
return holder;
 }

public interface OnItemClikListener {
void onItemClik(View view, int position);
 }

private OnItemClikListener mOnItemClikListener;

public void setItemClikListener(OnItemClikListener mOnItemClikListener) {
this.mOnItemClikListener = mOnItemClikListener;
 }

@Override
 public void onBindViewHolder(NowTimeTimeIntervalRecyclerViewAdapter.ViewHolder holder, int position) {
 holder.sonTimeText.setText(lists.get(position).getTimeName());
if (lists.get(position).getSelect()) {
 holder.sonTimeText.setTextColor(context.getResources().getColor(R.color.whit));
 holder.sonTimeText.setBackgroundResource(R.drawable.now_time_tv_frame_select);

 } else {
 holder.sonTimeText.setTextColor(context.getResources().getColor(R.color.now_time_no_text_week));
 holder.sonTimeText.setBackgroundResource(R.drawable.now_time_tv_frame);
 }
if (mOnItemClikListener != null) {
 holder.sonTimeText.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View view) {
int pos = holder.getLayoutPosition();
mOnItemClikListener.onItemClik(holder.itemView, pos);
if (lists.get(pos).getSelect()) {
holder.sonTimeText.setTextColor(context.getResources().getColor(R.color.now_time_no_text_week));
holder.sonTimeText.setBackgroundResource(R.drawable.now_time_tv_frame);
lists.get(pos).setSelect(false);
 } else {
holder.sonTimeText.setTextColor(context.getResources().getColor(R.color.whit));
holder.sonTimeText.setBackgroundResource(R.drawable.now_time_tv_frame_select);
lists.get(pos).setSelect(true);
 }
 }
 });
 }

 }
public List<NowTimeTimeInterval> getList(){
return lists;
 }

@Override
 public int getItemCount() {
return lists.size();
 }

public class ViewHolder extends RecyclerView.ViewHolder {
private TextView sonTimeText;

public ViewHolder(View itemView) {
super(itemView);
sonTimeText = (TextView) itemView.findViewById(R.id.tv_time_son);
 }
 }
}


在主activity中:
public class MemorandumRemindingActivity extends BaseActivity implements MemorandumRecyclerViewAdapter.IonSlidingViewClickListener, IMemorandumRemindingAView {
@Inject
 MemorandumRemindingAPresenter memorandumRemindingAPresenter;

private FloatingActionButton fabMemorandumRemindAdd;
private List<MemorandumRemind> remindList;
private MemorandumRecyclerViewAdapter memorandumRecyclerViewAdapter;
private RecyclerView recyclerViewLabel;

public static Intent getStartIntent(Context context) {
 Intent intent = new Intent(context, MemorandumRemindingActivity.class);
return intent;
 }

@Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 initView();
 initDate();
recyclerViewLabel.setLayoutManager(new LinearLayoutManager(this));

memorandumRecyclerViewAdapter = new MemorandumRecyclerViewAdapter(this, remindList);
recyclerViewLabel.setAdapter(memorandumRecyclerViewAdapter);
recyclerViewLabel.setItemAnimator(new DefaultItemAnimator());

 }

@Override
 public int getLayout() {
return R.layout.memorandumreminding_activity;
 }

@Override
 protected void inject(ActivityComponent activityComponent) {
 activityComponent.inject(this);
 }

@Override
 protected void attachView() {
memorandumRemindingAPresenter.attachView(this);
 }

@Override
 protected void detachPresenter() {
memorandumRemindingAPresenter.detachView();
 }

private void initView() {
recyclerViewLabel = (RecyclerView)findViewById(R.id.rv_memorandum_label) ;
fabMemorandumRemindAdd = (FloatingActionButton) findViewById(R.id.fab_memorandum_remind_add_label);
fabMemorandumRemindAdd.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.colorPrimary)));
fabMemorandumRemindAdd.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View view) {
 startActivity(MemorandumRemindSetUpActivity.getStartIntent(MemorandumRemindingActivity.this, true, 99999));
 }
 });


 }

private void initDate() {
remindList = memorandumRemindingAPresenter.queryAllLabel();

 }

@Override
 protected void onStart() {
super.onStart();
remindList = memorandumRemindingAPresenter.queryAllLabel();
memorandumRecyclerViewAdapter = new MemorandumRecyclerViewAdapter(this, remindList);
recyclerViewLabel.setAdapter(memorandumRecyclerViewAdapter);
 }


@Override
 public void onItemClick(View view, int position) {
 startActivity(MemorandumRemindSetUpActivity.getStartIntent(this,false,remindList.get(position).getId()));
 }

@Override
 public void onDeleteBtnCilck(View view, int position) {
memorandumRemindingAPresenter.deleteLabelById(remindList.get(position).getId());
memorandumRecyclerViewAdapter.removeData(position);
 }
} 
  



版权声明:本文为weixin_42716507原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。