Commit 95be3231 authored by jyx's avatar jyx

代码优化

parent 8b18cf92
package com.mints.goodmoney.ui.fragment
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.view.View
import androidx.fragment.app.FragmentTransaction
import com.mints.goodmoney.R
import com.mints.goodmoney.common.AppConfig
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.mvp.model.MainVedioMsgBean
import com.mints.goodmoney.mvp.presenters.HorizChannelPresenter
import com.mints.goodmoney.mvp.views.HorizChannelView
import com.mints.goodmoney.ui.activitys.AwardActivity
import com.mints.goodmoney.ui.fragment.base.BaseFragment
import com.yilan.sdk.player.ylplayer.YLPlayerConfig
import com.yilan.sdk.player.ylplayer.callback.OnPlayerCallBack
import com.yilan.sdk.ui.category.ChannelFragment
import kotlinx.android.synthetic.main.layout_red_box.*
import net.grandcentrix.tray.AppPreferences
/**
* 描述:横版视频页面
* 作者:孟崔广
* 时间:2020/10/29 13:48
*/
class HorizChannelFragment : BaseFragment(), HorizChannelView, View.OnClickListener {
private val TAG = HorizChannelFragment::class.java.simpleName
private val horizChannelPresenter by lazy { HorizChannelPresenter() }
private val ps by lazy { AppPreferences(context) }
private var channelFragment: ChannelFragment? = null
// 是否是第一次观看完视频
private var isFirstWatchVideo = true
private var isFirstLoadVideo = true // 首次加载视频初始化
private var rewardDoubleCoin = 0 // 大金币
private var rewardCoin = 0 // 小金币
private var isToCreateReward = true // 是否还有剩余红包 服务器返回
private var isAward = false // 是否可以领金币 本地判断
override fun initViewsAndEvents() {
horizChannelPresenter.attachView(this)
initChannelFragment()
initListener()
}
override fun getContentViewLayoutID() = R.layout.fragment_horiz_channel
override fun setUserVisibleHint(isVisibleToUser: Boolean) {
super.setUserVisibleHint(isVisibleToUser)
channelFragment?.userVisibleHint = isVisibleToUser
}
override fun onHiddenChanged(hidden: Boolean) {
if (hidden) {
onPause()
} else {
onResume()
}
super.onHiddenChanged(hidden)
channelFragment?.onHiddenChanged(hidden)
}
override fun onResume() {
super.onResume()
if (AppConfig.fragmentClickFlag == Constant.FRAGMENT_CLICK_CHANNEL) {
// 调用金币视频信息接口
horizChannelPresenter.getSmallHomeVedioBaseMsg()
registerPlayerCallBack()
channelFragment?.onResume()
if (isToCreateReward) {
// 防止界面切换 重置红包动画
// 好兔视频已加载 且 红包已转满
if (!cdvvYilanTime.isPlaying) {
cdvvYilanTime.startAnim()
}
}
}
}
override fun onPause() {
super.onPause()
if (AppConfig.fragmentClickFlag == Constant.FRAGMENT_CLICK_CHANNEL) {
YLPlayerConfig.config().unRegisterPlayerCallback()
}
pauseDownloadTime()
channelFragment?.onPause()
if (isToCreateReward) {
// 防止界面切换 重置红包动画
// 好兔视频已加载 且 红包已转满
if (!cdvvYilanTime.isPlaying) {
cdvvYilanTime.stopAnim()
}
}
}
override fun onDestroyView() {
cdvvYilanTime?.setCountDownVedioListener(null)
cdvvYilanTime?.onDestory()
super.onDestroyView()
}
override fun onDestroy() {
super.onDestroy()
YLPlayerConfig.config().unRegisterPlayerCallback()
horizChannelPresenter.detachView()
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.cdvvYilanTime -> {
if (isAward && isToCreateReward) {
isAward = false
val bundle = Bundle()
bundle.putInt(Constant.MAIN_CUR_COIN, rewardCoin)
bundle.putString(Constant.MAIN_CARRIER_TYPE, Constant.CARRIER_SMALLHOMEVEDIO)
bundle.putString(Constant.MAIN_EXTRA_ID, rewardDoubleCoin.toString())
readyGo(AwardActivity::class.java, bundle)
}
}
}
}
override fun getHomeVedioMsgSuc(data: MainVedioMsgBean) {
if (activity == null || requireActivity().isFinishing) {
return
}
ps.put(Constant.MAIN_VEDIO_TIME, data.createRewardNeedTime)
rewardCoin = data.rewardCoin
rewardDoubleCoin = data.rewardDoubleCoin
isToCreateReward = data.isToCreateReward
if (isToCreateReward) {
// 防止界面切换 重置倒计时
// 第一次能过 isFirstLoadVedio 判断好兔先加载视频
if (!isFirstLoadVideo && !cdvvYilanTime.isPlaying && !isAward) {
cdvvYilanTime?.stopRedbox()
cdvvYilanTime?.reset()
cdvvYilanTime?.start()
}
} else {
stopDownloadTime()
}
}
/**
* 红包倒计时初始化
*/
private fun initTimeView() {
cdvvYilanTime.setCountDownVedioListener {
if (isFirstWatchVideo) {
// 第一次观看完视频提醒用户
Handler(Looper.getMainLooper()).postDelayed({
vs_tips.visibility = View.GONE
}, 3000)
isFirstWatchVideo = false
}
// 转满一圈,可领金币标记
isAward = true
cdvvYilanTime?.showRedbox()
stopDownloadTime()
}
val videoTime = ps.getInt(Constant.MAIN_VEDIO_TIME, 30)
cdvvYilanTime.setTime(videoTime)
cdvvYilanTime.start()
}
private fun initListener() {
cdvvYilanTime.setOnClickListener(this)
}
private fun initChannelFragment() {
val transaction: FragmentTransaction = childFragmentManager.beginTransaction()
if (channelFragment == null) {
channelFragment = ChannelFragment()
transaction.add(R.id.yilan_fragment_frame_container, channelFragment!!, ChannelFragment::class.java.simpleName)
} else {
transaction.show(channelFragment!!)
}
transaction.commitAllowingStateLoss()
}
/**
* 倒计时红包暂停
*/
private fun pauseDownloadTime() {
cdvvYilanTime?.pause()
}
/**
*用户主动暂时或网络异常结束后 重新加载倒计时红包
*/
private fun resumeDownloadTime(id: String) {
if (!TextUtils.isEmpty(id)) {
if (!isAward) {
cdvvYilanTime?.resume()
}
}
}
/**
* 停止转动
*/
private fun stopDownloadTime() {
cdvvYilanTime?.stop()
}
private fun registerPlayerCallBack() {
// 设置点击的item播放状态,callback返回true标识用户已经处理了event,返回false,标识使用播放器内部逻辑处理event。
YLPlayerConfig.config().registerPlayerCallBack(object : OnPlayerCallBack {
override fun onStart(p0: String?, videoid: String, p2: String?) {
// 防止网络异常时, 第一次加载倒计时红包
if (isFirstLoadVideo) {
isFirstLoadVideo = false
initTimeView()
} else {
resumeDownloadTime(videoid)
}
}
override fun onPause(p0: String?, p1: String?, p2: String?) {
pauseDownloadTime()
}
override fun onResume(p0: String?, videoid: String, p2: String?) {
resumeDownloadTime(videoid)
}
override fun onComplete(p0: String?, videoid: String, p2: String?) {
}
override fun onLoopComplete(pager: String?, videoID: String, taskID: String?, num: Int) {
pauseDownloadTime()
}
override fun onStuckStart(p0: String?, p1: String?, p2: String?) {
}
override fun onStuckEnd(p0: String?, p1: String?, p2: String?) {
}
override fun onError(p0: String?, p1: String?, p2: String?) {
pauseDownloadTime()
}
override fun onStop(p0: String?, p1: String?, p2: String?) {
// pauseDownloadTime()
}
})
}
}
\ No newline at end of file
......@@ -229,6 +229,7 @@ class HorizonChannelFragment : BaseFragment(), HorizChannelView, View.OnClickLis
params.setMargins(0, 0, 0, BubbleUtils.dp2px(30))
cdvvYilanTime.layoutParams = params
cdvvYilanTime.setOnClickListener(this)
rl_fragment_main_redbox.setOnClickListener(this)
}
private fun initChannelFragment() {
......@@ -310,6 +311,7 @@ class HorizonChannelFragment : BaseFragment(), HorizChannelView, View.OnClickLis
if (!requireActivity().isFinishing) {
tv_fragment_main_redbox.text = "可拆开"
rl_fragment_main_redbox.isClickable = true
showRedbox(rl_fragment_main_redbox)
}
}
})
......
package com.mints.goodmoney.ui.fragment
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.androidkun.xtablayout.XTabLayout
import com.mints.goodmoney.R
import com.mints.goodmoney.ui.adapter.MessageFragAdapter
import com.mints.goodmoney.ui.fragment.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_main_money.*
import java.util.*
/**
* 描述:赚钱大厅
......@@ -16,8 +15,21 @@ import java.util.*
*/
class MoneyFragment : BaseFragment(), XTabLayout.OnTabSelectedListener {
private var mainFragment: MainFragment? = null
private var liebaoGameFragment: LiebaoGameFragment? = null
companion object {
const val FRAGMENT_TAG_VIDEO = "FRAGMENT_TAG_VIDEO"
const val FRAGMENT_TAG_BOOKSTORE = "FRAGMENT_TAG_BOOKSTORE"
const val FRAGMENT_TAG_NEWS = "FRAGMENT_TAG_NEWS"
const val FRAGMENT_TAG_GAME = "FRAGMENT_TAG_GAME"
}
// private var mainFragment: MainFragment? = null
// private var liebaoGameFragment: LiebaoGameFragment? = null
private var currentFragment: Fragment? = null
private var mainFragment: Fragment? = null
private var bookStoreFragment: Fragment? = null
private var newsFragment: Fragment? = null
private var liebaoGameFragment: Fragment? = null
private var curPosition = 0//当前标签选中的下标
......@@ -44,36 +56,88 @@ class MoneyFragment : BaseFragment(), XTabLayout.OnTabSelectedListener {
}
private fun initView() {
val tabs: MutableList<String> = ArrayList()
tabs.add("视频")
tabs.add("书城")
tabs.add("新闻")
tabs.add("游戏")
val fragments: MutableList<Fragment> = ArrayList()
mainFragment = MainFragment()
liebaoGameFragment = LiebaoGameFragment()
fragments.add(mainFragment!!)
fragments.add(MyZhangyueFragment())
fragments.add(RsNewsFragment())
fragments.add(liebaoGameFragment!!)
//去除两侧应阴影
vpFragmentMoney.overScrollMode = ViewPager.OVER_SCROLL_NEVER
vpFragmentMoney.offscreenPageLimit = tabs.size + 3
val adapter = MessageFragAdapter(childFragmentManager, fragments, tabs)
vpFragmentMoney.adapter = adapter
xtFragmentMoney.setxTabDisplayNum(tabs.size)
xtFragmentMoney.setupWithViewPager(vpFragmentMoney)
// val tabs: MutableList<String> = ArrayList()
// tabs.add("视频")
// tabs.add("书城")
// tabs.add("新闻")
// tabs.add("游戏")
//
// val fragments: MutableList<Fragment> = ArrayList()
// mainFragment = MainFragment()
// liebaoGameFragment = LiebaoGameFragment()
// fragments.add(mainFragment!!)
// fragments.add(MyZhangyueFragment())
// fragments.add(RsNewsFragment())
// fragments.add(liebaoGameFragment!!)
//
// //去除两侧应阴影
// vpFragmentMoney.overScrollMode = ViewPager.OVER_SCROLL_NEVER
// vpFragmentMoney.offscreenPageLimit = tabs.size + 3
// val adapter = MessageFragAdapter(childFragmentManager, fragments, tabs)
// vpFragmentMoney.adapter = adapter
// xtFragmentMoney.setxTabDisplayNum(tabs.size)
// xtFragmentMoney.setupWithViewPager(vpFragmentMoney)
if (mainFragment == null) {
mainFragment = MainFragment()
}
if (!mainFragment!!.isAdded) {
// 提交事务
childFragmentManager.beginTransaction().add(R.id.fragment_container, mainFragment!!).commitAllowingStateLoss()
// 记录当前Fragment
currentFragment = mainFragment
}
xtFragmentMoney.setxTabDisplayNum(4)
val newTab1 = xtFragmentMoney.newTab()
newTab1.text = "视频"
val newTab2 = xtFragmentMoney.newTab()
newTab2.text = "书城"
val newTab3 = xtFragmentMoney.newTab()
newTab3.text = "新闻"
val newTab4 = xtFragmentMoney.newTab()
newTab4.text = "游戏"
xtFragmentMoney.addTab(newTab1)
xtFragmentMoney.addTab(newTab2)
xtFragmentMoney.addTab(newTab3)
xtFragmentMoney.addTab(newTab4)
xtFragmentMoney.addOnTabSelectedListener(this)
}
/**
* 添加或者显示碎片
*
* @param fm
* @param fragment
*/
private fun addOrShowFragment(fm: FragmentManager,
fragment: Fragment, tag: String) {
if (currentFragment == fragment) return
val ft: FragmentTransaction = fm.beginTransaction()
if (!fragment.isAdded && null == fm.findFragmentByTag(tag)) { // 如果当前fragment未被添加,则添加到Fragment管理器中
ft.hide(currentFragment!!).add(R.id.fragment_container, fragment, tag).commitAllowingStateLoss()
} else {
ft.hide(currentFragment!!).show(fragment).commitAllowingStateLoss()
}
currentFragment = fragment
}
fun selectVedio() {
vpFragmentMoney.currentItem = 0
// vpFragmentMoney.currentItem = 0
if (mainFragment == null) {
mainFragment = MainFragment()
}
addOrShowFragment(childFragmentManager, mainFragment!!, FRAGMENT_TAG_VIDEO)
}
fun selectBook() {
vpFragmentMoney.currentItem = 1
// vpFragmentMoney.currentItem = 1
if (bookStoreFragment == null) {
bookStoreFragment = MyZhangyueFragment()
}
addOrShowFragment(childFragmentManager, bookStoreFragment!!, FRAGMENT_TAG_BOOKSTORE)
}
override fun onTabReselected(tab: XTabLayout.Tab) {
......@@ -83,6 +147,39 @@ class MoneyFragment : BaseFragment(), XTabLayout.OnTabSelectedListener {
}
override fun onTabSelected(tab: XTabLayout.Tab) {
// if (tab.position == 3) {
// if (!UserManager.getInstance().userIsLogin()) {
// readyGo(WxLoginActivity::class.java)
// vpFragmentMoney.currentItem = if (curPosition == 3) 0 else curPosition
// return
// }
// }
when (tab.position) {
0 -> {
if (mainFragment == null) {
mainFragment = MainFragment()
}
addOrShowFragment(childFragmentManager, mainFragment!!, FRAGMENT_TAG_VIDEO)
}
1 -> {
if (bookStoreFragment == null) {
bookStoreFragment = MyZhangyueFragment()
}
addOrShowFragment(childFragmentManager, bookStoreFragment!!, FRAGMENT_TAG_BOOKSTORE)
}
2 -> {
if (newsFragment == null) {
newsFragment = RsNewsFragment()
}
addOrShowFragment(childFragmentManager, newsFragment!!, FRAGMENT_TAG_NEWS)
}
3 -> {
if (liebaoGameFragment == null) {
liebaoGameFragment = LiebaoGameFragment()
}
addOrShowFragment(childFragmentManager, liebaoGameFragment!!, FRAGMENT_TAG_GAME)
}
}
curPosition = tab.position
}
}
\ No newline at end of file
......@@ -7,7 +7,7 @@
<com.androidkun.xtablayout.XTabLayout
android:id="@+id/xtFragmentMoney"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
app:xTabIndicatorColor="#F28335"
......@@ -23,9 +23,9 @@
<View style="@style/view_line_E6E6E6" />
<com.mints.goodmoney.ui.widgets.MyViewPager
android:id="@+id/vpFragmentMoney"
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="match_parent" />
</LinearLayout>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment