1:定義一個本身的父級容器,讓它繼承自一個佈局(LinearLayout、RelativeLayout均可以)android
public class SildingFinishLayout extends RelativeLayout implements View.OnTouchListener { /** * SildingFinishLayout佈局的父佈局 */
private ViewGroup mParentView; /** * 處理滑動邏輯的View */
private View touchView; /** * 滑動的最小距離 */
private int mTouchSlop; /** * 按下點的X座標 */
private int downX; /** * 按下點的Y座標 */
private int downY; /** * 臨時存儲X座標 */
private int tempX; /** * 滑動類 */
private Scroller mScroller; /** * SildingFinishLayout的寬度 */
private int viewWidth; /** * 記錄是否正在滑動 */
private boolean isSilding; private OnSildingFinishListener onSildingFinishListener; private boolean isFinish; public SildingFinishLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SildingFinishLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); mScroller = new Scroller(context); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (changed) { // 獲取SildingFinishLayout所在佈局的父佈局
mParentView = (ViewGroup) this.getParent(); viewWidth = this.getWidth(); } } /** * 設置OnSildingFinishListener, 在onSildingFinish()方法中finish Activity * * @param onSildingFinishListener */
public void setOnSildingFinishListener( OnSildingFinishListener onSildingFinishListener) { this.onSildingFinishListener = onSildingFinishListener; } /** * 設置Touch的View * * @param touchView */
public void setTouchView(View touchView) { this.touchView = touchView; touchView.setOnTouchListener(this); } public View getTouchView() { return touchView; } /** * 滾動出界面 */
private void scrollRight() { final int delta = (viewWidth + mParentView.getScrollX()); // 調用startScroll方法來設置一些滾動的參數,咱們在computeScroll()方法中調用scrollTo來滾動item
mScroller.startScroll(mParentView.getScrollX(), 0, -delta + 1, 0, Math.abs(delta)); postInvalidate(); } /** * 滾動到起始位置 */
private void scrollOrigin() { int delta = mParentView.getScrollX(); mScroller.startScroll(mParentView.getScrollX(), 0, -delta, 0, Math.abs(delta)); postInvalidate(); } /** * touch的View是不是AbsListView, 例如ListView, GridView等其子類 * * @return */
private boolean isTouchOnAbsListView() { return touchView instanceof AbsListView ? true : false; } /** * touch的view是不是ScrollView或者其子類 * * @return */
private boolean isTouchOnScrollView() { return touchView instanceof ScrollView ? true : false; } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = tempX = (int) event.getRawX(); downY = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE: int moveX = (int) event.getRawX(); int deltaX = tempX - moveX; tempX = moveX; if (Math.abs(moveX - downX) > mTouchSlop && Math.abs((int) event.getRawY() - downY) < mTouchSlop) { isSilding = true; // 若touchView是AbsListView, // 則當手指滑動,取消item的點擊事件,否則咱們滑動也伴隨着item點擊事件的發生
if (isTouchOnAbsListView()) { MotionEvent cancelEvent = MotionEvent.obtain(event); cancelEvent .setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); v.onTouchEvent(cancelEvent); } } if (moveX - downX >= 0 && isSilding) { mParentView.scrollBy(deltaX, 0); // 屏蔽在滑動過程當中ListView ScrollView等本身的滑動事件
if (isTouchOnScrollView() || isTouchOnAbsListView()) { return true; } } break; case MotionEvent.ACTION_UP: isSilding = false; if (mParentView.getScrollX() <= -viewWidth / 2) { isFinish = true; scrollRight(); } else { scrollOrigin(); isFinish = false; } break; } // 假如touch的view是AbsListView或者ScrollView 咱們處理完上面本身的邏輯以後 // 再交給AbsListView, ScrollView本身處理其本身的邏輯
if (isTouchOnScrollView() || isTouchOnAbsListView()) { return v.onTouchEvent(event); } // 其餘的狀況直接返回true
return true; } @Override public void computeScroll() { // 調用startScroll的時候scroller.computeScrollOffset()返回true,
if (mScroller.computeScrollOffset()) { mParentView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); if (mScroller.isFinished()) { if (onSildingFinishListener != null && isFinish) { onSildingFinishListener.onSildingFinish(); } } } } public interface OnSildingFinishListener { public void onSildingFinish(); } }
2.Acitity面試
在Activity的onCreate()方法裏面 @Override public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); SildingFinishLayout mSildingFinishLayout = (SildingFinishLayout) findViewById(R.id.timed_task_SildingFinishLayout); mSildingFinishLayout mSildingFinishLayout .setOnSildingFinishListener(new SildingFinishLayout.OnSildingFinishListener() { @Override public void onSildingFinish() { finish(); } }); mSildingFinishLayout .setTouchView(mSildingFinishLayout);//綁定底層的佈局就能夠了 :不起做用的話換個你activity 佈局裏面view就能夠啦
} 截止目前,這個功能已經實現了
解決問題:這個會出現左滑 背景白色問題
1.style.xml中中增長框架
<style name="AppTheme" parent="android:Theme.Light">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
2 acvitiy 的屬性中加上ide
android:theme="@style/AppTheme"
3 一些屬性的詳解佈局
<item name="android:windowFrame">@null</item> :Dialog的windowFrame框爲無 <item name="android:windowIsFloating">true</item>:是否浮如今activity之上 <item name="android:windowIsTranslucent">false</item>:是否半透明 <item name="android:windowNoTitle">true</item>:是否顯示title <item name="android:windowBackground">@drawable/dia_bg</item>:設置dialog的背景 <item name="android:backgroundDimEnabled">false</item>: 背景是否透明顯示
但願本文對你有所幫助~~若是對接口測試、自動化測試、面試經驗交流感興趣能夠加入咱們。642830685,免費領取最新軟件測試大廠面試資料和Python自動化、接口、框架搭建學習資料!技術大牛解惑答疑,同行一塊兒交流。post