riku
2022-01-20 5a0fff8095cd5356f57c181b7e7b820e0f7efacf
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package cn.flightfeather.thirdappmodule.module.inspectioninfo
 
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.content.ContextCompat
import android.support.v4.view.ViewCompat
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.*
import android.widget.PopupWindow
import android.widget.TextView
import butterknife.ButterKnife
import butterknife.Unbinder
import cn.flightfeather.thirdappmodule.R
import cn.flightfeather.thirdappmodule.adapter.AllRecyclerViewAdapter
import cn.flightfeather.thirdappmodule.bean.vo.ProblemCategoryVo
import cn.flightfeather.thirdappmodule.bean.vo.ProblemlistVo
import cn.flightfeather.thirdappmodule.util.Constant
import cn.flightfeather.thirdappmodule.util.Domain
import kotlinx.android.synthetic.main.activity_problem_change_pollution_scene.*
import kotlinx.android.synthetic.main.layout_actionbar.*
import java.util.*
 
/**
 * 2019.1.25
 * @author riku
 * 客户界面
 * 整改情况界面,展示客户未整改和已整改的问题列表
 */
class ProblemChangeActivity : AppCompatActivity() {
    private var problemlistVos: ArrayList<ProblemlistVo>? = null
    private var problemCategories: ArrayList<ProblemCategoryVo>? = null
    private var newSelectedTypes: MutableList<Int>? = null
    private var curSelectedTypes: MutableList<Int>? = null
    private val TITLE = "整改"
    private val POPUP_TITLE = "问题类型"
    private var unbinder: Unbinder? = null
 
