package com.dollearn.student.ui.home
|
|
import android.app.Activity
|
import android.content.Intent
|
import androidx.core.os.bundleOf
|
import androidx.recyclerview.widget.LinearLayoutManager
|
import cn.sinata.xldutils.fragment.BaseFragment
|
import com.dollearn.student.R
|
import com.dollearn.student.network.HttpManager
|
import com.dollearn.student.network.entity.Match
|
import com.dollearn.student.network.requestByF
|
import com.dollearn.student.ui.home.adapter.MatchAdapter
|
import kotlinx.android.synthetic.main.layout_common_list.*
|
import org.jetbrains.anko.support.v4.startActivityForResult
|
|
class MyMatchFragment:BaseFragment() {
|
override fun contentViewId() = R.layout.layout_common_list
|
|
private val type by lazy { arguments?.getInt("type", TYPE_ALL)?: TYPE_ALL }
|
private var page = 1
|
private val list = arrayListOf<Match>()
|
private val adapter = MatchAdapter(list,true)
|
|
override fun onFirstVisibleToUser() {
|
rv_list.layoutManager = LinearLayoutManager(requireContext())
|
rv_list.adapter = adapter
|
refreshLayout.setOnRefreshListener {
|
page = 1
|
getData()
|
}
|
refreshLayout.setOnLoadMoreListener {
|
page++
|
getData()
|
}
|
getData()
|
adapter.setOnItemClickListener { view, position ->
|
startActivityForResult<MyMatchDetailActivity>(1,"id" to list[position].id)
|
}
|
}
|
|
private fun getData(){
|
HttpManager.queryMyCompetitionList(type, page).requestByF(this,success = {_,data->
|
if (page == 1)
|
list.clear()
|
list.addAll(data?: arrayListOf())
|
adapter.notifyDataSetChanged()
|
if (list.isEmpty())
|
refreshLayout.finishRefreshWithNoMoreData()
|
else if (data.isNullOrEmpty())
|
refreshLayout.finishLoadMoreWithNoMoreData()
|
else if (page == 1)
|
refreshLayout.finishRefresh()
|
else
|
refreshLayout.finishLoadMore()
|
}){_,_->
|
if (page == 1)
|
refreshLayout.finishRefresh(false)
|
else
|
refreshLayout.finishLoadMore(false)
|
page--
|
}
|
}
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
super.onActivityResult(requestCode, resultCode, data)
|
if (requestCode == Activity.RESULT_OK)
|
refreshLayout.autoRefresh()
|
}
|
|
companion object{
|
const val TYPE_ALL = 0
|
const val TYPE_DOING = 2
|
const val TYPE_FINISHED = 3
|
const val TYPE_UN_START = 1
|
const val TYPE_CANCELED = 4
|
fun newInstance(type:Int):MyMatchFragment{
|
val myMatchFragment = MyMatchFragment()
|
myMatchFragment.arguments = bundleOf("type" to type)
|
return myMatchFragment
|
}
|
}
|
}
|