声明:这博文涉及的代码块及lib是来自JieGuo这位大神的,本人只是做了很小的修改方便大家理解。感谢感谢!!!
下面是最终的效果图:
布局文件:
<?xml version="1.0" encoding="utf-8"?> <yellow5a5.clearscreenhelper.View.RelativeRootView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/sample_clear_root_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@mipmap/bg" tools:context="yellow5a5.sample.SampleFirActivity"> <TextView android:id="@+id/sample_person_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="15dp" android:text="文字A" android:textSize="15sp" /> <Button android:id="@+id/sample_left_bottom_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_margin="10dp" android:text=" 按钮A " /> <Button android:id="@+id/sample_center_btn" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="按钮C" /> <Button android:id="@+id/sample_right_bottom_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_margin="10dp" android:text=" 按钮B " /> <TextView android:id="@+id/sample_right_top_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_margin="15dp" android:text="文字B" android:textStyle="bold" /> </yellow5a5.clearscreenhelper.View.RelativeRootView> 可以看到主要是自定义的relativeRootView,当然还有其他继承不同的自定义view(frameRootView,LinearRootView,RelativeRootView,ScreenSideView)。
其中最主要的就是ClearScreenHelper这个类。这是封装好了的。它里面就是来管理内部的view。引用大神的话:
对原有的逻辑代码及界面文件改动极小 通过对其内部的View进行绑定,动态设置需要移除的控件 逻辑维护起来十分方便,几行代码完美解决问题
在activity中使用:
mClearRootLayout = (RelativeRootView) findViewById(R.id.sample_clear_root_layout); mClearScreenHelper = new ClearScreenHelper(this, mClearRootLayout); mClearScreenHelper.bind(mLeftBottomBtn, mRightBottomBtn, mRightTopTextV, mInfoTextV,mCenterBtn); mClearScreenHelper.setIClearEvent(new IClearEvent() { @Override public void onClearEnd() { Toast.makeText(SampleFirActivity.this, "滑出屏幕了...", Toast.LENGTH_SHORT).show(); } @Override public void onRecovery() { Toast.makeText(SampleFirActivity.this, "重回屏幕了...", Toast.LENGTH_SHORT).show(); } }); 其中clearScreenHelper的2个参数分别是当前activity实例和xml最外层的自定义view布局ID
clearScreenHelper.bind()这个方法就是你滑动清屏需要哪些控件一起消失划出屏幕外。
setIClearEvent这是一个回调方法,当向右滑动出屏幕和向左滑动回来都会触发。
到此就可以了,上面这种是直接将lib以工程方式引入项目的。当然你也可以以添加依赖的方式进行。如下:
allprojects { repositories { ... maven { url "https://jitpack.io" } } } dependencies { compile 'com.github.Yellow5A5:ClearScreenHelper:1.0.0' }注意,这里依赖源码中它是buildToolsversion:25的
下面给出源码demo(里面包含引入的lib工程文件):点击打开链接
这个是我封装好的jar包,大家可以直接添加到libs文件夹中使用。下载地址:点击打开链接
用法:
com.henry.clearscreen.view.xxx 比如: <com.henry.clearscreen.view.RelativeRootView android:layout_width="wrap_content" android:layout_height="wrap_content" />说明一下:另外我发现textview添加了点击事件是没用的。这可能是需要完善的地方!!!