修改第三方view 依赖注入 崩溃问题

This commit is contained in:
squallzhjch 2023-01-11 14:24:28 +08:00
parent 6f5962c560
commit e69ff5f97f
8 changed files with 173 additions and 171 deletions

View File

@ -30,17 +30,6 @@
android:supportsRtl="true"
android:theme="@style/Theme.NavinfoVolvo"
android:usesCleartextTraffic="true">
<activity
android:name=".ui.message.MessageActivity"
android:configChanges="orientation"
android:exported="false"
android:label="@string/title_activity_second"
android:screenOrientation="portrait"
android:theme="@style/Theme.NavinfoVolvo.NoActionBar">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name="com.navinfo.volvo.ui.MainActivity"
android:exported="true"

View File

@ -4,7 +4,9 @@ import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.View.inflate
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
@ -21,14 +23,14 @@ import com.navinfo.volvo.tools.DisplayUtil
import com.navinfo.volvo.ui.fragments.BaseFragment
import com.yanzhenjie.recyclerview.*
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.io.IOException
@AndroidEntryPoint
class HomeFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListener {
private var _binding: FragmentHomeBinding? = null
private lateinit var _binding: FragmentHomeBinding
private var headView: HomeAdapterNotingBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
@ -36,14 +38,15 @@ class HomeFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListene
private val viewModel by viewModels<HomeViewModel>()
private val messageAdapter by lazy { HomeAdapter(this) }
private var headBinding: HomeAdapterNotingBinding? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
_binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = _binding!!.root
_binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)
// _binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = _binding.root
_binding.lifecycleOwner = this
headBinding = HomeAdapterNotingBinding.inflate(inflater, container, false)
headView = HomeAdapterNotingBinding.inflate(inflater, container, false)
initView()
return root
}
@ -51,9 +54,9 @@ class HomeFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListene
private fun initView() {
//创建菜单选项
//注意:使用滑动菜单不能开启滑动删除,否则只有滑动删除没有滑动菜单
var mSwipeMenuCreator = SwipeMenuCreator { _, rightMenu, _ ->
val mSwipeMenuCreator = SwipeMenuCreator { _, rightMenu, _ ->
//预览
var previewItem = SwipeMenuItem(context)
val previewItem = SwipeMenuItem(context)
previewItem.height = DisplayUtil.dip2px(requireContext(), 60f)
previewItem.width = DisplayUtil.dip2px(requireContext(), 80f)
previewItem.background = requireContext().getDrawable(R.color.yellow)
@ -62,7 +65,7 @@ class HomeFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListene
rightMenu.addMenuItem(previewItem)
//分享
var shareItem = SwipeMenuItem(context)
val shareItem = SwipeMenuItem(context)
shareItem.height = DisplayUtil.dip2px(requireContext(), 60f)
shareItem.width = DisplayUtil.dip2px(requireContext(), 80f)
shareItem.background = requireContext().getDrawable(R.color.blue)
@ -71,7 +74,7 @@ class HomeFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListene
rightMenu.addMenuItem(shareItem)
//添加菜单自动添加至尾部
var deleteItem = SwipeMenuItem(context)
val deleteItem = SwipeMenuItem(context)
deleteItem.height = DisplayUtil.dip2px(requireContext(), 60f)
deleteItem.width = DisplayUtil.dip2px(requireContext(), 80f)
deleteItem.text = requireContext().getString(R.string.delete)
@ -82,96 +85,93 @@ class HomeFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListene
}
val layoutManager = LinearLayoutManager(context)
_binding?.let {
_binding!!.homeRecyclerview.layoutManager = layoutManager
//自动增加分割线
_binding!!.homeRecyclerview.addItemDecoration(
DividerItemDecoration(
context, layoutManager.orientation
)
_binding.homeRecyclerview.layoutManager = layoutManager
//自动增加分割线
_binding.homeRecyclerview.addItemDecoration(
DividerItemDecoration(
context, layoutManager.orientation
)
//增加侧滑按钮
_binding!!.homeRecyclerview.setSwipeMenuCreator(mSwipeMenuCreator)
//单项点击
_binding!!.homeRecyclerview.setOnItemClickListener(this)
)
//增加侧滑按钮
_binding.homeRecyclerview.setSwipeMenuCreator(mSwipeMenuCreator)
//单项点击
_binding.homeRecyclerview.setOnItemClickListener(this)
_binding!!.homeRecyclerview.setLoadMoreListener {
Log.e("jingo", "下拉加载开始")
_binding.homeRecyclerview.setLoadMoreListener {
Log.e("jingo", "下拉加载开始")
} // 加载更多的监听。
} // 加载更多的监听。
//开始下拉刷新
_binding!!.homeSwipeRefreshLayout.setOnRefreshListener {
Log.e("jingo", "开始刷新")
//开始下拉刷新
_binding.homeSwipeRefreshLayout.setOnRefreshListener {
Log.e("jingo", "开始刷新")
// viewModel.getNetMessageList()
messageAdapter.refresh()
}
messageAdapter.refresh()
}
//列表自动分页
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.getNetMessageList().collect {
messageAdapter.submitData(it)
}
//列表自动分页
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.getNetMessageList().collect {
messageAdapter.submitData(it)
}
}
//侧滑菜单的监听必需在设置adapter之前
_binding!!.homeRecyclerview.setOnItemMenuClickListener { menuBridge, adapterPosition ->
menuBridge.closeMenu()
}
//侧滑菜单的监听必需在设置adapter之前
_binding.homeRecyclerview.setOnItemMenuClickListener { menuBridge, adapterPosition ->
menuBridge.closeMenu()
// val direction: Int = menuBridge.getDirection() // 左侧还是右侧菜单。
when (menuBridge.position) {// 菜单在RecyclerView的Item中的Position。
0 -> {//预览按钮
}
1 -> {//分享按钮
}
2 -> {//删除按钮
viewModel.deleteMessage(messageAdapter.getItemData(adapterPosition).id)
}
when (menuBridge.position) {// 菜单在RecyclerView的Item中的Position。
0 -> {//预览按钮
}
}
_binding!!.homeRecyclerview.adapter = messageAdapter
//初始状态添加监听
messageAdapter.addLoadStateListener {
when (it.refresh) {
is LoadState.NotLoading -> {
_binding!!.homeSwipeRefreshLayout.isRefreshing = false
if (messageAdapter.itemCount == 0)
_binding?.let {
_binding!!.homeRecyclerview.addHeaderView(headBinding?.root)
}
else {
_binding?.let {
_binding!!.homeRecyclerview.removeHeaderView(headBinding?.root)
}
}
Log.d("jingo", "is NotLoading")
}
is LoadState.Loading -> {
Log.d("jingo", "is Loading")
_binding!!.homeSwipeRefreshLayout.isRefreshing = true
}
is LoadState.Error -> {
_binding!!.homeSwipeRefreshLayout.isRefreshing = false
Log.d("jingo", "is Error:")
when ((it.refresh as LoadState.Error).error) {
is IOException -> {
Log.d("jingo", "IOException")
}
else -> {
Log.d("jingo", "others exception")
}
}
}
1 -> {//分享按钮
}
2 -> {//删除按钮
viewModel.deleteMessage(messageAdapter.getItemData(adapterPosition).id)
}
}
loadMoreFinish()
}
}
_binding.homeRecyclerview.adapter = messageAdapter
//初始状态添加监听
messageAdapter.addLoadStateListener {
when (it.refresh) {
is LoadState.NotLoading -> {
_binding.homeSwipeRefreshLayout.isRefreshing = false
headView?.let {
if (messageAdapter.itemCount == 0) {
if (_binding.homeRecyclerview.headerCount == 0)
_binding.homeRecyclerview.addHeaderView(headView!!.root)
} else {
if (_binding.homeRecyclerview.headerCount > 0)
_binding.homeRecyclerview.removeHeaderView(headView!!.root)
}
}
}
is LoadState.Loading -> {
_binding.homeSwipeRefreshLayout.isRefreshing = true
}
is LoadState.Error -> {
_binding.homeSwipeRefreshLayout.isRefreshing = false
when ((it.refresh as LoadState.Error).error) {
is IOException -> {
Log.d("jingo", "刷新 IOException $")
}
else -> {
Log.d("jingo", "刷新 others exception")
}
}
}
}
}
loadMoreFinish()
//监听数据请求是否结束
viewModel.isLoading.observe(viewLifecycleOwner, Observer {
if (!it) loadMoreFinish()
@ -179,13 +179,11 @@ class HomeFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListene
}
private fun loadMoreFinish() {
_binding?.let {
_binding!!.homeSwipeRefreshLayout.isRefreshing = false
// 第一次加载数据:一定要掉用这个方法。
// 第一个参数表示此次数据是否为空假如你请求到的list为空(== null || list.size == 0)那么这里就要true。
// 第二个参数表示是否还有更多数据根据服务器返回给你的page等信息判断是否还有更多这样可以提供性能如果不能判断则传true。
_binding!!.homeRecyclerview.loadMoreFinish(false, true)
}
_binding.homeSwipeRefreshLayout.isRefreshing = false
// 第一次加载数据:一定要掉用这个方法。
// 第一个参数表示此次数据是否为空假如你请求到的list为空(== null || list.size == 0)那么这里就要true。
// 第二个参数表示是否还有更多数据根据服务器返回给你的page等信息判断是否还有更多这样可以提供性能如果不能判断则传true。
_binding.homeRecyclerview.loadMoreFinish(false, true)
}
@ -196,8 +194,7 @@ class HomeFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListene
override fun onDestroyView() {
super.onDestroyView()
_binding = null
headBinding = null
headView = null
}
//点击项

View File

@ -51,6 +51,7 @@ import com.nhaarman.supertooltips.ToolTip
import dagger.hilt.android.AndroidEntryPoint
import indi.liyi.viewer.Utils
import indi.liyi.viewer.ViewData
import me.jagar.chatvoiceplayerlibrary.VoicePlayerView
import top.zibin.luban.Luban
import top.zibin.luban.OnCompressListener
import java.io.File
@ -78,6 +79,8 @@ class ObtainMessageFragment : Fragment() {
// onDestroyView.
private val binding get() = _binding!!
private lateinit var voiceView: VoicePlayerView
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
@ -86,6 +89,10 @@ class ObtainMessageFragment : Fragment() {
_binding = FragmentObtainMessageBinding.inflate(inflater, container, false)
val root: View = binding.root
voiceView =
LayoutInflater.from(requireActivity()).inflate(R.layout.widget_voice_player, null)
.findViewById(R.id.voicePlayerView)
_binding!!.llAudioPlay.addView(voiceView)
var messege = arguments?.getParcelable<GreetingMessage>("message")
if (messege == null) {
messege = GreetingMessage(who = obtainMessageViewModel.username)
@ -493,12 +500,12 @@ class ObtainMessageFragment : Fragment() {
}
override fun success(file: File) {
binding.voicePlayerView.setAudio(localFile.absolutePath)
voiceView.setAudio(localFile.absolutePath)
}
})
} else {
binding.voicePlayerView.setAudio(localFile.absolutePath)
voiceView.setAudio(localFile.absolutePath)
}
}

View File

@ -1,47 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragments.home.HomeFragment">
xmlns:tools="http://schemas.android.com/tools">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/home_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<data>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragments.home.HomeFragment">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/home_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="请输入查询内容" />
</com.google.android.material.textfield.TextInputLayout>
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/home_SwipeRefreshLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/home_search">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入查询内容" />
</com.google.android.material.textfield.TextInputLayout>
<com.yanzhenjie.recyclerview.SwipeRecyclerView
android:id="@+id/home_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/home_SwipeRefreshLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/home_search">
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<com.yanzhenjie.recyclerview.SwipeRecyclerView
android:id="@+id/home_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

View File

@ -232,24 +232,7 @@
android:layout_gravity="center"
android:visibility="gone"
android:orientation="horizontal">
<me.jagar.chatvoiceplayerlibrary.VoicePlayerView
android:id="@+id/voicePlayerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:enableVisualizer="true"
app:visualizationPlayedColor="@color/teal_200"
app:visualizationNotPlayedColor="@color/teal_700"
app:playPauseBackgroundColor="@color/teal_700"
app:timingBackgroundColor="@color/purple_200"
app:seekBarProgressColor="@color/purple_500"
app:showShareButton="false"
app:shareCornerRadius="100"
app:playPauseCornerRadius="100"
app:showTiming="true"
app:viewCornerRadius="100"
app:viewBackground="@android:color/transparent"
app:progressTimeColor="@color/purple_500"
app:seekBarThumbColor="@color/teal_200"/>
</LinearLayout>
<com.nhaarman.supertooltips.ToolTipRelativeLayout

View File

@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="暂无数据,可下拉刷新"
android:textSize="16sp" />
android:text="暂无数据,可下拉刷新" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<me.jagar.chatvoiceplayerlibrary.VoicePlayerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/voicePlayerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:enableVisualizer="true"
app:playPauseBackgroundColor="@color/teal_700"
app:playPauseCornerRadius="100"
app:progressTimeColor="@color/purple_500"
app:seekBarProgressColor="@color/purple_500"
app:seekBarThumbColor="@color/teal_200"
app:shareCornerRadius="100"
app:showShareButton="false"
app:showTiming="true"
app:timingBackgroundColor="@color/purple_200"
app:viewBackground="@android:color/transparent"
app:viewCornerRadius="100"
app:visualizationNotPlayedColor="@color/teal_700"
app:visualizationPlayedColor="@color/teal_200" />

View File

@ -24,13 +24,11 @@
tools:layout="@layout/fragment_home">
<action
android:id="@+id/action_home_to_obtainMessage"
app:destination="@id/navigation_obtain_message"
/>
app:destination="@id/navigation_obtain_message" />
<argument
android:name="message"
app:nullable="true"
app:argType="com.navinfo.volvo.database.entity.GreetingMessage"
/>
app:nullable="true" />
</fragment>
<fragment
@ -49,7 +47,6 @@
android:id="@+id/navigation_obtain_message"
android:name="com.navinfo.volvo.ui.fragments.message.ObtainMessageFragment"
android:label="问候编辑"
tools:layout="@layout/fragment_obtain_message" >
</fragment>
tools:layout="@layout/fragment_obtain_message"></fragment>
</navigation>