Android_ViewController

2020-07-15  本文已影响0人  大风车__

仿iOS的UIViewController(支持手势返回)

GitHub: https://github.com/QiaokeZ/Android_ViewController
效果演示1
view.gif
效果演示2
view.gif
如何使用
class MainActivity : AppCompatActivity() {

    lateinit var navigationController: NavigationController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        navigationController = NavigationController(this, AViewController(this))
        navigationController.setLifecycleCallbacks(MyViewControllerLifecycleCallbacks())
        val view = navigationController.makeContainerView()
        setContentView(view)
    }

    override fun onBackPressed() {
        if (navigationController.children.size == 1) {
            super.onBackPressed()
        } else {
            navigationController.pop()
        }
    }
}

class MyViewControllerLifecycleCallbacks : ViewControllerLifecycleCallbacks {

    override fun viewDidAppear(viewController: ViewController) {
        Log.e(viewController.javaClass?.name, "- viewDidAppear")
    }

    override fun viewWillAppear(viewController: ViewController) {
        Log.e(viewController.javaClass?.name, "- viewWillAppear")
    }

    override fun loadView(viewController: ViewController) {
        Log.e(viewController.javaClass?.name, "- loadView")
    }

    override fun viewWillDisappear(viewController: ViewController) {
        Log.e(viewController.javaClass?.name, "- viewWillDisappear")
    }

    override fun viewDidLoad(viewController: ViewController) {
        Log.e(viewController.javaClass?.name, "- viewDidLoad")
    }

    override fun viewDidDisappear(viewController: ViewController) {
        Log.e(viewController.javaClass?.name, "- viewDidDisappear")
    }
}

class AViewController(context: Context) : ViewController(context) {

    override fun loadView(): View {
        return LayoutInflater.from(context).inflate(R.layout.view_a, null)
    }
    
    override fun viewDidLoad() {
        super.viewDidLoad()
        contentView.button.setOnClickListener {
             navigation?.pop()
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读