参考各位大牛的博客后所总结的个人笔记。

 

实现类似微信二级菜单返回到一级菜单。

1、新建一个返回的类,继承自页面所继承的AppCompatActivity,也可以继承其他,根据需求修改。可自行修改按下的位置和滑动的距离。

public class LeftToRightBack extends AppCompatActivity {  
    View decorView;  
    float downX, downY;  
    float screenWidth, screenHeight;  
   

    boolean allow = false;  
    public void InitLeftToRightBack()  
    {  
        // 获得decorView  
        decorView = getWindow().getDecorView();  
   

        // 获得手机屏幕的宽度和高度,单位像素  
        DisplayMetrics metrics = new DisplayMetrics();  
        getWindowManager().getDefaultDisplay().getMetrics(metrics);  
        screenWidth = metrics.widthPixels;  
        screenHeight = metrics.heightPixels;  
        allow = true;  
    }  
    @Override  
    public boolean onTouchEvent(MotionEvent event) {  
        TouchEvent(event);  
        return super.onTouchEvent(event);  
    }  
    boolean isSide = false;  
    public void TouchEvent(MotionEvent event) {  
        if(allow){  
            if(event.getAction() == MotionEvent.ACTION_DOWN){// 当按下时  
                // 获得按下时的X坐标  
                downX = event.getX();  
                isSide = downX < screenWidth / 10;  
            }else if(event.getAction() == MotionEvent.ACTION_MOVE && isSide){// 当手指滑动时  
                // 获得滑过的距离  
                float moveDistanceX = event.getX() – downX;  
                if(moveDistanceX > 0){// 如果是向右滑动  
                    decorView.setX(moveDistanceX); // 设置界面的X到滑动到的位置  
                }  
   

            }else if(event.getAction() == MotionEvent.ACTION_UP && isSide){// 当抬起手指时  
                // 获得滑过的距离  
                float moveDistanceX = event.getX() – downX;  
                if(moveDistanceX > screenWidth / 3){  
                    // 如果滑动的距离超过了手机屏幕的1/3, 结束当前Activity  
                    finish();  
                }else{ // 如果滑动距离没有超过一半  
                    // 恢复初始状态  
                    decorView.setX(0);  
                }  
            }  
        }  
   

    }  
}  

2、新建二级页面,继承自LeftToRightBack,主要是为了继承TouchEvent事件,然后再onCreate方法中添加初始化InitLeftToRightBack();

public class ServerActionActivity extends LeftToRightBack {  
   

    //定义数据  
    private List<SeverAction> mData;  
    //定义ListView对象  
    private ListView mListViewArray;  
    private Context context;  
    String innerCachePath;  
   

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
   

        setContentView(R.layout.activity_serveraction);  
   

        context = getApplicationContext();  
        innerCachePath = this.getCacheDir().getAbsolutePath();  
   

        InitLeftToRightBack();  
   

}    }  

3、此时已经可以实现右滑返回,但是在滑动过程中会出现乱的页面。解决方法如下,在styles中加入透明的主题,这样在返回的时候不会出现乱的页面,而是出现上级页面。并且在AndroidManifest中引用。

Styles

<style name=“MyTheme” parent=“Base.Theme.AppCompat.Light.DarkActionBar”>  
    <item name=“colorPrimary”>@color/colorPrimary</item>  
    <item name=“colorPrimaryDark”>@color/colorPrimaryDark</item>  
    <item name=“colorAccent”>@color/colorAccent</item>  
    <item name=“android:windowBackground”>@android:color/transparent</item>  
    <item name=“android:colorBackgroundCacheHint”>@null</item>  
    <item name=“android:windowIsTranslucent”>true</item>  
</style>  

Manifest

<activity  
    android:name=“.ServerActionActivity”  
    android:theme=“@style/MyTheme” />  

4、新的问题又来了,摆放控件后可以看到上级页面,这样不行,所以要在二级页面上面添加上背景颜色

android:background=“@android:color/white”  

 

 

5、经过测试发现在有一个大的控件摆放在页面后右滑就没有用,Touch事件被控件消费掉了,所以要利用事件分发机制,保证页面可以正常返回。

获取控件后直接添加分发,如果还有其他控件消费了该事件,都要加上分发机制

mListViewArray = findViewById(R.id.sever_action_listview);  
mListViewArray.setOnTouchListener(new View.OnTouchListener() {  
    @Override  
    public boolean onTouch(View view, MotionEvent motionEvent) {  
        TouchEvent(motionEvent);  
        return false;  
    }  
});  

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