滑动面板(Java到Kotlin)

时间:2018-12-05 09:54:19

标签: java android kotlin

如何在Kotlin中显示SlidingUpPanel的面板状态?

我要按照本教程使用https://github.com/umano/AndroidSlidingUpPanel库:

http://www.devexchanges.info/2015/05/making-sliding-up-panel-like-google.html

,结果应如下所示:

https://www.youtube.com/watch?v=g464fp-2dQU

教程链接中的Java代码:

private SlidingUpPanelLayout.PanelSlideListener onSlideListener() {
    return new SlidingUpPanelLayout.PanelSlideListener() {
        @Override
        public void onPanelSlide(View view, float v) {
            textView.setText("panel is sliding");
        }

        @Override
        public void onPanelCollapsed(View view) {
            textView.setText("panel Collapse");
        }

        @Override
        public void onPanelExpanded(View view) {
            textView.setText("panel expand");
        }

        @Override
        public void onPanelAnchored(View view) {
            textView.setText("panel anchored");
        }

        @Override
        public void onPanelHidden(View view) {
            textView.setText("panel is Hidden");
        }
    };
}

从Java到Kotlin的转换代码(我从anko库而不是textView中放了烤面包):

private fun onSlideListener(): SlidingUpPanelLayout.PanelSlideListener {
    return object : SlidingUpPanelLayout.PanelSlideListener {
        override fun onPanelSlide(view: View, v: Float) {
            toast("panel is sliding")
        }

        fun onPanelCollapsed(view: View) {
            toast("panel Collapse")
        }

        fun onPanelExpanded(view: View) {
            toast("panel expand")
        }

        fun onPanelAnchored(view: View) {
            toast("panel anchored")
        }

        fun onPanelHidden(view: View) {
            toast("panel is Hidden")
        }
    }
}

在Java代码中,在onCreate方法中,我有:

slidingLayout.setPanelSlideListener(onSlideListener());

在Kotlin中似乎不起作用,因为我使用的方法是“点击”,“拖动”,“悬停”,“键”和“触摸”(监听器)。

我想其他好的方法可能是实施

SlidingPaneLayout.PanelSlideListener

在我的课堂上,但我仍然无法使其正常工作。

找到该面板是在Kotlin中显示还是隐藏的解决方案?

1 个答案:

答案 0 :(得分:0)

您需要加载“滑动布局”。

private lateinit var slidingLayout: SlidingUpPanelLayout
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.activity_main, container, false).also {
        slidingLayout = it.findViewById(R.id.sliding_Layout)
    }
}

而不是检查PanelState

if (slidingLayout.panelState == SlidingUpPanelLayout.PanelState.HIDDEN)

您还具有锚定,扩展,崩溃,拖曳的状态

如果您想在Kotlin中设置侦听器,则可以像这样完成。

slidingLayout.addPanelSlideListener(object : SlidingUpPanelLayout.PanelSlideListener {
        override fun onPanelSlide(panel: View?, slideOffset: Float) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onPanelStateChanged(panel: View?, previousState: SlidingUpPanelLayout.PanelState?, newState: SlidingUpPanelLayout.PanelState?) {
           TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }
    })