Pu Zhibing
2025-12-11 ab0c19e49fabdb8cc6eea454ea455bce2efbb718
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
package com.igo.user.ui.mine
 
import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import cn.sinata.xldutils.view.SwipeRefreshRecyclerLayout
import com.igo.user.R
import com.igo.user.network.HttpManager
import com.igo.user.network.entity.Goods
import com.igo.user.network.request
import com.igo.user.ui.TransparentStatusBarActivity
import com.igo.user.ui.mine.adapter.ScoreGoodsAdapter
import org.jetbrains.anko.backgroundColorResource
import org.jetbrains.anko.dip
import org.jetbrains.anko.find
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult
 
class ScoreActivity : TransparentStatusBarActivity() ,SwipeRefreshRecyclerLayout.OnRefreshListener{
    override fun setContentView() = R.layout.base_recyclerview_layout
 
    private val mSwipeRefreshLayout by lazy {
        find<SwipeRefreshRecyclerLayout>(R.id.swipeRefreshLayout)
    }
    private val datas = arrayListOf<Goods>()
    private val adapter = ScoreGoodsAdapter(datas)
    private val header by lazy {
        layoutInflater.inflate(R.layout.header_score,mSwipeRefreshLayout,false)
    }
    private val tv_score by lazy { header.find<TextView>(R.id.tv_score) }
 
    private var score = ""
    private var page = 1
 
    override fun initClick() {
        adapter.setOnItemClickListener { _, position ->
            startActivityForResult<GoodsDetailActivity>(1,"id" to datas[position].id,"score" to score)
        }
    }
 
    override fun initView() {
        title = getString(R.string.我的积分)
        titleBar.addRightButton(getString(R.string.兑换记录换行),onClickListener = View.OnClickListener {
            startActivity<RecordActivity>("type" to RecordActivity.TYPE_SCORE)
        })
        mSwipeRefreshLayout.backgroundColorResource = R.color.white
        mSwipeRefreshLayout.setPadding(dip(14),0,dip(14),0)
        mSwipeRefreshLayout.setLayoutManager(GridLayoutManager(this,2))
        mSwipeRefreshLayout.setAdapter(adapter)
        mSwipeRefreshLayout.setOnRefreshListener(this)
        adapter.setHeaderView(header)
        score = intent.getStringExtra("score")?:""
        tv_score.text = score
        getData()
    }
 
    private fun getData(){
        HttpManager.queryGoods(page).request(this,true,{_,data->
            mSwipeRefreshLayout.isRefreshing = false
            data?.apply {
                if (page == 1)
                    datas.clear()
                datas.addAll(this)
                if (datas.isEmpty())
                    mSwipeRefreshLayout.setLoadMoreText(getString(R.string.暂无数据))
                else if (data.isEmpty())
                    mSwipeRefreshLayout.setLoadMoreText(getString(R.string.没有更多))
                adapter.notifyDataSetChanged()
            }
        }){_,_->
            mSwipeRefreshLayout.isRefreshing = false
        }
    }
 
    override fun onRefresh() {
        page = 1
        getData()
    }
 
    override fun onLoadMore() {
        page++
        getData()
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK&&data!=null){
            if (requestCode == 1){
                score = (score.toInt() - data.getIntExtra("score",0)).toString()
                tv_score.text = score
            }
        }
    }
}