| | |
| | | package com.example.firstapp.ui.invitation |
| | | |
| | | import android.content.ClipData |
| | | import android.content.ClipboardManager |
| | | import android.content.Context |
| | | import android.content.Intent |
| | | import android.net.Uri |
| | | import android.os.Bundle |
| | | import android.os.Handler |
| | | import android.os.Looper |
| | | import android.text.Html |
| | | import android.util.TypedValue |
| | | import android.view.View |
| | | import android.widget.Button |
| | | import android.widget.TextView |
| | | import android.widget.Toast |
| | | import androidx.appcompat.app.AppCompatActivity |
| | | import androidx.recyclerview.widget.LinearLayoutManager |
| | | import androidx.recyclerview.widget.LinearSmoothScroller |
| | |
| | | import com.example.firstapp.adapter.InvitationAdapter |
| | | import com.example.firstapp.adapter.InvitationRecordAdapter |
| | | import com.example.firstapp.entity.InvitationRecord |
| | | import com.example.firstapp.utils.PreferencesManager |
| | | import kotlin.math.abs |
| | | |
| | | class InvitationActivity : AppCompatActivity() { |
| | |
| | | |
| | | //启动轮播 |
| | | startAutoScroll() |
| | | |
| | | //分享 |
| | | val btnInvite = findViewById<Button>(R.id.btnInvite) |
| | | btnInvite.setOnClickListener { |
| | | shareImageToWechat() |
| | | } |
| | | |
| | | //邀请码 |
| | | 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 |
| | | val clip = ClipData.newPlainText( |
| | | "邀请码", |
| | | invitationCodeText.text.toString().replace(" ", "") // 复制时去掉空格 |
| | | ) |
| | | clipboard.setPrimaryClip(clip) |
| | | Toast.makeText(this, "已复制邀请码", Toast.LENGTH_SHORT).show() |
| | | } |
| | | } |
| | | |
| | | private fun initViews() { |
| | |
| | | addRecordListener() |
| | | } |
| | | |
| | | private fun addSuccessListener(){ |
| | | private fun addSuccessListener() { |
| | | recyclerSuccessView.viewTreeObserver.addOnGlobalLayoutListener { |
| | | if (recyclerSuccessView.childCount > 0) { |
| | | // 计算预期高度(60dp转px) |
| | | val expectedHeight = dpToPx(60f) |
| | | if(itemHeight!= expectedHeight){ |
| | | if (itemHeight != expectedHeight) { |
| | | // 修正所有item的高度 |
| | | for (i in 0 until recyclerSuccessView.childCount) { |
| | | recyclerSuccessView.getChildAt(i).layoutParams.height = expectedHeight |
| | |
| | | } |
| | | } |
| | | |
| | | private fun addRecordListener(){ |
| | | private fun addRecordListener() { |
| | | recyclerRecordView.viewTreeObserver.addOnGlobalLayoutListener { |
| | | if (recyclerSuccessView.childCount > 0) { |
| | | // 计算预期高度(60dp转px) |
| | | val expectedHeight = dpToPx(60f) |
| | | if(itemHeight!= expectedHeight){ |
| | | if (itemHeight != expectedHeight) { |
| | | // 修正所有item的高度 |
| | | for (i in 0 until recyclerSuccessView.childCount) { |
| | | recyclerSuccessView.getChildAt(i).layoutParams.height = expectedHeight |
| | |
| | | data.clear() |
| | | data.addAll( |
| | | listOf( |
| | | InvitationRecord("H****e", "获得了1天会员","已注册"), |
| | | InvitationRecord("U****r", "获得了2天会员","已注册"), |
| | | InvitationRecord("A****e", "获得了免广告特权","已注册"), |
| | | InvitationRecord("B****e", "获得了3天会员","已注册"), |
| | | InvitationRecord("C****o", "获得了4天会员","已注册") |
| | | InvitationRecord("H****e", "获得了1天会员", "已注册"), |
| | | InvitationRecord("U****r", "获得了2天会员", "已注册"), |
| | | InvitationRecord("A****e", "获得了免广告特权", "已注册"), |
| | | InvitationRecord("B****e", "获得了3天会员", "已注册"), |
| | | InvitationRecord("C****o", "获得了4天会员", "已注册") |
| | | ) |
| | | ) |
| | | adapter.notifyDataSetChanged() |
| | |
| | | recorddata.clear() |
| | | recorddata.addAll( |
| | | listOf( |
| | | InvitationRecord("M****e", "","未注册"), |
| | | InvitationRecord("Q****r", "","已注册"), |
| | | InvitationRecord("W****e", "","未注册"), |
| | | InvitationRecord("E****e", "","未注册"), |
| | | InvitationRecord("R****o", "","已注册") |
| | | InvitationRecord("M****e", "", "未注册"), |
| | | InvitationRecord("Q****r", "", "已注册"), |
| | | InvitationRecord("W****e", "", "未注册"), |
| | | InvitationRecord("E****e", "", "未注册"), |
| | | InvitationRecord("R****o", "", "已注册") |
| | | ) |
| | | ) |
| | | recordadapter.notifyDataSetChanged() |
| | | } |
| | | |
| | | // 分享资源图片到微信 |
| | | private fun shareImageToWechat() { |
| | | try { |
| | | // 获取资源图片的URI |
| | | val imageUri = Uri.parse("android.resource://${packageName}/${R.drawable.location}") |
| | | |
| | | val intent = Intent().apply { |
| | | action = Intent.ACTION_SEND |
| | | type = "image/*" |
| | | putExtra(Intent.EXTRA_STREAM, imageUri) |
| | | flags = Intent.FLAG_GRANT_READ_URI_PERMISSION |
| | | setPackage("com.tencent.mm") // 指定微信包名 |
| | | } |
| | | |
| | | // 创建选择器,即使微信不可用也能选择其他应用 |
| | | val chooserIntent = Intent.createChooser(intent, "分享邀请图片") |
| | | |
| | | // 检查是否有应用能处理这个Intent |
| | | if (intent.resolveActivity(packageManager) != null) { |
| | | startActivity(chooserIntent) |
| | | } else { |
| | | Toast.makeText(this, "未找到可分享的应用", Toast.LENGTH_SHORT).show() |
| | | } |
| | | } catch (e: Exception) { |
| | | Toast.makeText(this, "分享失败: ${e.message}", Toast.LENGTH_SHORT).show() |
| | | } |
| | | } |
| | | |
| | | private fun formatInvitationCode(code: String): String { |
| | | return if (code.length > 2) { |
| | | code.chunked(2).joinToString(" ") |
| | | } else { |
| | | code |
| | | } |
| | | } |
| | | |
| | | private val scrollRunnable = object : Runnable { |
| | |
| | | private fun pxToDp(px: Int, context: Context): Int { |
| | | return (px / (context.resources.displayMetrics.density)).toInt() |
| | | } |
| | | |
| | | private fun dpToPx(dp: Float): Int { |
| | | return (dp * resources.displayMetrics.density).toInt() |
| | | } |