Android 사용자 정의 SwipeLayout QQ 사 이 드 슬라이더 항목 모방
16150 단어 AndroidSwipeLayout옆으로 미끄러지다
먼저 움 직 이 는 그림 을 보다.
레이아웃 파일 보기
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="www.weshared.qqcehua.MainActivity">
<include layout="@layout/swipelayout" />
</RelativeLayout>
swipelayout.xml
<?xml version="1.0" encoding="utf-8"?>
<www.weshared.qqcehua.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipelayout"
android:layout_width="match_parent"
android:layout_height="72dp">
<!--0 -->
<TextView
android:id="@+id/back_left_tv_mark"
android:layout_width="84dp"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_dark"
android:gravity="center"
android:text="Mark"
android:textColor="@android:color/white"
android:textSize="20sp" />
<!--1 -->
<LinearLayout
android:id="@+id/back_right_ll"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:orientation="horizontal">
<TextView
android:id="@+id/back_right_tv_call"
android:layout_width="84dp"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_dark"
android:gravity="center"
android:text="Call"
android:textColor="@android:color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/back_right_tv_delete"
android:layout_width="84dp"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:text="Delete"
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout>
<!--2 content-->
<TextView
android:id="@+id/front_tv_content"
android:layout_width="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_height="match_parent"
android:background="#666666" />
</www.weshared.qqcehua.SwipeLayout>
MainActivity 에서.
public class MainActivity extends AppCompatActivity {
private SwipeLayout mSwipeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
mSwipeLayout = (SwipeLayout) findViewById(R.id.swipelayout);
mSwipeLayout.setOnClickListener(new SwipeLayout.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.back_left_tv_mark:
toast("mark");
break;
case R.id.front_tv_content:
toast("content");
break;
case R.id.back_right_tv_call:
toast("call");
break;
case R.id.back_right_tv_delete:
toast("delete");
break;
}
}
});
}
public void toast(String message) {
Toast toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
if (!TextUtils.isEmpty(message) && toast != null) {
toast.setText(message);
toast.show();
}
}
}
사용자 정의 SwipeLayout 컨트롤
public class SwipeLayout extends FrameLayout {
private View mBackLeftView;
private ViewGroup mBackRightView;
private View mFrontView;
private int mWidth;
private int mHeight;
private int mLeftRange;
private int mRightRange;
private int status;
public final int NORMAL = 0;//
public final int LEFT_OPEN = 1;//
public final int RIGHT_OPEN = 2;//
public final int LEFT_OPENING = 3;//
public final int LEFT_CLOSING = 4;//
public final int RIGHT_OPENING = 5;//
public final int RIGHT_CLOSING = 6;//
private ViewDragHelper mViewDrawHelper;
private final int V = 300;//
private OnSwipeListener mOnSwipeListener;
private OnClickListener mOnClickListener;
private View mBackRightCall;
private View mBackRightDelete;
private int x;
private boolean isClick;
private final int DX = 10;
public interface OnSwipeListener {
void onLeftOpen(SwipeLayout swipeLayout);
void onLeftClose(SwipeLayout swipeLayout);
void onRightOpen(SwipeLayout swipeLayout);
void onRightClose(SwipeLayout swipeLayout);
}
public interface OnClickListener {
void onClick(View view);
}
public SwipeLayout(Context context) {
super(context);
init();
}
public SwipeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
status = NORMAL;// NORMAL
mViewDrawHelper = ViewDragHelper.create(this, callback);
}
public void setOnSwipeListener(OnSwipeListener mOnSwipeListener) {
this.mOnSwipeListener = mOnSwipeListener;
}
public void setOnClickListener(OnClickListener mOnClickListener) {
this.mOnClickListener = mOnClickListener;
}
private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
/** view*/
@Override
public boolean tryCaptureView(View child, int pointerId) {
if (child == mBackLeftView) {
return false;
}
return true;
}
/** */
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
if (child == mFrontView) {
if (left < -mRightRange) {
left = -mRightRange;
} else if (left > mLeftRange) {
left = mLeftRange;
}
} else if (child == mBackRightView) {
if (left < mWidth - mRightRange) {
left = mWidth - mRightRange;
} else if (left > mWidth) {
left = mWidth;
}
}
return left;
}
/** */
@Override
public void onViewDragStateChanged(int state) {
super.onViewDragStateChanged(state);
if (state == ViewDragHelper.STATE_IDLE) {//ViewDrawHelper
} else if (state == ViewDragHelper.STATE_DRAGGING) {//ViewDrawHelper
// ,
} else if (state == ViewDragHelper.STATE_SETTLING) {//ViewDrawHelper
// ,
}
}
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
super.onViewPositionChanged(changedView, left, top, dx, dy);
if (mFrontView == changedView) {
mBackRightView.offsetLeftAndRight(dx);
} else if (mBackRightView == changedView) {
mFrontView.offsetLeftAndRight(dx);
}
status = updateStatus();//
invalidate();//
}
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
int left = mFrontView.getLeft();
if (left < -mRightRange * 0.5f) {
if (xvel > V) {
normalClose();
} else {
rightOpen();
}
} else if (left >= -mRightRange * 0.5f && left <= 0) {
if (xvel < -V) {//
rightOpen();//
} else {
normalClose();
}
} else if (left > 0 && left <= mLeftRange * 0.5f) {
if (xvel > V) {
leftOpen();
} else {
normalClose();
}
} else if (left > mLeftRange * 0.5f && left <= mLeftRange) {
if (xvel < -V) {//
normalClose();
} else {
leftOpen();
}
}
}
};
public void dispatchClickListener() {
//
if (status == LEFT_OPEN) {
//mark Content
if (mOnClickListener != null) {
if (x > 0 && x < mLeftRange) {
mOnClickListener.onClick(mBackLeftView);
}
}
} else if (status == RIGHT_OPEN) {
//call Delete Content
if (mOnClickListener != null) {
if (x > mWidth - mRightRange && x < mWidth - mRightRange * 0.5f) {
mOnClickListener.onClick(mBackRightCall);
} else if (x >= mWidth - mRightRange * 0.5f && x <= mWidth) {
mOnClickListener.onClick(mBackRightDelete);
}
}
} else if (status == NORMAL) {
//content
if (mOnClickListener != null) {
mOnClickListener.onClick(mFrontView);
}
}
}
private int updateStatus() {
int left = mFrontView.getLeft();
if (left == -mRightRange) {
status = RIGHT_OPEN;
} else if (left == 0) {
status = NORMAL;
} else if (left == mLeftRange) {
status = LEFT_OPEN;
}
return status;
}
public void leftOpen() {
leftOpen(true);
}
public void leftOpen(boolean isSmooth) {
int finalLeft = mLeftRange;
int finalTop = 0;
if (isSmooth) {
if (mViewDrawHelper.smoothSlideViewTo(mFrontView, finalLeft, finalTop)) {
ViewCompat.postInvalidateOnAnimation(this);
}
} else {
layoutContent(LEFT_OPEN);
}
}
public void normalClose() {
normalClose(true);
}
public void normalClose(boolean isSmooth) {
int finalLeft = 0;
int finalTop = 0;
if (isSmooth) {
if (mViewDrawHelper.smoothSlideViewTo(mFrontView, finalLeft, finalTop)) {
ViewCompat.postInvalidateOnAnimation(this);
}
} else {
layoutContent(NORMAL);
}
}
public void rightOpen() {
rightOpen(true);
}
public void rightOpen(boolean isSmooth) {
int finalLeft = -mRightRange;
int finalTop = 0;
if (isSmooth) {
if (mViewDrawHelper.smoothSlideViewTo(mFrontView, finalLeft, finalTop)) {
ViewCompat.postInvalidateOnAnimation(this);
}
} else {
layoutContent(RIGHT_OPEN);
}
}
@Override
public void computeScroll() {
super.computeScroll();
if (mViewDrawHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mViewDrawHelper.shouldInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
dispatchOnTouchEvent(event);//
try {
mViewDrawHelper.processTouchEvent(event);// ViewDrawHelper
} catch (Exception e) {
}
return true;
}
public void dispatchOnTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
x = (int) event.getX();
isClick = true;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
int movex = (int) event.getX();
if (Math.abs(movex - x) > DX) {// ,
isClick = false;
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (isClick) {
dispatchClickListener();
}
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
//
mBackLeftView = getChildAt(0);
mBackRightView = (ViewGroup) getChildAt(1);
mFrontView = getChildAt(2);
mBackRightCall = mBackRightView.getChildAt(0);
mBackRightDelete = mBackRightView.getChildAt(1);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
mLeftRange = mBackLeftView.getMeasuredWidth();
mRightRange = mBackRightView.getMeasuredWidth();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
//
layoutContent(NORMAL);
}
public void layoutContent(int status) {
Rect frontRect = computeFrontRect(status);// ,
mFrontView.layout(frontRect.left, frontRect.top, frontRect.right, frontRect.bottom);
Rect rightBackRect = computeRightBackRect(frontRect);// ,
mBackRightView.layout(rightBackRect.left, rightBackRect.top, rightBackRect.right, rightBackRect.bottom);
}
public Rect computeFrontRect(int status) {
int left = 0;
int top = 0;
if (status == LEFT_OPEN) {
left = mLeftRange;
} else if (status == RIGHT_OPEN) {
left = -mRightRange;
}
return new Rect(left, top, left + mWidth, top + mHeight);
}
public Rect computeRightBackRect(Rect frontRect) {
int left = 0;
int top = 0;
if (frontRect != null) {
left = frontRect.right;
}
return new Rect(left, top, left + mRightRange, top + mHeight);
}
}
현재 컨트롤 결합 정도 가 너무 높 아서 나중에 천천히 최적화 하여 라 이브 러 리 로 작성 합 니 다.이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.