fix: 修复主页显示更多控件问题

This commit is contained in:
糕小菜 2024-12-07 10:21:21 +08:00
parent b7654c644c
commit cb9447ab82
2 changed files with 65 additions and 60 deletions

View File

@ -13,12 +13,16 @@ import android.graphics.drawable.ColorDrawable
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.IBinder import android.os.IBinder
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import android.widget.Toast import android.widget.Toast
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity.BIND_AUTO_CREATE import androidx.appcompat.app.AppCompatActivity.BIND_AUTO_CREATE
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.kaixed.kchat.R import com.kaixed.kchat.R
import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore 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.service.WebSocketService.LocalBinder
import com.kaixed.kchat.ui.activity.AddFriendsActivity import com.kaixed.kchat.ui.activity.AddFriendsActivity
import com.kaixed.kchat.ui.activity.FriendCircleActivity 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.SearchActivity
import com.kaixed.kchat.ui.activity.TestActivity 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.ConversationAdapter
import com.kaixed.kchat.ui.adapter.MyGridAdapter import com.kaixed.kchat.ui.adapter.MyGridAdapter
import com.kaixed.kchat.ui.base.BaseFragment import com.kaixed.kchat.ui.base.BaseFragment
import com.kaixed.kchat.ui.i.OnDialogFragmentClickListener import com.kaixed.kchat.ui.i.OnDialogFragmentClickListener
import com.kaixed.kchat.ui.i.OnItemListener import com.kaixed.kchat.ui.i.OnItemListener
import com.kaixed.kchat.ui.widget.HomeDialogFragment import com.kaixed.kchat.ui.widget.HomeDialogFragment
import com.kaixed.kchat.viewmodel.HomeViewModel
import io.objectbox.Box import io.objectbox.Box
class HomeFragment : BaseFragment<FragmentHomeBinding>(), OnItemListener, class HomeFragment : BaseFragment<FragmentHomeBinding>(), OnItemListener,
@ -54,6 +59,8 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(), OnItemListener,
private var talkerId: String? = null private var talkerId: String? = null
private val homeViewModel: HomeViewModel by viewModels()
private val items: List<HomeItem> by lazy { getHomeItems() } private val items: List<HomeItem> by lazy { getHomeItems() }
private var webSocketService: WebSocketService? = null private var webSocketService: WebSocketService? = null
@ -90,14 +97,46 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(), OnItemListener,
initView() initView()
startService() startService()
setOnClick() 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() { private fun setOnClick() {
binding.ifvAvatar.setOnClickListener { binding.ifvAvatar.setOnClickListener {
startActivity(Intent(context, TestActivity::class.java)) startActivity(Intent(context, TestActivity::class.java))
} }
binding.ivMessage.setOnClickListener {
}
binding.ivSearch.setOnClickListener { binding.ivSearch.setOnClickListener {
val intent = Intent(context, SearchActivity::class.java) val intent = Intent(context, SearchActivity::class.java)
startActivity(intent) startActivity(intent)
@ -139,7 +178,9 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(), OnItemListener,
} }
private fun initView() { private fun initView() {
setupGridView() if (binding.gridView.adapter == null) {
setupGridView()
}
setupRecycleView() setupRecycleView()
setupMoreOptionsToggle() setupMoreOptionsToggle()
} }
@ -163,10 +204,17 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(), OnItemListener,
} }
private fun createAnimator(start: Int, end: Int): ObjectAnimator { 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 val duration: Long = 500
animator.setDuration(duration) animator.setDuration(duration)
animator.addUpdateListener {
val value = animator.animatedValue as Int
binding.linearLayout.updateLayoutParams<MarginLayoutParams> {
topMargin = value
}
}
animator.addListener(object : AnimatorListenerAdapter() { animator.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator) { override fun onAnimationStart(animation: Animator) {
super.onAnimationStart(animation) super.onAnimationStart(animation)
@ -175,9 +223,6 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(), OnItemListener,
override fun onAnimationEnd(animation: Animator) { override fun onAnimationEnd(animation: Animator) {
super.onAnimationEnd(animation) super.onAnimationEnd(animation)
if (!flipped) {
binding.gridView.visibility = View.INVISIBLE
}
binding.ivMore.isEnabled = true binding.ivMore.isEnabled = true
} }
}) })
@ -195,11 +240,6 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(), OnItemListener,
conversationAdapter.setItemListener(this) conversationAdapter.setItemListener(this)
} }
private fun resetContent() {
flipImage(binding.ivMore)
flipped = false
}
enum class HomeItems(val title: String, val iconResId: Int, val isEnabled: Boolean) { enum class HomeItems(val title: String, val iconResId: Int, val isEnabled: Boolean) {
CHANGE_BACKGROUND("更换背景", R.drawable.ic_switch, true), CHANGE_BACKGROUND("更换背景", R.drawable.ic_switch, true),
CREATE_GROUP("创建群聊", R.drawable.ic_troop, false), CREATE_GROUP("创建群聊", R.drawable.ic_troop, false),
@ -221,49 +261,17 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(), OnItemListener,
} }
} }
private fun setupGridView() { private fun setupGridView() {
val myGridAdapter = MyGridAdapter(context, items) val gridAdapter = MyGridAdapter(context, items)
binding.gridView.adapter = myGridAdapter binding.gridView.adapter = gridAdapter
binding.gridView.selector = ColorDrawable(Color.TRANSPARENT) 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) @RequiresApi(Build.VERSION_CODES.TIRAMISU)
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
val intent = Intent(context, WebSocketService::class.java) val intent = Intent(context, WebSocketService::class.java)
requireActivity().bindService(intent, connection, BIND_AUTO_CREATE) activity?.bindService(intent, connection, BIND_AUTO_CREATE)
} }
override fun onStop() { override fun onStop() {

View File

@ -78,15 +78,9 @@
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_more">
<GridView <GridView
android:id="@+id/gridView" android:id="@+id/gridView"
@ -100,12 +94,15 @@
android:padding="10dp" android:padding="10dp"
android:scrollbars="none" android:scrollbars="none"
android:verticalSpacing="10dp" android:verticalSpacing="10dp"
android:visibility="invisible" /> android:visibility="invisible"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout <LinearLayout
android:id="@+id/linearlayout" android:id="@+id/linear_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_chat_list" android:id="@+id/recycle_chat_list"
@ -114,5 +111,5 @@
android:background="#F1F1F1" android:background="#F1F1F1"
android:overScrollMode="never" /> android:overScrollMode="never" />
</LinearLayout> </LinearLayout>
</FrameLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout> </LinearLayout>