package com.ghl.slidingdelete;
import com.nineoldandroids.view.ViewHelper;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class SlidingDelete extends HorizontalScrollView {
private LinearLayout mWapper;
private ViewGroup mContent;
private ViewGroup mDelete;
private int mScreenWidth;
private int mDeleteWidth=50;
private boolean once;//只计算一次
private boolean isopen;//删除按钮是否显示
public SlidingDelete(Context context) {
this(context,null);
// TODO Auto-generated constructor stub
}
public SlidingDelete(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
//获取屏幕宽度
WindowManager wm=(WindowManager)context.getSystemService(context.WINDOW_SERVICE);
DisplayMetrics outMetrics=new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
mScreenWidth=outMetrics.widthPixels;
//获取滑动删除按钮宽度,自定义属性
TypedArray array=context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlidingDelete, defStyle,0);
int n=array.getIndexCount();
for (int i = 0; i <n; i ) {
int attr=array.getIndex(i);
switch (attr) {
case R.styleable.SlidingDelete_deletewidth:
mDeleteWidth=array.getDimensionPixelSize(attr, (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics()));
break;
}
}
array.recycle();
}
public SlidingDelete(Context context, AttributeSet attrs) {
this(context, attrs,0);
// TODO Auto-generated constructor stub
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 布局视图
int mTotalWidth=0;
int childcount=mWapper.getChildCount();
//Log.v("Childcount", childcount "");
for (int i = 0; i <childcount; i ) {
View childView=mWapper.getChildAt(i);
int measureWidth=childView.getMeasuredWidth();
int measureHeight=childView.getMeasuredHeight();
childView.layout(mTotalWidth,t, measureWidth mTotalWidth, measureHeight);
mTotalWidth =measureWidth;
}
super.onLayout(changed, l, t, r, b);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 计算宽高
if(!once){
mWapper=(LinearLayout)getChildAt(0);
mContent=(ViewGroup)mWapper.getChildAt(0);
mDelete=(ViewGroup)mWapper.getChildAt(1);
mContent.getLayoutParams().width=mScreenWidth;
mDelete.getLayoutParams().width=mDeleteWidth;
once=true;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/*
@Override
public boolean onTouchEvent(MotionEvent ev) {
// 滑动触摸事件
Log.v("TouchEvent", "触摸事件");
int action=ev.getAction();
switch (action) {
case MotionEvent.ACTION_UP:
//得到隐藏宽度,左边为正,右边为负
int scrollx=getScrollX();
if(scrollx>=mDeleteWidth/2){
this.smoothScrollTo(0, 0);
isopen=false;
}else{
this.smoothScrollTo(mDeleteWidth, 0);
isopen=true;
}
break;
}
return super.onTouchEvent(ev);
}*/
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// 对滑动监听,l的值就是srcollX的值
//抽屜式效果,3.0以下要引入nineoldandroid包
ViewHelper.setTranslationX(mDelete, l);
super.onScrollChanged(l, t, oldl, oldt);
}
}
评论