app/src/main/java/com/example/firstapp/ui/invitation/InvitationActivity.kt
@@ -19,6 +19,7 @@
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
@@ -31,6 +32,7 @@
import com.example.firstapp.App.Companion.context
import com.example.firstapp.R
import com.example.firstapp.adapter.InvitationAdapter
import com.example.firstapp.adapter.InvitationRecord2Adapter
import com.example.firstapp.adapter.InvitationRecordAdapter
import com.example.firstapp.database.request.SmsLoginRequest
import com.example.firstapp.database.service.RetrofitClient
@@ -49,7 +51,7 @@
    private lateinit var recyclerSuccessView: RecyclerView
    private lateinit var recyclerRecordView: RecyclerView
    private lateinit var adapter: InvitationAdapter
    private lateinit var recordadapter: InvitationRecordAdapter
//    private lateinit var recordadapter: InvitationRecordAdapter
    private var data = mutableListOf<InvitationRecord>()
    private var recorddata = mutableListOf<InvitationRecord>()
    private val handler = Handler(Looper.getMainLooper())
@@ -57,6 +59,11 @@
    private var currentScrollPosition = 0
    private var currentRecordScrollPosition = 0
    private var itemHeight = 0 // 动态存储item高度
    private lateinit var invitedRecordRecyclerView2: RecyclerView
    private lateinit var invitationAdapter: InvitationRecord2Adapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
@@ -68,13 +75,10 @@
        //初始化Adapter
        initSuccessAdapter()
        initRecorddapter()
//        initRecorddapter()
        //加载数据
        getInvitereward()
        //loadData()
        //loadRecordData()
        //启动轮播
        startAutoScroll()
@@ -87,7 +91,6 @@
        //邀请码
        val invitationCodeText = findViewById<TextView>(R.id.invitationCodeText)
        //invitationCodeText.text = "A1B2"
        invitationCodeText.text = formatInvitationCode(PreferencesManager.getInviteCode());
        findViewById<Button>(R.id.copyButton).setOnClickListener {
            val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
@@ -98,90 +101,44 @@
            clipboard.setPrimaryClip(clip)
            Toast.makeText(this, "已复制邀请码", Toast.LENGTH_SHORT).show()
        }
    }
    private fun initViews() {
        findViewById<TextView>(R.id.tv_notic).apply {
            text = Html.fromHtml(getString(R.string.invite_reward_text), Html.FROM_HTML_MODE_LEGACY)
        }
        // 成功列表
        recyclerSuccessView = findViewById(R.id.invitationsuccessRecyclerView)
        addSuccessListener()
        recyclerRecordView = findViewById(R.id.invitationrecordRecyclerView)
        addRecordListener()
    }
    private fun addSuccessListener() {
        recyclerSuccessView.viewTreeObserver.addOnGlobalLayoutListener {
            if (recyclerSuccessView.childCount > 0) {
                // 计算预期高度(60dp转px)
                val expectedHeight = dpToPx(60f)
                if (itemHeight != expectedHeight) {
                    // 修正所有item的高度
                    for (i in 0 until recyclerSuccessView.childCount) {
                        recyclerSuccessView.getChildAt(i).layoutParams.height = expectedHeight
                    }
                    // 请求重新布局
                    recyclerSuccessView.requestLayout()
                    itemHeight = expectedHeight
                }
            }
        }
        recyclerSuccessView.layoutManager = object : LinearLayoutManager(this@InvitationActivity) {
            override fun onMeasure(
                recycler: RecyclerView.Recycler, state: RecyclerView.State,
                widthSpec: Int, heightSpec: Int
            ) {
                if (itemHeight > 0) {
                    // 使用实际测量的高度
                    setMeasuredDimension(
                        View.resolveSize(widthSpec, width),
                        itemHeight
                    )
                } else {
                    // 默认高度60dp
                    val defaultHeight = TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP, 60f,
                        resources.displayMetrics
                    ).toInt()
                    setMeasuredDimension(
                        View.resolveSize(widthSpec, width),
                        defaultHeight
                    )
                }
            }
        }
    }
    private fun addRecordListener() {
        recyclerRecordView.viewTreeObserver.addOnGlobalLayoutListener {
            if (recyclerSuccessView.childCount > 0) {
                // 计算预期高度(60dp转px)
                val expectedHeight = dpToPx(60f)
                if (itemHeight != expectedHeight) {
                    // 修正所有item的高度
                    for (i in 0 until recyclerSuccessView.childCount) {
                        recyclerSuccessView.getChildAt(i).layoutParams.height = expectedHeight
                    }
                    // 请求重新布局
                    recyclerSuccessView.requestLayout()
                    itemHeight = expectedHeight
                }
            }
        }
        recyclerRecordView.layoutManager = object : LinearLayoutManager(this@InvitationActivity) {
        recyclerSuccessView.layoutManager = object : LinearLayoutManager(this) {
            override fun onMeasure(
                recycler: RecyclerView.Recycler, state: RecyclerView.State,
                recycler: RecyclerView.Recycler,
                state: RecyclerView.State,
                widthSpec: Int, heightSpec: Int
            ) {
                if (itemHeight > 0) {
                    // 使用实际测量的高度
                    setMeasuredDimension(
                        View.resolveSize(widthSpec, width),
                        itemHeight
                    )
                } else {
                    // 默认高度60dp
                    val defaultHeight = TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP, 60f,
                        resources.displayMetrics
@@ -193,6 +150,9 @@
                }
            }
        }
        // 记录列表
//        recyclerRecordView = findViewById(R.id.invitationrecordRecyclerView)
//        recyclerRecordView.layoutManager = LinearLayoutManager(this)
    }
    private fun initSuccessAdapter() {
@@ -211,21 +171,25 @@
        recyclerSuccessView.adapter = adapter
    }
    private fun initRecorddapter() {
        recordadapter = InvitationRecordAdapter(this, recorddata).apply {
            registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
                override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
                    // 数据插入时检查高度
                    recyclerRecordView.post {
                        if (recyclerRecordView.childCount > 0) {
                            itemHeight = recyclerRecordView.getChildAt(0).height
                        }
                    }
                }
            })
        }
        recyclerRecordView.adapter = recordadapter
    }
