美文网首页kotlin频道Kotlin编程Kotlin专题
kotlin-andorid dashboard仪表盘页面

kotlin-andorid dashboard仪表盘页面

作者: 奈文摩尔定律 | 来源:发表于2017-05-31 22:14 被阅读58次
    WechatIMG1.jpeg WechatIMG2.png WechatIMG3.png

    先上代码了

    // 枚举图表种类
    enum class ChartItemtag(var index:Int){
        GoalCompletion(0),
        SalesFunnels(1),
        RecentBusiness(2)
    }
    class DashBoardFragment : StructFragment() {
        companion object{
            val pageIds = PageId(10001,10002,10003)
        }
        data class PageId(var containerId:Int,var iconId:Int,var labelId:Int)
        data class ItemChart(var title:String,var containerTag:Int,var iconcode:String,var iconcolor:String,var label:String)
    
        val datasource:List<ItemChart>? by lazy {
            listOf<ItemChart>(
                    ItemChart("销售情况",ChartItemtag.GoalCompletion.index,"\ue60d","#a1e3a1","销售目标完成情况"),
                    ItemChart("销售漏斗",ChartItemtag.SalesFunnels.index,"\ue619","#f08980","销售漏斗"),
                    ItemChart("近期商机",ChartItemtag.RecentBusiness.index,"\ue612","#a2b4fe","近期重点商机")
            )
        }
    
        var arrowv:View?=null
    
        val selectedList = listOf<String>(
                "全部仪表盘","绩效仪表盘","行为仪表盘","客户分析"
        )
        val selectedview:SelectedListView? by lazy {
            val view = SelectedListView(context)
            view.layoutParams = LinearLayout.LayoutParams(matchParent, matchParent)
            view.list?.adapter = SelectedAdapter(selectedList)
            view.together = arrowv
            view
        }
    
    
        val alldashboard:RecyclerView? by lazy {
            val recycler = RecyclerView(context)
            val lpm = LinearLayoutManager(context)
                lpm.orientation = LinearLayoutManager.VERTICAL
            recycler.layoutManager = lpm
            recycler.layoutParams = ViewGroup.LayoutParams(matchParent, matchParent)
            recycler
        }
    
        override fun initProperties() {
            super.initProperties()
            title = "全部仪表盘"
        }
    
        override fun initView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?) {
            super.initView(inflater, container, savedInstanceState)
            val container_ = LinearLayout(context)
            container_.orientation = LinearLayout.VERTICAL
            fragment_view = container_
        }
    
        override fun initUIChildView() {
            super.initUIChildView()
    
            val toolbarSearchId = 20003
    
            toolbar = DeToolbar(context)
            toolbar.title_view?.removeView(toolbar.title_tv)
            toolbar.title_view?.addView(
                    context.linearLayout{
                        orientation = LinearLayout.HORIZONTAL
                        gravity = Gravity.CENTER
                        textView {
                            textSize = 20f
                            textColor = Color.WHITE
                            text = title
                        }.lparams { rightMargin = dip(3) }
                        textView {
                            id = toolbarSearchId
                            typeface = App.instance?.iconfont
                            textSize = 20f
                            textColor = Color.WHITE
                            text = "\ue60c"
                        }
                    }
            )
            arrowv = toolbar.title_view?.find(toolbarSearchId)
            (fragment_view as LinearLayout).addView(toolbar)
    
    
    
            val framelayout = FrameLayout(context)
                framelayout.layoutParams = FrameLayout.LayoutParams(matchParent, matchParent)
                framelayout.addView(alldashboard)
                framelayout.addView(selectedview)
            (fragment_view as LinearLayout).addView(framelayout)
    
            alldashboard?.adapter = DashMenuAdapter(context,datasource!!);
        }
    
        override fun settingsDelegate() {
            toolbar.title_view?.onClick {v->
                selectedview?.toggle()
            }
        }
    
    
        inner class SelectedAdapter(var datasource_: List<String>?) : RecyclerView.Adapter<SelectedAdapter.ViewHolder>() {
    
            val tvId = 100001
    
            override fun getItemCount(): Int  = datasource_!!.size
    
            override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
                val obj = datasource_!![position]
                with(holder!!){
                    tv?.text = obj
                }
            }
    
            override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
                val layout  = context.linearLayout {
                                gravity = Gravity.CENTER
                                background = resources.getDrawable(R.drawable.itempressed_draw)
                                textView {
                                    padding = dip(15)
                                    id = tvId
                                    textSize = 16f
                                    textColor = Color.BLACK
                                    gravity = Gravity.CENTER
                                }.lparams{width = matchParent}
                            }
                layout.layoutParams = LinearLayout.LayoutParams(matchParent, wrapContent)
                 parent?.addView(layout)
                return ViewHolder(layout)
            }
    
            inner class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
                var tv: TextView? = itemView?.find(tvId)
            }
        }
    }
    

    ChartItemtag是图表的枚举种类
    DashBoardFragment是列表页,StructFragment是自己封装的一个关于Fragment的生命周期的简单组件,
    图表的具体展示是采用android的webview组合echart图表(为了配合ios的图表,这样省时省力)

    DashWebViewFragment 是webview展示图表的Fragment

    open abstract class DashWebViewFragment :StructFragment() {
        data class PageId(
                var toolbar_leftId:Int,var toobar_titleId:Int,
                var toolbar_rightId:Int,var refreshId:Int,var webviewId:Int)
        val pageids = PageId(R.id.left_tv, R.id.title_tv, R.id.right_tv,10001,10002);
    
        val progressDialog:ProgressDialog? by lazy {
            val progress = ProgressDialog(context)
                progress.setMessage("页面加载中,请稍后")
                progress.setCancelable(true)
                progress
        }
    
        val chart: WebView? by lazy {
            val webView = fragment_view.find<WebView>(pageids.webviewId)
            val settings = webView.settings
            //设置webview
            settings.setDefaultTextEncodingName("utf-8")
            settings.setJavaScriptEnabled(true)
    
            settings.setDisplayZoomControls(false)
            settings.setSupportZoom(true)
            settings.setBuiltInZoomControls(true)
    
            settings.setUseWideViewPort(true)
    
            settings.setLoadWithOverviewMode(true)
    
            settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN)
    
            settings.setBlockNetworkImage(false)
            settings.setLoadsImagesAutomatically(true)
    
            webView.setWebViewClient(object :WebViewClient(){
                override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                    super.onPageStarted(view, url, favicon)
                    progressDialog?.show()
                }
            })
    
            webView.setWebChromeClient(object : WebChromeClient() {
                override fun onProgressChanged(view: WebView, newProgress: Int) {
                    super.onProgressChanged(view, newProgress)
                    if (newProgress >= 100) {
                        progressDialog?.dismiss()
                        calljsfunction()
                    }
                }
    
            })
            webView.addJavascriptInterface(object : Any() {
                    @JavascriptInterface
                    fun alert(message:String) {
                        context.toast(message)
                    }
            }, "baseObj")
    
            webView
        }
    
        val refreshview:SwipeRefreshLayout? by lazy {
            val refresh = fragment_view.find<SwipeRefreshLayout>(pageids.refreshId)
            refresh
        }
    
        override fun initView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?) {
            super.initView(inflater, container, savedInstanceState)
            fragment_view = context.verticalLayout {
                weightSum = 5f
                backgroundColor = resources.getColor(R.color.defaultgray)
                include<View>(R.layout.toolbar)
                swipeRefreshLayout {
                    id = pageids.refreshId
                    webView {
                        id = pageids.webviewId
                    }.lparams { width = matchParent;height = matchParent; }
                }.lparams { width = matchParent;height = matchParent;}
    
            }
            fragment_view.layoutParams = ViewGroup.LayoutParams(matchParent, matchParent)
        }
    
        override fun initUIChildView() {
            super.initUIChildView()
            with(fragment_view) {
                find<TextView>(R.id.title_tv)?.text = title
                find<TextView>(R.id.left_tv)?.onClick {
                    activity.finish()
                }
            }
        }
    
        override fun settingsDelegate() {
            super.settingsDelegate()
            refreshview?.setOnRefreshListener{
                chart?.reload()
                refreshview?.isRefreshing = false
            }
        }
    
        override fun onResume() {
            chart?.onResume()
            chart?.reload()
            super.onResume()
    
        }
    
        override fun onPause() {
            super.onPause()
            chart?.onPause()
        }
    
        override fun onDestroy() {
            if (chart != null) {
                chart?.destroy();
            }
            super.onDestroy()
        }
    
        abstract fun calljsfunction()
    }
    

    其他的比较容易理解,就不多说了

    相关文章

      网友评论

      本文标题:kotlin-andorid dashboard仪表盘页面

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