仿鲜果网切换activity(二)

http://www.trinea.cn/android/touch-event-delivery-mechanism/

http://www.cnblogs.com/sunzn/archive/2013/05/10/3064129.html

http://blog.csdn.net/hqdoremi/article/details/9979739

http://www.itfriend.cn/user/crelaber/article/details/101022

背景变黑
https://github.com/Issacw0ng/SwipeBackLayout/issues/4#issuecomment-24438037
全屏感应
https://github.com/Issacw0ng/SwipeBackLayout/issues/8

https://github.com/Issacw0ng/SwipeBackLayout

https://github.com/stormzhang/9GAG

1. 第二种方式是基于开源控件 SwipeBackLayout

2. 使用方式很简单,只需要当前页面的activity继承 SwipeBackActivity

SwipeBackActivity--》 SwipeBackActivityHelper--》 SwipeBackLayout--》 ViewDragHelper

3. 使用过程中的问题1:右滑的时候,下面出现的背景不是前一个activity,而是黑色背景,解决方式是activity或者application的theme设置背景色透明
< item  name =  "android:windowIsTranslucent"  > true  </ item >

4. 使用过程中的问题2:只有点击屏幕最左侧才有响应,屏幕其他位置无响应。解决方式是viewDragHelper中的一个方法
private int getEdgeTouched(int x, int y) {
            int result = 0;

            // if (x < mParentView.getLeft() + mEdgeSize)
            if ( canDrag)
                result = EDGE_LEFT;
            // if (y < mParentView.getTop() + mEdgeSize)
            // result = EDGE_TOP;
            // if (x > mParentView.getRight() - mEdgeSize)
            // result = EDGE_RIGHT;
            // if (y > mParentView.getBottom() - mEdgeSize)
            // result = EDGE_BOTTOM;

            return result;
     }

只需设置result永远为left即可。

5. 使用过程中的问题3:第一个activity不需要左滑关闭。这个问题swipeBack已经有解决, SwipeBackActivity实现的一个接口 SwipeBackActivityBase中有如下方法:
public abstract SwipeBackLayout getSwipeBackLayout();
设置是否返回效果
public abstract void setSwipeBackEnable( boolean enable);
执行返回效果
public abstract void scrollToFinish Activity();

6. 原理是
(1) activity的window设置背景色为透明,decorview设置背景为null。
mActivity .getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT ));
mActivity.getWindow().getDecorView(). setBackgroundDrawable(null);
mSwipeBackLayout = (SwipeBackLayout) LayoutInflater.from(mActivity).inflate(
                me.imid.swipebacklayout.lib.R.layout.swipeback_layout , null);

在oncreate执行完后,把swipebacklayout绑定到activity上
@Override
protected void onPostCreate (Bundle savedInstanceState) {
     super. onPostCreate(savedInstanceState);
     mHelper. onPostCreate();
}

public void onPostCreate() {
        mSwipeBackLayout.attachToActivity( mActivity);
        Utils.convertActivityFromTranslucent(mActivity );
}

所谓绑定到activity,只是把layout加到decorview上
public void attachToActivity (Activity activity) {
        mActivity = activity;
        TypedArray a = activity.getTheme().obtainStyledAttributes(new int[] {
            android.R.attr. windowBackground
        });
        int background = a.getResourceId(0, 0);
        a.recycle();

        ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
        ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
        decorChild.setBackgroundResource(background);
        decor.removeView(decorChild);
        addView(decorChild);
        setContentView(decorChild);
        decor.addView( this);
    }





你可能感兴趣的:(仿鲜果网切换activity(二))