美文网首页
Android_ViewController

Android_ViewController

作者: 大风车__ | 来源:发表于2020-07-15 14:47 被阅读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()
        }
    }
}

相关文章

网友评论

      本文标题:Android_ViewController

      本文链接:https://www.haomeiwen.com/subject/fkndhktx.html