android 语音直播间-礼物和背包的弹框和处理
效果如图:礼物和背包都是可以很多页的,可能这是唯一稍微麻烦一点的地方,直接贴全部详细代码。layout dialog_room_detail_gift.xml的代码:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/a...
效果如图:
礼物和背包都是可以很多页的,可能这是唯一稍微麻烦一点的地方,直接贴全部详细代码。
1、首先写一个名为FragmentRoomDetailGift的DialogFragment,是整个的弹框(包括礼物和背包模块,以及点击可以跳转到别的模块的小游戏“摇糖罐”),FragmentRoomDetailGift继承自我封装的抽象类BaseDialogFragment2。
2、礼物和背包的分页,思路是创建一个view的集合,每页设置8个礼物,下面会贴出礼物的页面GiftFragment.kt的代码。
3、关于送礼物走的接口就是看具体情况了,现在送礼物走的是接口,然后回调走的websocket,通知房间里的其他人进行下一步操作。其实最好的方案是进房间走一次接口,其他所有的操作都走websocket,关于websocket这个分类其他文章会写。
BaseDialogFragment2.kt的代码:
abstract class BaseDialogFragment2 : DialogFragment() {
protected val TAG = javaClass.simpleName
protected lateinit var mContext: Context
abstract val layoutRes: Int
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mContext = activity as Context
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var rootView = inflater.inflate(layoutRes, null)
mContext = activity as Context
return rootView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
initView()
super.onViewCreated(view, savedInstanceState)
}
private fun initView() {
initAll()
setListener()
processLogic()
}
protected abstract fun initAll()
protected abstract fun setListener()
protected abstract fun processLogic()
protected fun hideSoftKeyboard(view: View) {
activity?.inputMethodManager?.hideSoftInputFromWindow(view.windowToken, 0)
}
protected fun showSoftKeyboard(view: View) {
activity?.inputMethodManager?.showSoftInput(view, InputMethodManager.SHOW_FORCED)
}
}
FragmentRoomDetailGift.kt的代码:
class FragmentRoomDetailGift : BaseDialogFragment2() {
override val layoutRes: Int
get() = R.layout.dialog_room_detail_gift
//礼物和背包的fragment的集合
var mFragments: Array<Fragment>? = null
//礼物和背包的标题集合
var title: Array<String>? = null
//礼物和背包的pageadapter
var adapter: CustomFragmentPagerAdapter? = null
//选择打赏对象的弹框
var popGiftLeft: PopGiftLeft? = null
//选择赠送多个礼物的弹框
var popGiftRight: PopGiftRight? = null
//一个礼物赠送多次的自定义弹框,这里的数据是本地的
var beenRight = arrayListOf<GiftRightBean>()
//麦上人员的信息/选择被打赏的人
var beenResultLeft = mutableListOf<RoomDetailBean.HousePitBean>()
//一次送出多个礼物的礼物数据
var beanResultRight: GiftRightBean? = null
//点击摇糖罐的接口的实现方法
var mListener: OnFragmentInteractionListener? = null
//语音直播间的id
var room_id = ""
//语音直播间的环信id
var chatrooms = ""
//送给某人的选中的user_id
var to_user_id = ""
override fun onStart() {
super.onStart()
//弹框的样式和动画等的配置
dialog.window.setBackgroundDrawable(ColorDrawable(getResources().getColor(R.color.transparent)))
dialog.window.setGravity(Gravity.BOTTOM)
dialog.window.setLayout(ScreenUtils.getScreenWidth(), (ScreenUtils.getScreenHeight() * 5.2 / 10).toInt())
dialog.window.setWindowAnimations(R.style.ShowDialogBottom)
}
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnFragmentInteractionListener) {
mListener = context as OnFragmentInteractionListener
} else {
throw RuntimeException("$context must implement OnFragmentInteractionListener")
}
}
override fun onDetach() {
super.onDetach()
mListener = null
}
override fun initAll() {
// setStyle(DialogFragment.STYLE_NO_FRAME, R.style.custom_dialog1)
//从直播间详情传递的麦上人员信息
var list = arguments?.getSerializable("bean") as MutableList<RoomDetailBean.HousePitBean>
beenResultLeft = list.filter { !it.user_id.isNullOrEmpty() } as MutableList<RoomDetailBean.HousePitBean>
beenResultLeft?.forEach {
it.isChecked = false
}
//房主在麦位,放在第一个显示
var admin = beenResultLeft?.filter { it.pit_number == 9 }
if (admin?.size != 0) {
beenResultLeft?.removeAll(admin)
beenResultLeft?.addAll(0, admin)
}
//不能打赏自己
var me = beenResultLeft?.filter { it.user_id == MyApplication.getInstance().user.user_id }
beenResultLeft?.removeAll(me)
room_id = arguments?.getString("id") ?: ""
chatrooms = arguments?.getString("chatrooms") ?: ""
to_user_id = arguments?.getString("to_user_id") ?: ""
beenResultLeft?.forEach {
if (it.user_id == to_user_id) {
it.isChecked = true
}
}
refreshCheckedStatus(1)
tv_money.text = MyApplication.getInstance().user.money
if (MyApplication.getInstance().user.is_open_tangguo == 1) {
//开启
tv_sugar.visibility = View.VISIBLE
} else {
//关闭
tv_sugar.visibility = View.GONE
}
title = arrayOf("礼物", "背包")
mFragments = arrayOf(GiftFragment.newInstance(null), BagFragment.newInstance(null))
adapter = CustomFragmentPagerAdapter(mFragments, childFragmentManager)
svp.adapter = adapter
ctl.setViewPager(svp, title)
svp.offscreenPageLimit = 2
svp.setScroll(true)
svp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
if (position == 1) {
btn_all_bag.visibility = View.VISIBLE
} else {
btn_all_bag.visibility = View.GONE
}
}
override fun onPageSelected(position: Int) {
}
})
beenRight?.clear()
beenRight?.also {
it.add(GiftRightBean()?.also { it.name = "1";it.content = "一心一意" })
it.add(GiftRightBean()?.also { it.name = "10";it.content = "十全十美" })
it.add(GiftRightBean()?.also { it.name = "66";it.content = "一切顺利" })
it.add(GiftRightBean()?.also { it.name = "99";it.content = "天长地久" })
it.add(GiftRightBean()?.also { it.name = "188";it.content = "要抱抱" })
it.add(GiftRightBean()?.also { it.name = "520";it.content = "我爱你" })
it.add(GiftRightBean()?.also { it.name = "1314";it.content = "一生一世" })
}
}
override fun setListener() {
tv_sugar.setOnClickListener {
dismiss()
mListener?.onFragmentInteraction(1)
}
//打赏全麦
btn_all_mics.setOnClickListener {
if (beenResultLeft == null || beenResultLeft.size == 0) {
ToastUtils.showShort("没有打赏对象")
return@setOnClickListener
}
if (beenResultLeft.filter { it.isChecked }.size == beenResultLeft?.size) {
beenResultLeft.forEach {
it.isChecked = false
}
} else {
beenResultLeft.forEach {
it.isChecked = true
}
}
refreshCheckedStatus(2)
}
//选择打赏对象
tv_choose.setOnClickListener {
if (beenResultLeft == null || beenResultLeft.size == 0) {
ToastUtils.showShort("没有打赏对象")
return@setOnClickListener
}
if (popGiftLeft == null) {
popGiftLeft = PopGiftLeft(mContext, beenResultLeft) {
beenResultLeft = it
refreshCheckedStatus(2)
}
}
popGiftLeft?.setData(beenResultLeft)
var windowPos = SizeUtils.calculatePopWindowPos2(tv_choose, popGiftLeft?.contentView)
popGiftLeft?.showAtLocation(tv_choose, Gravity.TOP and Gravity.LEFT, windowPos[0], windowPos[1] - 200)
}
tv_count.setOnClickListener {
if (popGiftRight == null) {
popGiftRight = PopGiftRight(mContext, beenRight) {
beanResultRight = it
tv_count.text = beanResultRight?.name
}
}
var windowPos = SizeUtils.calculatePopWindowPos3(tv_count, popGiftRight?.contentView)
popGiftRight?.showAtLocation(tv_count, Gravity.TOP or Gravity.LEFT, windowPos[0], windowPos[1])
}
//跳转到充值
tv_money.setOnClickListener {
startActivityForResult<ChargeActivity>(1)
}
//确认打赏的按钮
btn_pay.setOnClickListener {
var giftBeen = listOf<GiftBean>()
when (svp.currentItem) {
0 -> {
giftBeen = (mFragments!![0] as GiftFragment).allBeen.filter { it.isChecked }
}
1 -> {
giftBeen = (mFragments!![1] as BagFragment).allBeen.filter { it.isChecked }
}
}
if (giftBeen == null || giftBeen.size == 0) {
ToastUtils.showShort("请选择礼物")
return@setOnClickListener
}
var userBeen = beenResultLeft.filter { it.isChecked }
if (userBeen == null || userBeen.size == 0) {
ToastUtils.showShort("请选择打赏对象")
return@setOnClickListener
}
var giftBean = giftBeen[0]
var pits = ""
var user_ids = ""
var pit_names = ""
userBeen.forEach {
pits = pits + it.pit_number + ","
user_ids = user_ids + it.user_id + ","
pit_names = pit_names + it.nickname + ","
}
pits = pits.substring(0, pits.length - 1)
user_ids = user_ids.substring(0, user_ids.length - 1)
pit_names = pit_names.substring(0, pit_names.length - 1)
giveGift(user_ids, giftBean, tv_count.text.toString(), room_id, pits, pit_names, 0, userBeen.size)
}
//背包礼物全部送出的按钮
btn_all_bag.setOnClickListener {
var giftBeen = listOf<GiftBean>()
when (svp.currentItem) {
1 -> {
giftBeen = (mFragments!![1] as BagFragment).allBeen.filter { it.isChecked }
}
}
if (giftBeen == null || giftBeen.size == 0) {
ToastUtils.showShort("请选择礼物")
return@setOnClickListener
}
var userBeen = beenResultLeft.filter { it.isChecked }
if (userBeen == null || userBeen.size == 0) {
ToastUtils.showShort("请选择打赏对象")
return@setOnClickListener
}
if (userBeen.size > 1) {
ToastUtils.showShort("只能送给一个打赏对象")
return@setOnClickListener
}
var giftBean = giftBeen[0]
var pits = ""
var user_ids = ""
var pit_names = ""
userBeen.forEach {
pits = pits + it.pit_number + ","
user_ids = user_ids + it.user_id + ","
pit_names = pit_names + it.nickname + ","
}
pits = pits.substring(0, pits.length - 1)
user_ids = user_ids.substring(0, user_ids.length - 1)
pit_names = pit_names.substring(0, pit_names.length - 1)
AlertDialog.Builder(mContext).setMessage("确定要送出吗?")
.setPositiveButton("确定") { p0, p1 -> giveGift(user_ids, giftBean, giftBean.numbers, room_id, pits, pit_names, 1, 1) }
.setNegativeButton("取消", null)
.show()
}
}
override fun processLogic() {
}
interface OnFragmentInteractionListener {
fun onFragmentInteraction(code: Int)
}
//赠送礼物接口
fun giveGift(user_id: String, gift: GiftBean, number: String, id: String, pit: String, pit_names: String, send_all_status: Int, size: Int) {
Net.post(mContext, UrlUtils().giveGift, mapOf("user_id" to user_id, "gift_id" to if (svp.currentItem == 0) gift.id else gift.gift_id, "number" to number, "id" to id, "pit" to pit, "type" to svp.currentItem + 1, "send_all_status" to send_all_status), object : Net.Callback(mContext, true) {
override fun onSuccess(t: Any?) {
getData()
if (svp.currentItem == 1) {
//从背包送的礼物
(mFragments!![1] as BagFragment).refresh(gift.gift_id, number.toInt(), send_all_status, size)
}
pit_names?.split(",").forEach {
val message = EMMessage.createTxtSendMessage("${MyApplication.getInstance().user.nickname}送给$it", chatrooms)
message.from = MyApplication.getInstance().user.emchat_username
message.chatType = EMMessage.ChatType.ChatRoom
message.setAttribute("action", 2)
message.setAttribute("gift_id", if (svp.currentItem == 0) gift.id else gift.gift_id)
message.setAttribute("gift_pic", gift.picture)
message.setAttribute("gift_name", gift.name)
message.setAttribute("gift_price", gift.price)
message.setAttribute("gift_spectial", gift.special)
message.setAttribute("gift_num", number)
gift.pits = pit
message.setAttribute("pits", gift.pits)
message.setAttribute("user_id", MyApplication.getInstance().user.user_id)
message.setAttribute("nickname", MyApplication.getInstance().user.nickname)
message.setAttribute("avatar", MyApplication.getInstance().user.head_picture)
message.setAttribute("rank_id", MyApplication.getInstance().user.rank_id)
message.setAttribute("nobility_name", MyApplication.getInstance().user.nobility_name)
message.setAttribute("role", MyApplication.getInstance().user.role)
EMClient.getInstance().chatManager().sendMessage(message)
}
EventBusUtils.post(EventBusUtils.EventMessage(EVENT_MESSAGE_GIFT_SEND, gift))//自己展示动画
val jsonObject = JSONObject()
jsonObject.put("type", "giveGift")
jsonObject.put("token", MyApplication.getInstance().token)
jsonObject.put("room_id", room_id)
jsonObject.put("user_id", user_id)
jsonObject.put("gift_id", if (svp.currentItem == 0) gift.id else gift.gift_id)
jsonObject.put("number", number)
WebSocketUtils.getInstance().sendMessage(jsonObject.toString())
val jsonObject1 = JSONObject()
jsonObject1.put("type", "refresh2")
jsonObject1.put("token", MyApplication.getInstance().token)
jsonObject1.put("room_id", room_id)
jsonObject1.put("user_id", user_id)
jsonObject1.put("gift_id", if (svp.currentItem == 0) gift.id else gift.gift_id)
jsonObject1.put("number", number)
WebSocketUtils.getInstance().sendMessage(jsonObject1.toString())
userMoney()
//送完礼物之后,礼物框不消失
// this@FragmentRoomDetailGift.dismiss()
}
override fun onError(apiException: Throwable?) {
if (apiException is ApiException) {
when (apiException.code) {
2 -> {
DialogMoneyCharge(mContext) {
dismiss()
mContext.startActivity<ChargeActivity>()
}.show()
}
}
}
}
override fun onMessage(status: Int, msg: String) {
super.onMessage(status, msg)
ToastUtils.showShort(msg)
}
})
}
//更新金币的数量
fun userMoney() {
Net.post(mContext, UrlUtils().userMoney, emptyMap(), object : Net.Callback() {
override fun onSuccess(t: Any?) {
var user = MyApplication.getInstance().user
user.money = t.toString()
MyApplication.getInstance().user = user
}
override fun onError(apiException: Throwable?) {
}
})
}
//更新选中的打赏对象的状态
fun refreshCheckedStatus(first: Int) {
var list1 = beenResultLeft.filter { it.isChecked }
if (first == 1 && list1.size == 0) {
//点击进入默认选中房主,如果房主在麦位
this.beenResultLeft.forEach {
if (it?.pit_number == 9 && !TextUtils.isEmpty(it?.user_id)) {
it?.isChecked = true
}
}
}
var list = beenResultLeft.filter { it.isChecked }
when (list.size) {
0 -> {
tv_choose.text = "选择打赏对象"
btn_all_mics.setBackgroundResource(R.drawable.bg_r99_trans_colormain)
btn_all_mics.setTextColor(mContext.resources.getColor(R.color.color_main))
}
beenResultLeft.size -> {
tv_choose.text = "${list.size} 打赏全麦"
btn_all_mics.setBackgroundResource(R.drawable.bg_r99_colormain)
btn_all_mics.setTextColor(mContext.resources.getColor(R.color.white))
}
9 -> {
tv_choose.text = "9 打赏全麦"
btn_all_mics.setBackgroundResource(R.drawable.bg_r99_colormain)
btn_all_mics.setTextColor(mContext.resources.getColor(R.color.white))
}
else -> {
var s = ""
list.forEach {
s = s + it.nickname + ","
}
s = s.substring(0, s.length - 1)
tv_choose.text = "${list.size} ${s}"
btn_all_mics.setBackgroundResource(R.drawable.bg_r99_trans_colormain)
btn_all_mics.setTextColor(mContext.resources.getColor(R.color.color_main))
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1) {
getData()
}
}
/*
* 获取金币数量,在金币变动之后调用
* */
fun getData() {
Net.post(mContext, UrlUtils().userInfo, emptyMap(), object : Net.Callback(mContext, false) {
override fun onSuccess(t: Any?) {
val gson = Gson()
var userBean = gson.fromJson(JSON.toJSONString(t), UserBean::class.java)
MyApplication.getInstance().user = userBean
tv_money.text = MyApplication.getInstance().user.money
}
override fun onError(apiException: Throwable?) {
}
})
}
}
layout dialog_room_detail_gift.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:background="@drawable/bg_r8_14123a"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<com.flyco.tablayout.SlidingTabLayout
android:id="@+id/ctl"
android:layout_width="wrap_content"
android:layout_height="@dimen/topbar_height"
app:tl_indicator_color="@color/color_main"
app:tl_indicator_corner_radius="2dp"
app:tl_indicator_gravity="BOTTOM"
app:tl_indicator_height="3dp"
app:tl_indicator_style="NORMAL"
app:tl_indicator_width_equal_title="true"
app:tl_tab_space_equal="false"
app:tl_textSelectColor="@color/color_main"
app:tl_textUnselectColor="@color/color_33"
app:tl_textsize="14sp" />
<TextView
android:id="@+id/tv_sugar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="摇糖罐"
android:textColor="@color/color_33" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/tv_money"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawableLeft="@drawable/talk_icon_bi"
android:drawableRight="@drawable/talk_icon_more_1"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:text="0"
android:textColor="@color/color_99"
android:textSize="11sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/height_line"
android:background="@color/color_f8f8f8" />
<com.lnkj.tangguan.widget.ScrollViewPager
android:id="@+id/svp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_all_bag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="15dp"
android:background="@drawable/bg_r99_colormain"
android:minWidth="76dp"
android:minHeight="32dp"
android:text="全部送出"
android:textColor="@color/white"
android:textSize="14sp"
android:visibility="gone"
tools:visibility="visible" />
<android.support.constraint.ConstraintLayout
android:id="@+id/cl_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="0dp">
<Button
android:id="@+id/btn_all_mics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/bg_r99_trans_colormain"
android:minWidth="60dp"
android:minHeight="32dp"
android:text="全麦"
android:textColor="@color/color_main"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@+id/btn_all_mics"
app:layout_constraintLeft_toRightOf="@+id/btn_all_mics"
app:layout_constraintRight_toLeftOf="@+id/ll_num"
app:layout_constraintTop_toTopOf="@+id/btn_all_mics">
<TextView
android:id="@+id/tv_choose"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/bg_r99_trans_f"
android:drawableRight="@drawable/talk_btn_top"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:singleLine="true"
android:text="选择打赏对象"
android:textColor="@color/color_99"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_num"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="@drawable/bg_r99_trans_colormain"
android:gravity="center_vertical"
app:layout_constraintBottom_toBottomOf="@+id/btn_all_mics"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/btn_all_mics">
<TextView
android:id="@+id/tv_count"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawableRight="@drawable/talk_btn_top"
android:drawablePadding="5dp"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:text="1"
android:textColor="@color/color_33"
android:textSize="14sp" />
<Button
android:id="@+id/btn_pay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_r99_colormain"
android:minWidth="60dp"
android:minHeight="32dp"
android:text="打赏"
android:textColor="@color/white"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</LinearLayout>
drawable bg_r8_14123a.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white" />
<corners
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
</shape>
drawable bg_r99_colormain.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/color_main" />
<corners android:radius="99dp" />
</shape>
drawable bg_r99_trans_colormain.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white" />
<stroke android:color="@color/color_main"
android:width="@dimen/height_line"></stroke>
<corners android:radius="99dp" />
</shape>
GiftFragment.kt的代码:
class GiftFragment : BaseFragment<GiftContract.Present>(), GiftContract.View {
companion object {
fun newInstance(args: Bundle?): GiftFragment {
val fragment = GiftFragment()
fragment.arguments = args
return fragment
}
}
override val mPresenter: GiftContract.Present
get() = GiftPresent().also { it.attachView(this) }
override val layoutRes: Int
get() = R.layout.fragment_gift
internal var map: MutableMap<Int, List<GiftBean>> = HashMap()
internal var adapters: MutableList<GiftAdapter> = ArrayList()
internal var views: MutableList<View> = ArrayList()
var allBeen = mutableListOf<GiftBean>()
override fun initAll() {
mPresenter?.giftWall()
}
override fun setListener() {
}
override fun processLogic() {
}
override fun getContext(): Context {
return mContext
}
override fun onEmpty() {
}
override fun onError() {
}
override fun giftWall(mutableList: MutableList<GiftBean>?) {
if (mutableList == null) {
return
}
allBeen = mutableList
try {
var k = 0
var i = 0
while (i < mutableList.size) {
var been = mutableListOf<GiftBean>()
//8是每页想设置的礼物数量
if (i + 8 > mutableList.size) {
been = mutableList.subList(i, mutableList.size)
} else {
been = mutableList.subList(i, i + 8)
}
map.put(k, been)
k++
views.add(initGridView(been))
i = i + 8
}
val giftPagerAdapter = GiftPagerAdapter(views)
vp.setAdapter(giftPagerAdapter)
ci.setViewPager(vp)
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun initGridView(been: List<GiftBean>): View {
val rootView = layoutInflater.inflate(R.layout.layout_gift_grid, null)
val rv = rootView.findViewById(R.id.gv) as RecyclerView
val giftAdapter = GiftAdapter(been as MutableList<GiftBean>)
rv.layoutManager = GridLayoutManager(mContext, 4)
giftAdapter?.bindToRecyclerView(rv)
giftAdapter?.setOnItemClickListener { adapter, view, position ->
var item = adapter.getItem(position) as GiftBean
allBeen.forEach {
it.isChecked = it.id == item.id
}
refresh()
}
adapters?.add(giftAdapter)
return rootView
}
fun refresh() {
var item = allBeen.filter { it.isChecked }.get(0)
adapters.forEach { adapter ->
adapter.data.forEach {
it.isChecked = item.id == it.id
}
adapter.notifyDataSetChanged()
}
}
}
fragment_gift的代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:background="@color/white"
tools:context=".ui.fragment0.roomdetail.gift.gift.GiftFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<me.relex.circleindicator.CircleIndicator
android:id="@+id/ci"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:ci_animator="@anim/indicator_no_animator"
app:ci_drawable="@drawable/white_radius_square_un"
app:ci_drawable_unselected="@drawable/white_radius_square"
app:ci_height="6dp"
app:ci_margin="6dp"
app:ci_width="6dp" />
</LinearLayout>
</FrameLayout>
注:CircleIndicator的库是 compile ‘me.relex:circleindicator:1.2.2@aar’
更多推荐
所有评论(0)