我尝试在我的项目(DSL)中使用片段。但是,我得到“无视” 因此,我对MatchFragment.kt使用MatchUI.kt(布局),MatchActivity.kt(容器),并且我使用MainActivity中的工具栏菜单来调用MatchFragment.kt
注意:我有2个菜单(lastMatch和NextMatch),并且我有2个条件:
private fun getEventList() {
when {
isLastMatch -> presenter.getLast15Events(eventId)
isNextMatch -> presenter.getLast15Events(eventId)
else -> {
toast("No Data")
}
}
}
帮助我更正我的代码
MatchUI.kt
class MatchUI : AnkoComponent<Context> {
private lateinit var progressBar: ProgressBar
private lateinit var swipeRefresh: SwipeRefreshLayout
private lateinit var eventList: RecyclerView
override fun createView(ui: AnkoContext<Context>): View {
return with(ui){
linearLayout {
lparams(width = matchParent, height = wrapContent)
orientation = LinearLayout.VERTICAL
topPadding = dip(15)
leftPadding = dip(15)
rightPadding = dip(15)
swipeRefresh = swipeRefreshLayout {
setColorSchemeResources(
R.color.colorAccent,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light)
relativeLayout {
lparams(width = matchParent, height = wrapContent)
eventList = recyclerView {
lparams(width = matchParent, height = wrapContent)
layoutManager = LinearLayoutManager(context)
}
progressBar = progressBar {
}.lparams{
centerHorizontally()
}
}
}
}
}
}
}
匹配Fragment.kt
private const val LIST_LAST_MATCH = "lastMatch"
private const val LIST_NEXT_MATCH = "nextMatch"
class MatchFragment : Fragment(), MatchView {
private val eventId: Int = 4328
private var isLastMatch: Boolean = true
private var isNextMatch: Boolean = true
private var events: MutableList<Event> = mutableListOf()
private lateinit var progressBar: ProgressBar
private lateinit var swipeRefresh: SwipeRefreshLayout
private lateinit var adapter: MatchAdapter
private lateinit var presenter: MatchPresenter
private lateinit var eventlist: RecyclerView
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
adapter = MatchAdapter(events)
eventlist.adapter = adapter
val request = ApiRespository()
val gson = Gson()
presenter = MatchPresenter(this, request, gson)
swipeRefresh.onRefresh {
getEventList()
}
}
private fun getEventList() {
when {
isLastMatch -> presenter.getLast15Events(eventId)
isNextMatch -> presenter.getLast15Events(eventId)
else -> {
toast("No Data")
}
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return MatchUI().createView(AnkoContext.create(ctx))
}
override fun showLoading() {
progressBar.visible()
}
override fun hideLoading() {
progressBar.invisible()
}
override fun showEventList(data: List<Event>) {
swipeRefresh.isRefreshing = false
events.clear()
events.addAll(data)
adapter.notifyDataSetChanged()
}
companion object {
@JvmStatic
fun lastInstance(isLastMatch: Boolean) = MatchFragment(). apply {
arguments = Bundle().apply {
putBoolean(LIST_LAST_MATCH, isLastMatch)
}
}
@JvmStatic
fun nextInstance(isNextMatch: Boolean) = MatchFragment(). apply {
arguments = Bundle().apply {
putBoolean(LIST_NEXT_MATCH, isNextMatch)
}
}
}
}
MatchAktivity.kt
class MatchActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MatchActivityUI().setContentView(this)
}
class MatchActivityUI : AnkoComponent<MatchActivity>{
override fun createView(ui: AnkoContext<MatchActivity>): View {
return with(ui){
linearLayout {
}
frameLayout {
id = R.id.fragmentHolder
lparams(width = matchParent, height = matchParent)
}
}
}
}
}
MainActivity.kt
//Menu
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.getItemId()
if (id == R.id.lastMatch){
lastMatchFragment(savedInstanceState = null)
}
if (id == R.id.nextMatch){
nextMatchFragment(savedInstanceState = null)
}
return true
}
private fun lastMatchFragment(savedInstanceState: Bundle?){
if(savedInstanceState == null){
supportFragmentManager
.beginTransaction()
.replace(R.id.fragmentHolder, MatchFragment.lastInstance(isLastMatch = true), MatchFragment::class.java.simpleName)
.commit()
}
}
private fun nextMatchFragment(savedInstanceState: Bundle?){
if(savedInstanceState == null){
supportFragmentManager
.beginTransaction()
.replace(R.id.fragmentHolder, MatchFragment.nextInstance(isNextMatch = true), MatchFragment::class.java.simpleName)
.commit()
}
}
}