1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package cn.flightfeather.thirdappmodule.dataanalysis
 
import android.os.Bundle
import android.support.design.widget.NavigationView
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.widget.DrawerLayout
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import cn.flightfeather.thirdappmodule.CommonApplication
import cn.flightfeather.thirdappmodule.R
import cn.flightfeather.thirdappmodule.httpservice.TaskService
import java.util.*
 
/**
 * 数据分析模块
 */
class AnysisFragment : Fragment(), View.OnClickListener {
    private var application: CommonApplication? = null
    private var taskService: TaskService? = null
    private var visible = true
    private var drawerLayout: DrawerLayout? = null
    private var nvAnalysis: NavigationView? = null
    private val fragmentList: MutableList<Fragment> = ArrayList()
    private var fManager: FragmentManager? = null
    private var fragmentCurrent: Fragment? = null
    private var ivMenu: ImageView? = null
 
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_anysis, container, false)
    }
 
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        application = activity!!.application as CommonApplication
        taskService = application!!.retrofit.create(TaskService::class.java)
        fManager = activity!!.supportFragmentManager
        initActionbar(view)
        initNavigationView(view)
        initFragment()
    }
 
    private fun initNavigationView(view: View) {
        drawerLayout = view.findViewById<View>(R.id.DL_analysis) as DrawerLayout
        nvAnalysis = view.findViewById<View>(R.id.NV_analysis) as NavigationView
 
//        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, R.string.action_settings, R.string.action_settings);
//        drawerLayout.setDrawerListener(toggle);
//        toggle.syncState();
 
//        NV_analysis.setItemIconTintList(null);//设置图标颜色为本身颜色
        nvAnalysis!!.setNavigationItemSelectedListener { item ->
            val order = item.order
            try {
                switchSelectedFragment(fragmentList[order])
                ivMenu!!.refreshDrawableState()
            } catch (e: Exception) {
                e.printStackTrace()
            }
            drawerLayout!!.closeDrawer(Gravity.END)
            true
        }
    }
 
    private fun initFragment() {
        fragmentList.add(AnysisProgressFragment())
        fragmentList.add(AnalysisProblemFragment())
        //        fragmentList.add(new AnysisRankFragment());
        fManager!!.beginTransaction().add(R.id.FL_analysis, fragmentList[0]).commit()
        fragmentCurrent = fragmentList[0]
    }
 
    private fun switchSelectedFragment(fragment: Fragment) {
        if (fragment.isAdded) {
            fManager!!.beginTransaction().hide(fragmentCurrent!!).show(fragment).commit()
        } else {
            fManager!!.beginTransaction().add(R.id.FL_analysis, fragment).hide(fragmentCurrent!!).show(fragment).commit()
        }
        fragmentCurrent = fragment
    }
 
    private fun initActionbar(view: View) {
//        view.findViewById(R.id.img_left).setVisibility(View.GONE);
//        view.findViewById(R.id.spinner_topclass_task).setVisibility(View.GONE);
//        view.findViewById(R.id.img_right).setOnClickListener(this);
//        ((TextView) view.findViewById(R.id.actionbar_title)).setTitle("数据分析");
        ivMenu = view.findViewById<View>(R.id.IV_menu) as ImageView
        ivMenu!!.setOnClickListener(this)
    }
 
    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        //        taskProgressAdapter.notifyDataSetChanged();
    }
 
    override fun onClick(v: View) {
        when (v.id) {
            R.id.IV_menu -> {
                if (visible) drawerLayout!!.openDrawer(Gravity.END) else drawerLayout!!.closeDrawer(Gravity.END)
                visible = !visible
            }
        }
    }
}