Не найдено представление для идентификатора 0x7f090050 (com.project.footballapp: id /gmentHolder) для фрагмента MatchFragment {3f4cd12 # 0 id = 0x7f090050 - PullRequest
0 голосов
/ 07 декабря 2018

Я пытаюсь использовать фрагмент в моем проекте (DSL).но я получаю «Нет представления», поэтому я использую MatchUI.kt (макет), MatchActivity.kt (контейнер) для MatchFragment.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()
                        }
                    }
                }
            }
        }
    }
}

MatchFragment.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()
        }
    }
}
...