From a873764794630f726aba27ce4f3c67ae8c62289c Mon Sep 17 00:00:00 2001 From: zhujie <leon.zhu@cloudroam.com.cn> Date: 星期五, 18 四月 2025 17:40:32 +0800 Subject: [PATCH] 样式调整 --- app/src/main/res/layout/activity_invitation_main.xml | 3 app/src/main/res/drawable/table_cell_border.xml | 7 + app/src/main/java/com/example/firstapp/adapter/InvitationRecordAdapter.kt | 12 +- app/src/main/res/drawable/table_cell_border_no_right.xml | 16 ++++ app/src/main/java/com/example/firstapp/ui/invitation/InvitationActivity.kt | 146 ++--------------------------------- 5 files changed, 38 insertions(+), 146 deletions(-) diff --git a/app/src/main/java/com/example/firstapp/adapter/InvitationRecordAdapter.kt b/app/src/main/java/com/example/firstapp/adapter/InvitationRecordAdapter.kt index 1d8e5f1..0eb9ae2 100644 --- a/app/src/main/java/com/example/firstapp/adapter/InvitationRecordAdapter.kt +++ b/app/src/main/java/com/example/firstapp/adapter/InvitationRecordAdapter.kt @@ -11,7 +11,6 @@ import com.example.firstapp.entity.InvitationRecord import com.example.firstapp.utils.Log - class InvitationRecordAdapter( private val context: Context, private val data: List<InvitationRecord>, @@ -20,17 +19,16 @@ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InvitationRecordViewHolder { val view = LayoutInflater.from(context) .inflate(R.layout.activity_invitation_record, parent, false) - return InvitationRecordViewHolder(view) } - override fun onBindViewHolder(holder: InvitationRecordViewHolder, position: Int) { - val record = data[position % data.size] // 使用取模实现循环 - holder.recordmessage.text = "${record.userName}" - holder.recordstatus.text = "${record.status}" + override fun onBindViewHolder(holder: InvitationRecordViewHolder, position: Int) { + val record = data[position] // 直接使用实际位置 + holder.recordmessage.text = record.userName + holder.recordstatus.text = record.status } - override fun getItemCount() = if (data.isEmpty()) 0 else Int.MAX_VALUE + override fun getItemCount() = data.size // 直接返回实际数据大小 class InvitationRecordViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val recordmessage: TextView = itemView.findViewById(R.id.recordmessage) diff --git a/app/src/main/java/com/example/firstapp/ui/invitation/InvitationActivity.kt b/app/src/main/java/com/example/firstapp/ui/invitation/InvitationActivity.kt index 2bc9633..efb0e6c 100644 --- a/app/src/main/java/com/example/firstapp/ui/invitation/InvitationActivity.kt +++ b/app/src/main/java/com/example/firstapp/ui/invitation/InvitationActivity.kt @@ -72,9 +72,6 @@ //加载数据 getInvitereward() - //loadData() - - //loadRecordData() //启动轮播 startAutoScroll() @@ -87,7 +84,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 @@ -100,88 +96,40 @@ } } + 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 +141,9 @@ } } } + // 记录列表 + recyclerRecordView = findViewById(R.id.invitationrecordRecyclerView) + recyclerRecordView.layoutManager = LinearLayoutManager(this) } private fun initSuccessAdapter() { @@ -212,18 +163,7 @@ } 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 - } - } - } - }) - } + recordadapter = InvitationRecordAdapter(this, recorddata) recyclerRecordView.adapter = recordadapter } @@ -263,34 +203,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 +292,6 @@ // 使用取模运算确保位置在有效范围内 currentScrollPosition++ - //currentRecordScrollPosition++ // 创建自定义平滑滚动器 val smoothScroller = object : LinearSmoothScroller(this@InvitationActivity) { @@ -405,32 +316,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 +332,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) } diff --git a/app/src/main/res/drawable/table_cell_border.xml b/app/src/main/res/drawable/table_cell_border.xml index e589ce8..528b645 100644 --- a/app/src/main/res/drawable/table_cell_border.xml +++ b/app/src/main/res/drawable/table_cell_border.xml @@ -1,4 +1,7 @@ <shape xmlns:android="http://schemas.android.com/apk/res/android"> - <stroke android:width="0.5dp" android:color="#DDDDDD"/> - <padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp"/> + <solid android:color="#FFFFFF" /> + <!-- 统一设置所有边线 --> + <stroke + android:width="1dp" + android:color="#DDDDDD" /> </shape> \ No newline at end of file diff --git a/app/src/main/res/drawable/table_cell_border_no_right.xml b/app/src/main/res/drawable/table_cell_border_no_right.xml new file mode 100644 index 0000000..1a3662a --- /dev/null +++ b/app/src/main/res/drawable/table_cell_border_no_right.xml @@ -0,0 +1,16 @@ +<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> + <item> + <shape> + <solid android:color="#FFFFFF" /> + <stroke + android:width="1dp" + android:color="#DDDDDD" /> + </shape> + </item> + <!-- 用白色覆盖右边框 --> + <item android:right="1dp"> + <shape> + <solid android:color="#FFFFFF" /> + </shape> + </item> +</layer-list> \ No newline at end of file diff --git a/app/src/main/res/layout/activity_invitation_main.xml b/app/src/main/res/layout/activity_invitation_main.xml index 40b98d9..2859f8b 100644 --- a/app/src/main/res/layout/activity_invitation_main.xml +++ b/app/src/main/res/layout/activity_invitation_main.xml @@ -159,14 +159,13 @@ android:id="@+id/tableLayout" android:layout_width="match_parent" android:layout_height="wrap_content" - android:background="@drawable/table_border" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingBottom="16dp" android:shrinkColumns="*" android:stretchColumns="*"> - <TableRow android:background="@drawable/table_cell_border"> + <TableRow> <TextView android:layout_width="wrap_content" -- Gitblit v1.9.3