riku
2025-10-17 fbae5f3ea74727ccadc48314a864a1ea0099a945
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
package cn.flightfeather.thirdappmodule.module.inspection
 
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.View
import android.widget.ImageView
import cn.flightfeather.thirdappmodule.CommonApplication
import cn.flightfeather.thirdappmodule.R
import cn.flightfeather.thirdappmodule.model.bean.LedgerRecord
import cn.flightfeather.thirdappmodule.module.base.BaseFragment
import cn.flightfeather.thirdappmodule.module.common.PhotoViewerActivity2
import cn.flightfeather.thirdappmodule.view.recyclerview.*
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import kotlinx.android.synthetic.main.layout_recycler_view_refresh.*
import org.jetbrains.anko.support.v4.toast
import java.util.ArrayList
 
/**
 * @author riku
 * Date:2022/4/12
 *
 */
class PicEvidenceFragment : BaseFragment() {
 
    companion object {
        const val ARG_PROBLEM = "ledgerList"
 
        fun newInstance(ledgerRecords: ArrayList<LedgerRecord>): PicEvidenceFragment {
            return PicEvidenceFragment().apply {
                arguments = Bundle().apply {
                    putParcelableArrayList(ARG_PROBLEM, ledgerRecords)
                }
            }
        }
    }
 
    lateinit var viewModel: PicEvidenceViewModel
 
    private var ledgerRecords: List<LedgerRecord>? = null
    private lateinit var recyclerViewPanel: RecyclerViewPanel<LedgerRecord>
 
    override fun getLayoutId(): Int = R.layout.layout_recycler_view_refresh
 
    override fun onCreateView() {
        super.onCreateView()
        ledgerRecords = arguments?.getParcelableArrayList(ARG_PROBLEM)
        viewModel = ViewModelProviders.of(activity!!).get(PicEvidenceViewModel::class.java)
    }
 
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initUI()
        initObserver()
    }
 
    private fun initUI() {
        val dataModel = object : DataLoadModel<LedgerRecord>(context!!) {
            override fun loadDataByRefresh() {
                onSuccess(ledgerRecords)
            }
 
            override fun loadDataByLoadMore(page: Int) {
                onPage(2, 1)
                onSuccess(emptyList())
            }
        }
        recyclerViewPanel = object : RecyclerViewPanel<LedgerRecord>(dataModel, recycler_view, context, refresh_layout) {
            override fun getItemLayoutId(): Int? = R.layout.item_image_chose
 
            override fun addSection(dataList: MutableList<MySection<LedgerRecord>>) {
                super.addSection(dataList)
                //每个LedgerRecord代表一项台账,其中包含所有的图片
                // 将每项台账的名称作为分隔标题
                val result = mutableListOf<MySection<LedgerRecord>>()
                dataList.forEach {
                    // 分割标题
                    result.add(MySection(true, it.t.ledgerName, LedgerRecord()))
                    // 台账图片
                    it.t.path1?.split(";")?.forEach { url ->
                        result.add(MySection(LedgerRecord(
                                path1 = viewModel.application.ROOT_URL_RELEASE_IMAGE_2 + url,
                                // FIXME: 2022/4/13 此处借用该字段记录图片是否被选中,对应界面展示相应选中效果
                                upLoad = false
                        )))
                    }
                }
                dataList.clear()
                dataList.addAll(result)
            }
 
            override fun onBindSectionView(holder: BaseCustomViewHolder?, item: MySection<LedgerRecord>?) {
                super.onBindSectionView(holder, item)
                item?.let {
                    holder?.setText(R.id.tv_section_head, it.header)
                }
            }
 
            override fun onBindView(holder: BaseCustomViewHolder, item: MySection<LedgerRecord>?) {
                item?.let {
                    holder.addOnClickListener(R.id.img_photo)
                            .addOnClickListener(R.id.fl_chose)
                            //图片是否选中判断
                            .setImageResource(R.id.img_chose, if (it.t.upLoad == true) R.drawable.ic_check_green_a700_24dp else R.color.white)
                            .getView<ImageView>(R.id.img_photo).let { i ->
                                Glide.with(this@PicEvidenceFragment).load(it.t.path1).into(i)
                            }
                }
            }
 
            override fun onItemChildClick(adapter: BaseQuickAdapter<Any?, BaseViewHolder>, view: View, position: Int, dataList: List<LedgerRecord>) {
                super.onItemChildClick(adapter, view, position, dataList)
                when (view.id) {
                    // 放大图片
                    R.id.img_photo -> {
                        val paths = ArrayList<String>()
                        var pos = position
                        for (i in dataList.indices) {
                            val d = dataList[i]
                            if (d.path1 == null) {
                                if (position > i) pos--
                            } else {
                                paths.add(d.path1!!)
                            }
                        }
                        PhotoViewerActivity2.goto(activity, paths, pos)
                    }
                    // 选择图片
                    R.id.fl_chose -> {
                        val d = dataList[position]
                        viewModel.chosePic(d.path1 ?: "",{
                            d.upLoad = it
                            adapter.notifyItemChanged(position)
                        },{ toast(it) })
                    }
                }
            }
 
 
            override fun getMyLayoutManager(): RecyclerView.LayoutManager = StaggeredGridLayoutManager(3, LinearLayoutManager.VERTICAL)
//                    GridLayoutManager(activity, 3, LinearLayoutManager.VERTICAL, false).apply {
//                        spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
//                            override fun getSpanSize(p0: Int): Int {
//                                return if (adapter?.getItemViewType(p0) == BaseRecyclerAdapter.SECTION_HEADER_VIEW) {
//                                    spanCount
//                                } else {
//                                    1
//                                }
//                            }
//                        }
//                    }
 
            override fun needSwipeRefresh(): Boolean  = false
        }.also {
            it.init()
            it.startRefresh()
        }
    }
 
    private fun initObserver() {
        // 监听清空按钮事件
        viewModel.selectedImgUrls.observe(this, Observer {
            if (it?.size == 0) {
                recyclerViewPanel.adapter?.apply {
                    data.forEach {s ->
                        s.t.upLoad = false
                    }
                    notifyDataSetChanged()
                }
            }
        })
    }
}