Android自定义控件Android开发技术分享Android开发经验谈

仿IOS右滑finish activity,不依赖布局实现

2017-12-10  本文已影响145人  麦兜叮叮当

右滑 finish 活动在IOS APP中是很常见的,可惜Android并没有提供给我们这样的实现。

不过还是有方法的,比如通过自定义ViewGroup拦截触摸事件,但是这并不是一种好的方案,因为耦合度比较高。ok,既然这样,我们来试一试下面这种方案。

是不是很简单,下面提供源码。

public class SixActivity extends AppCompatActivity {

    @NoteInterface(value = R.id.button)
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivitySixBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_six);

        NoteParse.Companion.init(this);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View aView) {
                startActivity(new Intent(SixActivity.this, SevenActivity.class));
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });
    }
}
class SevenActivity : AppCompatActivity() {

    private var slop: Int = 0
    private var x: Float = 0.toFloat()
    private var y: Float = 0.toFloat()

    //拦截的范围
    private var scope: Int = 0
    private var halfScope: Int = 0

    private lateinit var mConstraintLayout : ConstraintLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_seven)

        val dm = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(dm)
        val width = dm.widthPixels

        scope = width / 6
        halfScope = width / 2
        mConstraintLayout = findViewById(R.id.constraint_layout)
//        触发移动事件的最小距离
        slop = ViewConfiguration.get(this).scaledTouchSlop
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                x = event.x
                y = event.y
            }
            MotionEvent.ACTION_MOVE ->
                if (event.x - x > 0
                        && x.toInt() <= scope
                        && Math.abs(event.x - x) > Math.abs(event.y - y)
                        && Math.abs(event.x - x) > slop) {
                    (mConstraintLayout.parent as ViewGroup).scrollTo(-(event.x.toInt() - x.toInt()), 0)
                    return true
                }
            MotionEvent.ACTION_UP -> {
                if (event.x < halfScope) {
                    // 如果没有拉取过半,则回退
                    (mConstraintLayout.parent as ViewGroup).scrollTo(0, 0)
                } else {
                    if (event.x - x > 0
                            && x.toInt() <= scope
                            && Math.abs(event.x - x) > Math.abs(event.y - y)
                            && Math.abs(event.x - x) > slop) {
                        finish()
                    }
                }
            }
        }
        return super.onTouchEvent(event)
    }

}

最后需要修改一下style,让我们的window背景为透明。

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

        <item name="custom_attr_app_bg">#ffffff</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="button_bg">#ff0000</item>
//右滑需要加上下面两个item属性
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>

结束,主Activity牵扯到@NoteInterface,是我自定义的注解,通过它来实例化我们的view,请忽略。布局就不放了,很简单。

源码已放github:https://github.com/519401502/JNI

笔者能力有限,不足之处欢迎指出。

上一篇下一篇

猜你喜欢

热点阅读