作者 liyan

修改BaseFragment

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="11" />
<bytecodeTargetLevel target="1.8" />
</component>
</project>
\ No newline at end of file
... ...
... ... @@ -10,6 +10,7 @@ import androidx.core.content.ContextCompat.getColor
import androidx.core.graphics.ColorUtils
import androidx.fragment.app.Fragment
import com.br_technology.securitytrain_master.R
import com.br_technology.securitytrain_master.databinding.ActivityAccountLoginBinding.inflate
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import java.util.*
... ... @@ -31,10 +32,11 @@ abstract class BaseFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(getLayoutId(), null)
loadService = LoadSir.getDefault().register(rootView) { reLoad() }
val rootView = inflate(inflater, container,false)
// loadService = LoadSir.getDefault().register(rootView) { reLoad() }
// EventBus.getDefault().register(this)
return loadService.loadLayout
// return loadService.loadLayout
return rootView.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
... ...
package com.wjx.android.wanandroidmvvm.base.view
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.br_technology.securitytrain_master.base.common.CommonUtil
import com.br_technology.securitytrain_master.base.common.State
import com.br_technology.securitytrain_master.base.common.StateType
... ... @@ -22,8 +27,21 @@ import com.wjx.android.wanandroidmvvm.common.callback.LoadingCallBack
* Time: 16:36
*/
abstract class BaseLifeCycleFragment<VM : BaseViewModel<*>> : BaseFragment() {
abstract class BaseLifeCycleFragment<VM : BaseViewModel<*>,VB : ViewBinding>(private val inflate: (LayoutInflater, ViewGroup?, Boolean) -> VB
) : BaseFragment() {
protected lateinit var mViewModel: VM
private var _binding: VB? = null
val binding: VB get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = inflate(inflater, container, false)
return binding.root
}
override fun initView() {
... ... @@ -91,4 +109,9 @@ abstract class BaseLifeCycleFragment<VM : BaseViewModel<*>> : BaseFragment() {
super.reLoad()
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
\ No newline at end of file
... ...
package com.br_technology.securitytrain_master.ui.bank
import com.br_technology.securitytrain_master.R
import com.br_technology.securitytrain_master.databinding.FragmentBankBinding
import com.br_technology.securitytrain_master.ui.bank.viewmodel.BankViewModel
import com.wjx.android.wanandroidmvvm.base.view.BaseLifeCycleFragment
... ... @@ -9,7 +10,7 @@ import com.wjx.android.wanandroidmvvm.base.view.BaseLifeCycleFragment
* auth:张继
* des:
*/
class BankFragment :BaseLifeCycleFragment<BankViewModel>() {
class BankFragment :BaseLifeCycleFragment<BankViewModel,FragmentBankBinding>(FragmentBankBinding::inflate) {
override fun initDataObserver() {
}
... ...
... ... @@ -11,6 +11,7 @@ import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.br_technology.securitytrain_master.R
import com.br_technology.securitytrain_master.databinding.FragmentHomeBinding
import com.br_technology.securitytrain_master.ui.home.activity.DatabaseActivity
import com.br_technology.securitytrain_master.ui.home.activity.SearchActivity
import com.br_technology.securitytrain_master.ui.home.activity.TextCourseActivity
... ... @@ -18,9 +19,11 @@ import com.br_technology.securitytrain_master.ui.home.adapter.RecommendDataAdapt
import com.br_technology.securitytrain_master.ui.home.adapter.VideoCourseAdapter
import com.br_technology.securitytrain_master.ui.home.pojo.RecommendData
import com.br_technology.securitytrain_master.ui.home.pojo.VideoCourse
import com.br_technology.securitytrain_master.ui.home.viewmodel.MineViewModel
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.wjx.android.wanandroidmvvm.base.view.BaseLifeCycleFragment
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.adapter.BannerImageAdapter
... ... @@ -33,129 +36,129 @@ import com.youth.banner.indicator.CircleIndicator
* auth:张继
* des:
*/
//class HomeFragment :BaseLifeCycleFragment<MineViewModel>() {
// override fun initDataObserver() {
//
// }
//
// override fun getLayoutId(): Int {
// return R.layout.fragment_home
// }
//}
class HomeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val banner: Banner<Int, BannerImageAdapter<Int>> = view.findViewById(R.id.banner)
banner.setAdapter(object : BannerImageAdapter<Int>(
mutableListOf(
R.mipmap.banner,
R.mipmap.banner,
R.mipmap.banner
)
) {
override fun onBindView(
holder: BannerImageHolder,
data: Int,
position: Int,
size: Int
) {
Glide.with(requireActivity())
.load(data)
.apply(RequestOptions.bitmapTransform(RoundedCorners(8)))
.into(holder.imageView)
}
}).addBannerLifecycleObserver(this)
.indicator = CircleIndicator(requireContext())
// 共享资料库
view.findViewById<TextView>(R.id.database).setOnClickListener {
startActivity(DatabaseActivity::class.java)
}
// 共享资料库
view.findViewById<TextView>(R.id.txt_course).setOnClickListener {
startActivity(TextCourseActivity::class.java)
}
// 在线课程
view.findViewById<TextView>(R.id.online_course).setOnClickListener {
// startActivity()
}
// 视频课程
val videoCourse: RecyclerView = view.findViewById(R.id.video_course)
videoCourse.addItemDecoration(object : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
super.getItemOffsets(outRect, view, parent, state)
if (parent.getChildAdapterPosition(view) % 2 == 0) {
outRect.top = 24
outRect.right = 15
} else {
outRect.top = 24
outRect.left = 15
}
}
})
val videoCourseAdapter = VideoCourseAdapter()
val list = mutableListOf(
VideoCourse(R.mipmap.banner, "10课时", "自动化制作课程", "", "张钧-三岗职位培训班"),
VideoCourse(R.mipmap.banner, "10课时", "自动化制作课程", "", "张钧-三岗职位培训班"),
VideoCourse(R.mipmap.banner, "10课时", "自动化制作课程", "", "张钧-三岗职位培训班"),
VideoCourse(R.mipmap.banner, "10课时", "自动化制作课程", "", "张钧-三岗职位培训班"),
)
videoCourse.adapter = videoCourseAdapter
videoCourseAdapter.addList(list)
val recommendData: RecyclerView = view.findViewById(R.id.recommended_data)
val recommendDataAdapter = RecommendDataAdapter()
val recommendList = mutableListOf(
RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
)
recommendData.adapter = recommendDataAdapter
recommendDataAdapter.addList(recommendList)
view.findViewById<View>(R.id.search).setOnClickListener {
val intent = Intent(requireActivity(), SearchActivity::class.java)
startActivity(intent)
}
}
class HomeFragment : BaseLifeCycleFragment<MineViewModel,FragmentHomeBinding>(FragmentHomeBinding::inflate) {
override fun initDataObserver() {
private fun startActivity(cls: Class<out Activity>) {
startActivity(cls, null)
}
private fun startActivity(cls: Class<out Activity>, bundle: Bundle?) {
val intent = Intent(requireActivity(), cls)
if (bundle != null) {
intent.putExtras(bundle)
}
startActivity(intent)
override fun getLayoutId(): Int {
return R.layout.fragment_home
}
}
\ No newline at end of file
}
//class HomeFragment : Fragment() {
// override fun onCreateView(
// inflater: LayoutInflater,
// container: ViewGroup?,
// savedInstanceState: Bundle?
// ): View? {
// return inflater.inflate(R.layout.fragment_home, container, false)
// }
//
// override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// super.onViewCreated(view, savedInstanceState)
//
//
// val banner: Banner<Int, BannerImageAdapter<Int>> = view.findViewById(R.id.banner)
// banner.setAdapter(object : BannerImageAdapter<Int>(
// mutableListOf(
// R.mipmap.banner,
// R.mipmap.banner,
// R.mipmap.banner
// )
// ) {
// override fun onBindView(
// holder: BannerImageHolder,
// data: Int,
// position: Int,
// size: Int
// ) {
// Glide.with(requireActivity())
// .load(data)
// .apply(RequestOptions.bitmapTransform(RoundedCorners(8)))
// .into(holder.imageView)
//
// }
// }).addBannerLifecycleObserver(this)
// .indicator = CircleIndicator(requireContext())
//
// // 共享资料库
// view.findViewById<TextView>(R.id.database).setOnClickListener {
// startActivity(DatabaseActivity::class.java)
// }
//
// // 共享资料库
// view.findViewById<TextView>(R.id.txt_course).setOnClickListener {
// startActivity(TextCourseActivity::class.java)
// }
// // 在线课程
// view.findViewById<TextView>(R.id.online_course).setOnClickListener {
//// startActivity()
// }
//
// // 视频课程
// val videoCourse: RecyclerView = view.findViewById(R.id.video_course)
// videoCourse.addItemDecoration(object : RecyclerView.ItemDecoration() {
// override fun getItemOffsets(
// outRect: Rect,
// view: View,
// parent: RecyclerView,
// state: RecyclerView.State
// ) {
// super.getItemOffsets(outRect, view, parent, state)
// if (parent.getChildAdapterPosition(view) % 2 == 0) {
// outRect.top = 24
// outRect.right = 15
// } else {
// outRect.top = 24
// outRect.left = 15
// }
// }
// })
// val videoCourseAdapter = VideoCourseAdapter()
// val list = mutableListOf(
// VideoCourse(R.mipmap.banner, "10课时", "自动化制作课程", "", "张钧-三岗职位培训班"),
// VideoCourse(R.mipmap.banner, "10课时", "自动化制作课程", "", "张钧-三岗职位培训班"),
// VideoCourse(R.mipmap.banner, "10课时", "自动化制作课程", "", "张钧-三岗职位培训班"),
// VideoCourse(R.mipmap.banner, "10课时", "自动化制作课程", "", "张钧-三岗职位培训班"),
// )
// videoCourse.adapter = videoCourseAdapter
// videoCourseAdapter.addList(list)
//
// val recommendData: RecyclerView = view.findViewById(R.id.recommended_data)
// val recommendDataAdapter = RecommendDataAdapter()
// val recommendList = mutableListOf(
// RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
// RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
// RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
// RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
// RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
// RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
// RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
// RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
// RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
// RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
// RecommendData(R.mipmap.banner, "思维导图高分作文法(高中)议论", "从这个角度看,康得在不经意间这样说过,既然我已踏上了这条道路,那么"),
// )
// recommendData.adapter = recommendDataAdapter
// recommendDataAdapter.addList(recommendList)
//
//
// view.findViewById<View>(R.id.search).setOnClickListener {
// val intent = Intent(requireActivity(), SearchActivity::class.java)
// startActivity(intent)
// }
// }
//
//
// private fun startActivity(cls: Class<out Activity>) {
// startActivity(cls, null)
// }
//
// private fun startActivity(cls: Class<out Activity>, bundle: Bundle?) {
// val intent = Intent(requireActivity(), cls)
// if (bundle != null) {
// intent.putExtras(bundle)
// }
// startActivity(intent)
// }
//}
\ No newline at end of file
... ...
package com.br_technology.securitytrain_master.ui.main
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.br_technology.securitytrain_master.R
import com.br_technology.securitytrain_master.databinding.ActivityMainBinding
... ... @@ -29,7 +30,7 @@ class MainActivity :
)
binding.apply {
viewPage.adapter = mainAdapter
mainAdapter.addData(list)
mainAdapter.addData(list as List<Fragment>)
viewPage.offscreenPageLimit = 1
home.isChecked = true
... ...
... ... @@ -19,7 +19,7 @@ class MainViewModel:BaseViewModel<MainRepository>() {
}
init {
val list= listOf(HomeFragment(),BankFragment(),MineFragment())
liveData.postValue(list)
liveData.postValue(list as List<Fragment>?)
}
... ...
package com.br_technology.securitytrain_master.ui.home
import android.app.Fragment
import com.br_technology.securitytrain_master.R
import com.br_technology.securitytrain_master.databinding.FragmentMineBinding
import com.br_technology.securitytrain_master.ui.home.viewmodel.MineViewModel
import com.wjx.android.wanandroidmvvm.base.view.BaseLifeCycleFragment
... ... @@ -9,7 +11,7 @@ import com.wjx.android.wanandroidmvvm.base.view.BaseLifeCycleFragment
* auth:张继
* des:
*/
class MineFragment :BaseLifeCycleFragment<MineViewModel>() {
class MineFragment :BaseLifeCycleFragment<MineViewModel,FragmentMineBinding>(FragmentMineBinding::inflate) {
override fun initDataObserver() {
}
... ...