Skip to content

Commit

Permalink
feat:增加音乐、相声等功能,优化Handler用法
Browse files Browse the repository at this point in the history
  • Loading branch information
Rickon committed Jul 17, 2020
1 parent 68eb1c0 commit d5e2d51
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 325 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 25,6 @@ Public contributions are welcomed in the form of pull requests with a clear desc
## Thanks to the open source project

* [glide](https://github.com/bumptech/glide)
* [Litepal](https://github.com/LitePalFramework/LitePal)
* [blurkit-android](https://github.com/CameraKit/blurkit-android)

## About me
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 49,7 @@ open class BaseActivity : AppCompatActivity() {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}

protected open fun mainHandlerMessage(activity: BaseActivity?, msg: Message?) {}
protected open fun mainHandlerMessage(activity: BaseActivity?, msg: Message) {}

companion object {
class WithoutLeakHandler(baseActivity: BaseActivity) : Handler() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 21,7 @@ import kotlinx.android.synthetic.main.activity_radio.*

class RadioActivity : BaseActivity(), View.OnClickListener {

private lateinit var mRecommendRadioList: MutableList<Radio>
private var mRecommendRadioList: MutableList<Radio> = mutableListOf()
private var mLoading = false
private var currentRadioPos = Int.MAX_VALUE

Expand All @@ -33,45 33,29 @@ class RadioActivity : BaseActivity(), View.OnClickListener {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_radio)

initView()
initListener()

setSupportActionBar(radio_toolbar)
//禁止显示默认 title
supportActionBar?.setDisplayShowTitleEnabled(false)
radio_toolbar.setNavigationOnClickListener { finish() }
initRecyclerView()

loadRadioList()

mPlayerServiceManager = XmPlayerManager.getInstance(applicationContext)
mPlayerServiceManager?.addPlayerStatusListener(mPlayerStatusListener)
}

override fun mainHandlerMessage(activity: BaseActivity?, msg: Message?) {
override fun mainHandlerMessage(activity: BaseActivity?, msg: Message) {
super.mainHandlerMessage(activity, msg)
when (msg?.what) {
MSG_LOAD_RADIO_SUCCESS -> {
val myActivity = activity as RadioActivity
myActivity?.let {
it.recommend_radio_list.layoutManager = LinearLayoutManager(it.applicationContext, LinearLayoutManager.HORIZONTAL, false)
it.horiRadioAdapter = HoriRadioAdapter(it.applicationContext, it.mRecommendRadioList)
it.recommend_radio_list.adapter = it.horiRadioAdapter

it.horiRadioAdapter.setOnKotlinItemClickListener(object : HoriRadioAdapter.IKotlinItemClickListener {
override fun onItemClickListener(position: Int) {
if (position != it.currentRadioPos) {
Log.d(TAG, position.toString())
it.currentRadioPos = position
val radio = it.mRecommendRadioList[position]
//播放直播
it.mPlayerServiceManager?.playLiveRadioForSDK(radio, -1, -1)
it.horiRadioAdapter.notifyDataSetChanged()
}
}
})
}
}
when (msg.what) {
MSG_LOAD_RADIO_SUCCESS -> horiRadioAdapter.notifyDataSetChanged()
}
}

private fun initView() {
setSupportActionBar(radio_toolbar)
//禁止显示默认 title
supportActionBar?.setDisplayShowTitleEnabled(false)
radio_toolbar.setNavigationOnClickListener { finish() }
}

private fun initListener() {
Expand All @@ -80,6 64,25 @@ class RadioActivity : BaseActivity(), View.OnClickListener {
province_city_btn.setOnClickListener(this)
}

private fun initRecyclerView() {
recommend_radio_list.layoutManager = LinearLayoutManager(applicationContext, LinearLayoutManager.HORIZONTAL, false)
horiRadioAdapter = HoriRadioAdapter(applicationContext, mRecommendRadioList)
recommend_radio_list.adapter = horiRadioAdapter

horiRadioAdapter.setOnKotlinItemClickListener(object : HoriRadioAdapter.IKotlinItemClickListener {
override fun onItemClickListener(position: Int) {
if (position != currentRadioPos) {
Log.d(TAG, position.toString())
currentRadioPos = position
val radio = mRecommendRadioList[position]
//播放直播
mPlayerServiceManager?.playLiveRadioForSDK(radio, -1, -1)
horiRadioAdapter.notifyDataSetChanged()
}
}
})
}

/**
* 加载对应省份直播电台不播放
*/
Expand All @@ -94,11 97,8 @@ class RadioActivity : BaseActivity(), View.OnClickListener {
CommonRequest.getRadios(map, object : IDataCallBack<RadioList> {
override fun onSuccess(radioList: RadioList?) {
if (radioList?.radios != null) {
mRecommendRadioList = radioList.radios

val msg = Message()
msg.what = MSG_LOAD_RADIO_SUCCESS
mainHandler.sendMessage(msg)
mRecommendRadioList.addAll(radioList.radios)
mainHandler.sendEmptyMessage(MSG_LOAD_RADIO_SUCCESS)
}
mLoading = false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 1,7 @@
package com.rickon.ximalayakotlin.activities

import android.os.Bundle
import android.os.Message
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.rickon.ximalayakotlin.R
Expand All @@ -18,7 19,7 @@ import kotlinx.android.synthetic.main.activity_country_radio.*

class RadioListActivity : BaseActivity() {

private lateinit var mRecommendRadioList: MutableList<Radio>
private var mRecommendRadioList: MutableList<Radio> = mutableListOf()
private lateinit var verticalRadioAdapter: VerticalRadioAdapter

private var mLoading = false
Expand All @@ -33,6 34,8 @@ class RadioListActivity : BaseActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_country_radio)

initRecyclerView()

provinceCode = intent.getStringExtra("province_code")
provinceName = intent.getStringExtra("province_name")

Expand All @@ -48,6 51,34 @@ class RadioListActivity : BaseActivity() {
mPlayerServiceManager?.addPlayerStatusListener(mPlayerStatusListener)
}

override fun mainHandlerMessage(activity: BaseActivity?, msg: Message) {
super.mainHandlerMessage(activity, msg)
when (msg.what) {
MSG_LOAD_RADIO_SUCCESS -> verticalRadioAdapter.notifyDataSetChanged()
}
}

private fun initRecyclerView() {
country_radio_recycler.layoutManager = LinearLayoutManager(applicationContext,
LinearLayoutManager.VERTICAL, false)
verticalRadioAdapter = VerticalRadioAdapter(applicationContext, mRecommendRadioList)
country_radio_recycler.adapter = verticalRadioAdapter

verticalRadioAdapter.setOnKotlinItemClickListener(object : VerticalRadioAdapter.IKotlinItemClickListener {
override fun onItemClickListener(position: Int) {
if (position != currentRadioPos) {
Log.d(TAG, position.toString())
currentRadioPos = position

val radio = mRecommendRadioList[position]
//播放直播
mPlayerServiceManager?.playLiveRadioForSDK(radio, -1, -1)
verticalRadioAdapter.notifyDataSetChanged()
}
}
})
}

/**
* 加载国家台
*/
Expand All @@ -70,27 101,8 @@ class RadioListActivity : BaseActivity() {
CommonRequest.getRadios(map, object : IDataCallBack<RadioList> {
override fun onSuccess(radioList: RadioList?) {
if (radioList?.radios != null) {
mRecommendRadioList = radioList.radios

country_radio_recycler.layoutManager = LinearLayoutManager(applicationContext,
LinearLayoutManager.VERTICAL, false)
verticalRadioAdapter = VerticalRadioAdapter(applicationContext, mRecommendRadioList)
country_radio_recycler.adapter = verticalRadioAdapter

verticalRadioAdapter.setOnKotlinItemClickListener(object : VerticalRadioAdapter.IKotlinItemClickListener {
override fun onItemClickListener(position: Int) {
if (position != currentRadioPos) {
Log.d(TAG, position.toString())
currentRadioPos = position

val radio = mRecommendRadioList[position]
//播放直播
mPlayerServiceManager?.playLiveRadioForSDK(radio, -1, -1)

verticalRadioAdapter.notifyDataSetChanged()
}
}
})
mRecommendRadioList.addAll(radioList.radios)
mainHandler.sendEmptyMessage(MSG_LOAD_RADIO_SUCCESS)
}
mLoading = false
}
Expand Down Expand Up @@ -151,7 163,7 @@ class RadioListActivity : BaseActivity() {
private const val COUNTRY_RADIO_TYPE = 1
private const val PROVINCE_RADIO_TYPE = 2
private const val NET_RADIO_TYPE = 3
private const val LOAD_PROVINCE_SUCCESS = 4
private const val MSG_LOAD_RADIO_SUCCESS = 4
}

}
Original file line number Diff line number Diff line change
@@ -1,7 1,6 @@
package com.rickon.ximalayakotlin.activities

import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
Expand All @@ -22,37 21,16 @@ import kotlinx.android.synthetic.main.activity_rank_list.*

class RankListActivity : BaseActivity() {

private lateinit var mRankRadioList: List<Radio>
private var mRankRadioList: MutableList<Radio> = mutableListOf()
private lateinit var verticalRadioAdapter: VerticalRadioAdapter
private var currentRadioPos = Integer.MAX_VALUE

private var mPlayerServiceManager: XmPlayerManager? = null

override fun mainHandlerMessage(activity: BaseActivity?, msg: Message?) {
override fun mainHandlerMessage(activity: BaseActivity?, msg: Message) {
super.mainHandlerMessage(activity, msg)
when (msg?.what) {
LOAD_RADIO_RANK_SUCCESS -> {
rank_radio_list.layoutManager = LinearLayoutManager(applicationContext, RecyclerView.VERTICAL, false)
//下面代码解决滑动无惯性的问题
rank_radio_list.isNestedScrollingEnabled = false
verticalRadioAdapter = VerticalRadioAdapter(applicationContext, mRankRadioList)
rank_radio_list.adapter = verticalRadioAdapter

verticalRadioAdapter.setOnKotlinItemClickListener(object : VerticalRadioAdapter.IKotlinItemClickListener {
override fun onItemClickListener(position: Int) {
if (position != currentRadioPos) {
Log.d(TAG, position.toString())
currentRadioPos = position

val radio = mRankRadioList[position]
//播放直播
mPlayerServiceManager?.playLiveRadioForSDK(radio, -1, -1)

verticalRadioAdapter.notifyDataSetChanged()
}
}
})
}
when (msg.what) {
LOAD_RADIO_RANK_SUCCESS -> verticalRadioAdapter.notifyDataSetChanged()
}
}

Expand All @@ -62,6 40,8 @@ class RankListActivity : BaseActivity() {

initToolBar()

initRecyclerView()

loadRankRadioList()

mPlayerServiceManager = XmPlayerManager.getInstance(applicationContext)
Expand All @@ -75,18 55,37 @@ class RankListActivity : BaseActivity() {
rank_list_toolbar.setNavigationOnClickListener { finish() }
}

private fun initRecyclerView() {
rank_radio_list.layoutManager = LinearLayoutManager(applicationContext, RecyclerView.VERTICAL, false)
//下面代码解决滑动无惯性的问题
rank_radio_list.isNestedScrollingEnabled = false
verticalRadioAdapter = VerticalRadioAdapter(applicationContext, mRankRadioList)
rank_radio_list.adapter = verticalRadioAdapter

verticalRadioAdapter.setOnKotlinItemClickListener(object : VerticalRadioAdapter.IKotlinItemClickListener {
override fun onItemClickListener(position: Int) {
if (position != currentRadioPos) {
Log.d(TAG, position.toString())
currentRadioPos = position

val radio = mRankRadioList[position]
//播放直播
mPlayerServiceManager?.playLiveRadioForSDK(radio, -1, -1)

verticalRadioAdapter.notifyDataSetChanged()
}
}
})
}

private fun loadRankRadioList() {
val map = HashMap<String, String>()
map[DTransferConstants.RADIO_COUNT] = 10.toString()
CommonRequest.getRankRadios(map, object : IDataCallBack<RadioList> {
override fun onSuccess(p0: RadioList?) {
if (p0?.radios != null) {
mRankRadioList = p0.radios


val msg = Message()
msg.what = LOAD_RADIO_RANK_SUCCESS
mainHandler.sendMessage(msg)
mRankRadioList.addAll(p0.radios)
mainHandler.sendEmptyMessage(LOAD_RADIO_RANK_SUCCESS)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,10 @@ package com.rickon.ximalayakotlin.fragment

import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Message
import androidx.fragment.app.Fragment
import java.lang.ref.WeakReference

/**
* @Description:
Expand All @@ -12,6 15,7 @@ import androidx.fragment.app.Fragment
*/
open class BaseFragment() : Fragment() {
lateinit var mContext: Context
protected lateinit var mainHandler: WithoutLeakHandler

override fun onAttach(context: Context) {
super.onAttach(context)
Expand All @@ -20,6 24,21 @@ open class BaseFragment() : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mainHandler = WithoutLeakHandler(this)
}

protected open fun mainHandlerMessage(baseFragment: BaseFragment?, msg: Message) {}

companion object {
class WithoutLeakHandler(baseFragment: BaseFragment) : Handler() {
private var baseFragment: WeakReference<BaseFragment> = WeakReference(baseFragment)

override fun handleMessage(msg: Message) {
super.handleMessage(msg)
val myFragment = baseFragment.get()
myFragment?.mainHandlerMessage(myFragment, msg)
}
}
}

}
Loading

0 comments on commit d5e2d51

Please sign in to comment.