    //<editor-fold desc="lifecycle">
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置允许使用转场动画
        window.requestFeature(Window.FEATURE_CONTENT_TRANSITIONS)
        setContentView(R.layout.activity_problem_change_pollution_scene)
        unbinder = ButterKnife.bind(this)
        val intent = intent
        problemlistVos = intent.getSerializableExtra(ARG_PROBLEM) as ArrayList<ProblemlistVo>
        problemCategories = intent.getSerializableExtra(ARG_TOTAL_PROBLEM) as ArrayList<ProblemCategoryVo>
        newSelectedTypes = ArrayList()
        curSelectedTypes = ArrayList()
        initToolBar()
        initTab()
        initViewPager()
        initPopup()
        initClickListener()
    }
 
    override fun onDestroy() {
        super.onDestroy()
        if (unbinder != null) {
            unbinder!!.unbind()
        }
    }
 
    //</editor-fold>
 
    //<editor-fold desc="标题栏">
 
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private fun initToolBar() {
//        action_bar.setElevation(0);
//        action_bar.setBackgroundColor(Color.alpha(0));
        action_bar!!.findViewById<View>(R.id.spinner_topclass_task).visibility = View.GONE
        //        action_bar.findViewById(R.id.img_left).setVisibility(View.GONE);
        img_right!!.setImageResource(R.drawable.ic_filter_list_white_36dp)
        action_bar!!.findViewById<View>(R.id.ll_menu_text).visibility = View.GONE
        actionbar_title!!.text = TITLE
    }
 
    private fun initClickListener() {
        img_left.setOnClickListener { finish() }
        img_right.setOnClickListener {
            popShow()
            newSelectedTypes!!.clear()
            newSelectedTypes!!.addAll(curSelectedTypes!!)
        }
    }
 
    //</editor-fold>
 
    //<editor-fold desc="ViewPager初始化">
    private var tabIndicators = mutableListOf<String>()//tab标题
    private var tabFragments = mutableListOf<Fragment>()//内容
    private var contentAdapter: ContentPagerAdapter? = null//viewPager内容页面适配器
 
    private fun initTab() {
//        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout!!.tabGravity = Gravity.CENTER_HORIZONTAL
        tabLayout!!.setTabTextColors(ContextCompat.getColor(this, R.color.unselected), ContextCompat.getColor(this, R.color.main_text_color))
        tabLayout!!.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.main_text_color))
        ViewCompat.setElevation(tabLayout!!, 2f)
        tabLayout!!.setupWithViewPager(viewPager) //将tab和viewPager绑定
    }
 
    private val unChangedProblems = ArrayList<ProblemlistVo>()
    private val changedProblems = ArrayList<ProblemlistVo>()
    private fun initViewPager() {
        changedProblems.clear()
        unChangedProblems.clear()
        for (i in problemlistVos!!.indices) {
            val problemlistVoTemp = problemlistVos!![i]
            if (!problemlistVoTemp.ischanged) {
                unChangedProblems.add(problemlistVoTemp)
            } else if (problemlistVoTemp.extension3 != Domain.CHANGE_CHECK_PASS) {
                unChangedProblems.add(problemlistVoTemp)
            } else {
                changedProblems.add(problemlistVoTemp)
            }
        }
        tabIndicators.add("待整改问题")
        tabIndicators.add("已整改问题")
        tabFragments.add(ProblemChangeFragment.newInstance(unChangedProblems, Constant.UNCHANGED))
        tabFragments.add(ProblemChangeFragment.newInstance(changedProblems, Constant.CHANGED))
        contentAdapter = ContentPagerAdapter(supportFragmentManager)
        viewPager!!.adapter = contentAdapter
    }
 
    //刷新ViewPager
    fun refreshViewPager() {
        changedProblems.clear()
        unChangedProblems.clear()
        for (i in problemlistVos!!.indices) {
            val problemlistVoTemp = problemlistVos!![i]
            if (!problemlistVoTemp.ischanged) {
                unChangedProblems.add(problemlistVoTemp)
            } else if (problemlistVoTemp.extension3 != Domain.CHANGE_CHECK_PASS) {
                unChangedProblems.add(problemlistVoTemp)
            } else {
                changedProblems.add(problemlistVoTemp)
            }
        }
        val pf1 = tabFragments!![0] as ProblemChangeFragment
        val pf2 = tabFragments!![1] as ProblemChangeFragment
        pf1.notifyDataSetChanged()
        pf2.notifyDataSetChanged()
    }
 
    //整改完成后,刷新问题列表
    fun refreshProblemList(problemlistVo: ProblemlistVo?) {
        if (problemlistVo == null) return
        for (p in problemlistVos!!) {
            if (p.guid == problemlistVo.guid) {
                problemlistVos!!.remove(p)
                break
            }
        }
        problemlistVos!!.add(problemlistVo)
    }
 
    /**
     * viewPager适配器
     */
    internal inner class ContentPagerAdapter(fm: FragmentManager?) : FragmentPagerAdapter(fm) {
        override fun getItem(position: Int): Fragment {
            return tabFragments!![position]
        }
 
        override fun getCount(): Int {
            return tabIndicators!!.size
        }
 
        override fun getPageTitle(position: Int): CharSequence? {
            return tabIndicators!![position]
        }
    }
 
    //</editor-fold>
 
    //<editor-fold desc="根据用户选择显示对应类型的问题">
    private fun showSelectedProblemType(type: List<Int>?) {
        problemlistVos!!.clear()
        for (t in type!!) {
            problemlistVos!!.addAll(problemCategories!![t].problemlistVos)
        }
        if (type.isEmpty()) {
            for (p in problemCategories!!) {
                problemlistVos!!.addAll(p.problemlistVos)
            }
        }
        refreshViewPager()
    }
 
    //</editor-fold>
 
    //<editor-fold desc="popupWindow">
    private var popupWindow: FilterPopupWindow? = null
    private var popupIsShowing = false
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private fun initPopup() {
        popupWindow = FilterPopupWindow(this, problemCategories)
    }
 
    private fun popShow() {
        popupWindow!!.showAtLocation(action_bar, Gravity.BOTTOM, 0, 0)
        popupIsShowing = true
        setWindowAloha(0.6f)
    }
 
    private fun popDismiss() {
        popupWindow!!.dismiss()
        popupIsShowing = false
    }
 
    private fun setWindowAloha(f: Float) {
        val window = window
        val wl = window.attributes
        wl.alpha = f
        window.attributes = wl
    }
 
    private fun popupRefresh() {
        popupWindow!!.notifyDataSetChanged()
    }
 
    internal inner class FilterPopupWindow @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) constructor(context: Context?, private val datas: List<ProblemCategoryVo>?) : PopupWindow(context) {
        var rv_problem_type: RecyclerView? = null
        var adapter: AllRecyclerViewAdapter<ProblemCategoryVo>? = null
        private fun initView(view: View) {
            rv_problem_type = view.findViewById(R.id.rv_problem_type_2)
            adapter = object : AllRecyclerViewAdapter<ProblemCategoryVo>(
                datas,
                R.layout.item_problem_type,
                this@ProblemChangeActivity
            ) {
                override fun bindView(holder: MyViewHolder, obj: ProblemCategoryVo, isSelected: Boolean, position: Int) {
                    //填充问题类型名称
                    holder.setText(R.id.text_problem_type, obj.problemTypeName)
 
                    //根据问题数量设置点击事件
                    if (obj.totalCount == 0) {
                        holder.setAlpha(R.id.text_problem_type, 0.2f)
                        holder.setOnClickListener(R.id.text_problem_type, null)
                    } else {
                        holder.setOnClickListener(R.id.text_problem_type) { v ->
                            v.isSelected = !v.isSelected
                            if (v.isSelected) {
                                newSelectedTypes!!.add(position)
                            } else {
                                newSelectedTypes!!.remove(position)
                            }
                        }
                        holder.setAlpha(R.id.text_problem_type, 1f)
                    }
 
                    //根据用户的选择设置是否选择状态
                    if (curSelectedTypes!!.contains(position)) {
                        holder.setSelected(R.id.text_problem_type, true)
                    } else {
                        holder.setSelected(R.id.text_problem_type, false)
                    }
                }
            }
            val layoutManager = GridLayoutManager(this@ProblemChangeActivity, 3)
            layoutManager.orientation = LinearLayoutManager.VERTICAL
            rv_problem_type?.setLayoutManager(layoutManager)
            rv_problem_type?.setAdapter(adapter)
        }
 
        private fun setClickListener(v: View) {
            val text_complete = v.findViewById<TextView>(R.id.text_complete)
            val text_cancel = v.findViewById<TextView>(R.id.text_cancel)
            text_complete.setOnClickListener {
                popDismiss()
                curSelectedTypes!!.clear()
                curSelectedTypes!!.addAll(newSelectedTypes!!)
                showSelectedProblemType(curSelectedTypes)
                popupRefresh()
            }
            text_cancel.setOnClickListener { popDismiss() }
        }
 
        fun notifyDataSetChanged() {
            adapter!!.notifyDataSetChanged()
        }
 
        init {
            height = ViewGroup.LayoutParams.WRAP_CONTENT
            width = ViewGroup.LayoutParams.MATCH_PARENT
            elevation = 2f
            setBackgroundDrawable(null)
            isOutsideTouchable = true
            isFocusable = true
            animationStyle = R.style.popwin_anim_style
            setOnDismissListener { setWindowAloha(1f) }
            val contentView = LayoutInflater.from(this@ProblemChangeActivity).inflate(R.layout.popup_problem_selector, null, false)
            initView(contentView)
            setClickListener(contentView)
            setContentView(contentView)
        }
    } //</editor-fold>
 
    companion object {
        //<editor-fold desc="全局变量">
        const val ARG_PROBLEM = "problem"
        const val ARG_TOTAL_PROBLEM = "totalProblem"
    }
}