//    private fun initRecorddapter() {
//        recordadapter = InvitationRecordAdapter(this, recorddata)
//        recyclerRecordView.adapter = recordadapter
//    }
//    private fun initRecorddapter() {
//        recordadapter = InvitationRecordAdapter(this, recorddata).apply {
//            registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
//                override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
//                    // 数据插入时检查高度
//                    recyclerRecordView.post {
//                        if (recyclerRecordView.childCount > 0) {
//                            itemHeight = recyclerRecordView.getChildAt(0).height
//                        }
//                    }
//                }
//            })
//        }
//        recyclerRecordView.adapter = recordadapter
//    }
    private fun getInvitereward() {
        lifecycleScope.launch {
@@ -244,10 +208,17 @@
                            addAll(records.myInvite ?: emptyList())  // 注意这里使用myInvite
                        }
                        // 加载适配器
                        invitedRecordRecyclerView2 = findViewById(R.id.invited_record_recycler_view_2)
                        invitedRecordRecyclerView2.layoutManager = LinearLayoutManager(this@InvitationActivity)
                        invitationAdapter = InvitationRecord2Adapter(records.myInvite)
                        invitedRecordRecyclerView2.adapter = invitationAdapter
                        // 在UI线程更新适配器
                        withContext(Dispatchers.Main) {
                            adapter.notifyDataSetChanged()
                            recordadapter.notifyDataSetChanged()
//                            recordadapter.notifyDataSetChanged()
                        }
                    } ?: run {
                        Log.w("API", "Response data is null")
@@ -263,34 +234,6 @@
            }
        }
    }
//    private fun loadData() {
//        data.clear()
//        data.addAll(
//            listOf(
//                InvitationRecord("H****e", "获得了1天会员", "已注册"),
//                InvitationRecord("U****r", "获得了2天会员", "已注册"),
//                InvitationRecord("A****e", "获得了免广告特权", "已注册"),
//                InvitationRecord("B****e", "获得了3天会员", "已注册"),
//                InvitationRecord("C****o", "获得了4天会员", "已注册")
//            )
//        )
//        adapter.notifyDataSetChanged()
//    }
//    private fun loadRecordData() {
//        recorddata.clear()
//        recorddata.addAll(
//            listOf(
//                InvitationRecord("M****e", "", "未注册"),
//                InvitationRecord("Q****r", "", "已注册"),
//                InvitationRecord("W****e", "", "未注册"),
//                InvitationRecord("E****e", "", "未注册"),
//                InvitationRecord("R****o", "", "已注册")
//            )
//        )
//        recordadapter.notifyDataSetChanged()
//    }
    private fun showImagePreviewDialog() {
        val dialog = Dialog(this, android.R.style.Theme_Translucent_NoTitleBar).apply {
@@ -380,7 +323,6 @@
            // 使用取模运算确保位置在有效范围内
            currentScrollPosition++
            //currentRecordScrollPosition++
            // 创建自定义平滑滚动器
            val smoothScroller = object : LinearSmoothScroller(this@InvitationActivity) {
@@ -405,32 +347,9 @@
                targetPosition = currentScrollPosition
            }
//            val smoothRecordScroller = object : LinearSmoothScroller(this@InvitationActivity) {
//                override fun getVerticalSnapPreference(): Int = SNAP_TO_START
//
//                override fun calculateDyToMakeVisible(view: View, snapPreference: Int): Int {
//                    // 计算需要滚动的距离,确保完整显示下一个item
//                    val top = view.top
//                    val height = view.height
//                    return when {
//                        snapPreference == SNAP_TO_START -> -top
//                        snapPreference == SNAP_TO_END -> -(top - (recyclerRecordView.height - height))
//                        else -> -(top - (recyclerRecordView.height / 2 - height / 2))
//                    }
//                }
//
//                override fun calculateTimeForScrolling(dx: Int): Int {
//                    // 根据滚动距离动态计算时间,保持匀速
//                    return maxOf((500f * abs(dx) / itemHeight).toInt(), 200)
//                }
//            }.apply {
//                targetPosition = currentRecordScrollPosition
//            }
            // 启动平滑滚动
            recyclerSuccessView.layoutManager?.startSmoothScroll(smoothScroller)
            //recyclerRecordView.layoutManager?.startSmoothScroll(smoothRecordScroller)
            // 更智能的边界检测
            (recyclerSuccessView.layoutManager as? LinearLayoutManager)?.let { lm ->
@@ -444,18 +363,6 @@
                    recyclerSuccessView.scrollToPosition(jumpPosition)
                }
            }
//            (recyclerRecordView.layoutManager as? LinearLayoutManager)?.let { lm ->
//                val lastVisible = lm.findLastVisibleItemPosition()
//                val totalItems = recordadapter.itemCount
//
//                // 当接近"虚拟列表"末尾时,跳转到中间位置
//                if (lastVisible >= totalItems - 3) {
//                    val jumpPosition = (totalItems / 2) * (Int.MAX_VALUE / totalItems)
//                    currentRecordScrollPosition = jumpPosition
//                    recyclerRecordView.scrollToPosition(jumpPosition)
//                }
//            }
            handler.postDelayed(this, scrollInterval)
        }