From cb9447ab820c220266c1c35a92196a07d3ab3069 Mon Sep 17 00:00:00 2001 From: kaixed Date: Sat, 7 Dec 2024 10:21:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=BB=E9=A1=B5?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E6=9B=B4=E5=A4=9A=E6=8E=A7=E4=BB=B6=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kaixed/kchat/ui/fragment/HomeFragment.kt | 104 ++++++++++-------- app/src/main/res/layout/fragment_home.xml | 21 ++-- 2 files changed, 65 insertions(+), 60 deletions(-) diff --git a/app/src/main/java/com/kaixed/kchat/ui/fragment/HomeFragment.kt b/app/src/main/java/com/kaixed/kchat/ui/fragment/HomeFragment.kt index a238783..7b10a46 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/fragment/HomeFragment.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/fragment/HomeFragment.kt @@ -13,12 +13,16 @@ import android.graphics.drawable.ColorDrawable import android.os.Build import android.os.Bundle import android.os.IBinder +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.view.ViewGroup.MarginLayoutParams import android.widget.Toast import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity.BIND_AUTO_CREATE +import androidx.core.view.updateLayoutParams +import androidx.fragment.app.viewModels import androidx.recyclerview.widget.LinearLayoutManager import com.kaixed.kchat.R import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore @@ -30,15 +34,16 @@ import com.kaixed.kchat.service.WebSocketService import com.kaixed.kchat.service.WebSocketService.LocalBinder import com.kaixed.kchat.ui.activity.AddFriendsActivity import com.kaixed.kchat.ui.activity.FriendCircleActivity +import com.kaixed.kchat.ui.activity.MainActivity import com.kaixed.kchat.ui.activity.SearchActivity import com.kaixed.kchat.ui.activity.TestActivity -import com.kaixed.kchat.ui.activity.WebViewActivity import com.kaixed.kchat.ui.adapter.ConversationAdapter import com.kaixed.kchat.ui.adapter.MyGridAdapter import com.kaixed.kchat.ui.base.BaseFragment import com.kaixed.kchat.ui.i.OnDialogFragmentClickListener import com.kaixed.kchat.ui.i.OnItemListener import com.kaixed.kchat.ui.widget.HomeDialogFragment +import com.kaixed.kchat.viewmodel.HomeViewModel import io.objectbox.Box class HomeFragment : BaseFragment(), OnItemListener, @@ -54,6 +59,8 @@ class HomeFragment : BaseFragment(), OnItemListener, private var talkerId: String? = null + private val homeViewModel: HomeViewModel by viewModels() + private val items: List by lazy { getHomeItems() } private var webSocketService: WebSocketService? = null @@ -90,14 +97,46 @@ class HomeFragment : BaseFragment(), OnItemListener, initView() startService() setOnClick() + setOnListener() + } + + override fun onResume() { + super.onResume() + setupGridView() + } + + private fun setOnListener() { + binding.gridView.setOnItemClickListener { _, _, position, _ -> + Log.d("haha", "Item clicked at position: $position") + val homeItem = items[position] + val homeItemEnum = HomeItems.entries.find { it.title == homeItem.name } + when (homeItemEnum) { + HomeItems.ADD_FRIENDS -> { + val intent = Intent(context, AddFriendsActivity::class.java) + startActivity(intent) + } + + HomeItems.CLOSE_APP -> { + activity?.finishAffinity() + } + + HomeItems.FRIEND_CIRCLE -> { + startActivity(Intent(context, FriendCircleActivity::class.java)) + } + + HomeItems.FRIENDS -> { + (activity as MainActivity).updatePosition(1) + } + + else -> Toast.makeText(requireContext(), "暂未实现", Toast.LENGTH_SHORT).show() + } + } } private fun setOnClick() { binding.ifvAvatar.setOnClickListener { startActivity(Intent(context, TestActivity::class.java)) } - binding.ivMessage.setOnClickListener { - } binding.ivSearch.setOnClickListener { val intent = Intent(context, SearchActivity::class.java) startActivity(intent) @@ -139,7 +178,9 @@ class HomeFragment : BaseFragment(), OnItemListener, } private fun initView() { - setupGridView() + if (binding.gridView.adapter == null) { + setupGridView() + } setupRecycleView() setupMoreOptionsToggle() } @@ -163,10 +204,17 @@ class HomeFragment : BaseFragment(), OnItemListener, } private fun createAnimator(start: Int, end: Int): ObjectAnimator { - val animator = ObjectAnimator.ofInt(binding.linearlayout, "top", start, end) + val animator = ObjectAnimator.ofInt(binding.linearLayout, "top", start, end) val duration: Long = 500 animator.setDuration(duration) + animator.addUpdateListener { + val value = animator.animatedValue as Int + binding.linearLayout.updateLayoutParams { + topMargin = value + } + } + animator.addListener(object : AnimatorListenerAdapter() { override fun onAnimationStart(animation: Animator) { super.onAnimationStart(animation) @@ -175,9 +223,6 @@ class HomeFragment : BaseFragment(), OnItemListener, override fun onAnimationEnd(animation: Animator) { super.onAnimationEnd(animation) - if (!flipped) { - binding.gridView.visibility = View.INVISIBLE - } binding.ivMore.isEnabled = true } }) @@ -195,11 +240,6 @@ class HomeFragment : BaseFragment(), OnItemListener, conversationAdapter.setItemListener(this) } - private fun resetContent() { - flipImage(binding.ivMore) - flipped = false - } - enum class HomeItems(val title: String, val iconResId: Int, val isEnabled: Boolean) { CHANGE_BACKGROUND("更换背景", R.drawable.ic_switch, true), CREATE_GROUP("创建群聊", R.drawable.ic_troop, false), @@ -221,49 +261,17 @@ class HomeFragment : BaseFragment(), OnItemListener, } } - private fun setupGridView() { - val myGridAdapter = MyGridAdapter(context, items) - binding.gridView.adapter = myGridAdapter - + val gridAdapter = MyGridAdapter(context, items) + binding.gridView.adapter = gridAdapter binding.gridView.selector = ColorDrawable(Color.TRANSPARENT) - - binding.gridView.setOnItemClickListener { _, _, position, _ -> - val homeItem = items[position] - val homeItemEnum = HomeItems.entries.find { it.title == homeItem.name } - when (homeItemEnum) { - HomeItems.ADD_FRIENDS -> { - val intent = Intent(context, AddFriendsActivity::class.java) - startActivity(intent) - } - - HomeItems.CLOSE_APP -> { - requireActivity().finishAffinity() - } - - HomeItems.FRIEND_CIRCLE -> { - startActivity(Intent(context, FriendCircleActivity::class.java)) - } - - else -> Toast.makeText(requireContext(), "暂未实现", Toast.LENGTH_SHORT).show() - } - } - - } - - override fun onResume() { - super.onResume() - if (flipped) { - resetContent() - } -// updateChatLists() } @RequiresApi(Build.VERSION_CODES.TIRAMISU) override fun onStart() { super.onStart() val intent = Intent(context, WebSocketService::class.java) - requireActivity().bindService(intent, connection, BIND_AUTO_CREATE) + activity?.bindService(intent, connection, BIND_AUTO_CREATE) } override fun onStop() { diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index f6e7d94..bba4105 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -78,15 +78,9 @@ - - - + android:layout_height="match_parent"> + android:visibility="invisible" + app:layout_constraintTop_toTopOf="parent" /> + android:layout_height="0dp" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintTop_toTopOf="parent"> - +