我似乎无法将我的Kotlin Recycler多视图代码整理出来

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我似乎无法将我的Kotlin Recycler多视图代码整理出来相关的知识,希望对你有一定的参考价值。

所以我知道这是代码起伏但我遇到问题让我的RecyclerView在片段中整理出来。我已经阅读了一堆教程,每个人似乎都有不同的做事方式。只是无法确定它...所以这是我的代码:

FRAGMENT XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.xxxx.listview.CalendarFragment">    

    <android.support.v7.widget.RecyclerView
        android:id="@+id/event_recycle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="horizontal" />
</FrameLayout>

数据类

data class SectionOrRow(var needTicketing:String?,var eventSponsor:String?,var eventED:String?,var eventSD:String?,var eventTitle:String?,var mysectionName:String?,var isRow:Boolean){

    companion object {

        public fun createRow(row: String): Unit {
           // val ret = SectionOrRow()
         //   return ret
        }

        public fun createSection(section: String): Unit {
           // val ret = SectionOrRow(null,section,false)
           // return ret
        }
    }
}

碎片类

package com.example.XXX.listview




class CalendarFragment : Fragment() {
    lateinit var eventInfo: JsonArray<JsonObject>
    lateinit var event2Info: JsonArray<JsonObject>
    val eventList0 = ArrayList<SectionOrRow>()    
    val TAG : String= "CalendarFragment"

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        val view : View = inflater.inflate(R.layout.fragment_calendar,container,false)
        (activity as MainActivity).getSupportActionBar()?.setTitle("Medfield Calendar")
        var event_recycle:RecyclerView = view.findViewById(R.id.event_recycle)


        var myUrl:String = "https://www.nosite.com/functions/API/eventsJSON2.php?zip_code=02052"
        myUrl.httpGet().responseString { request, response, result ->
            //do something with response
            when (result){
                is Result.Failure -> {
                    Log.d("Towns",result.toString())
                    val error = result.getAs<String>()
                    Log.d("Towns error ",error)
                }
                is Result.Success -> {

                    val data = result.getAs<String>()
                    val events : Parser = Parser()
                    val eventBuilder: StringBuilder  = StringBuilder(data)
                    val eventsjson = events.parse(eventBuilder) as JsonObject

                    Log.d("JOO",eventsjson.toString())

                    var eventList0 = ArrayList<SectionOrRow>()


                    eventInfo  = eventsjson.array("events")!!

                    Log.d("length array",this.eventInfo.size.toString())
                    if(this.eventInfo.size<2){
                        Toast.makeText(context,"There are no categories for this town...", Toast.LENGTH_LONG).show()
                    }else{

                        Log.d("type",this.eventInfo.javaClass.name) // kotlin.Double
                        for (i in eventInfo.indices){



                            //Log.d("Loops"+i.toString(),eventInfo[i].string("category"))
                            //Log.d("Loops"+i.toString(),eventInfo[i].string("events2"))
                            var theCat = eventInfo[i].string("category")
                            eventList0.add(SectionOrRow(null,null,null,null,null,theCat,false))


                            var theEvents:JsonArray<JsonObject>? = eventInfo[i].array("events2")
                            Log.d("MSG1","IS category header"+theCat)
                            theEvents?.let{
                                for(x in theEvents.indices){
                                    val needT: String? = theEvents[x].string("news_needsticketing")
                                    val eventSpon: String? = theEvents[x].string("news_sponsor")
                                    val startD: String? = theEvents[x].string("news_start_date")
                                    val endD: String? = theEvents[x].string("news_expiration")
                                    val newsT: String? = theEvents[x].string("news_tile")

                                    eventList0.add(SectionOrRow(needT,eventSpon,endD,startD,newsT,theCat,true))
                                }
                                Log.d("MSG2","Are Events")
                            }

                            //eventList0.add(SectionOrRow.createRow("City"))
                        }


                    }

                    event_recycle.layoutManager = LinearLayoutManager(context)
                    event_recycle.hasFixedSize()
                    event_recycle.adapter = MyEventAdapter(context,eventList0)
                    Log.d("All Size First",eventList0.size.toString())



                    //Log.d("FN",nextOne.toString())
                    // myview.textView3.setText(html.fromHtml(ownerMessage,Html.FROM_HTML_MODE_LEGACY))
                    //myview.textView3.setText(myCS)

                }
            }
        }

        // Inflate the layout for this fragment
        return view

    }

    inner class MyEventAdapter(context: Context,val theEvents:ArrayList<SectionOrRow>):RecyclerView.Adapter<RecyclerView.ViewHolder>(){

        override fun getItemViewType(position: Int): Int {
            if(theEvents[position].isRow){
                return 0
            }else{
                return 1
            }


        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val item = theEvents.get(position)

            if(item.isRow){
                val h = holder as RowViewHolder
                h.textView.setText(item.mysectionName)
            }else{
                val h = holder as SectionViewHolder
                h.textView.setText(item.eventSD)
            }

        }



        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder? {

            if(viewType==0) {
                val v = LayoutInflater.from(parent.context).inflate(R.layout.calendar_row, parent, false)
                return RowViewHolder(v)
            }

            if(viewType==1){
                val v = LayoutInflater.from(parent.context).inflate(R.layout.calendar_section, parent, false)
                return SectionViewHolder(v)
            }
            return null
        }

        override fun getItemCount(): Int {
                Log.d("All Size",theEvents.size.toString())
                return theEvents.size
        }


        inner class RowViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val textView: TextView

            init {
                textView = itemView.findViewById<View>(R.id.sectionHead) as TextView
            }
        }

        inner class SectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val textView: TextView

            init {
                textView = itemView.findViewById<View>(R.id.dateOfEvent) as TextView
            }
        }

    }

}

我得到的错误:

10-10 09:12:49.239 32612-32612 / com.example.xxxx.listview E / RecyclerView:没有连接适配器;跳过布局

所以我似乎无法将适配器连接到回收站视图。确定还有其他错误,但这是问题一;)

 inner class MyEventAdapter(context: Context,val theEvents:ArrayList<SectionOrRow>):RecyclerView.Adapter<RecyclerView.ViewHolder>(){

        override fun getItemViewType(position: Int): Int {
            Log.d("Current Boolean",theEvents[position].isRow.toString())

            if(theEvents[position].isRow){
                //true is row
                return 1
            }else{
                return 0
            }


        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val item = theEvents.get(position)
            Log.d("Current Item",item.toString())

            if(item.isRow){
                val h = holder as RowViewHolder

                val s = java.text.SimpleDateFormat("EEE
MM/dd")
                val format = s.format(Integer.parseInt(item.eventSD) * 1000L)

                h.textView.setText(format)
                Log.d("Current Title",item.eventTitle.toString())

                h.titleView.setText(item.eventTitle)
                h.datesView.setText(item.eventED)
                h.sponsorView.setText(item.eventSponsor)
                if(item.needTicketing=="Y") {
                    h.ticketView.setText("Ticketing Available")
                }else{
                    h.ticketView.setText("")

                }
            }else{
                val h = holder as SectionViewHolder
                h.textView.setText(item.mysectionName)
            }

        }



        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder? {

            if(viewType==1) {
                val v = LayoutInflater.from(parent.context).inflate(R.layout.calendar_row, parent, false)

                v.setOnClickListener(object: View.OnClickListener {
                    override fun onClick(p0: View?) {
                        Toast.makeText(
                                v.context,
                                "Please Click Set Town to Return to Previous Screen....",
                                Toast.LENGTH_SHORT
                        ).show()

                    }
                })
                return RowViewHolder(v)
            }

            if(viewType==0){
                val v = LayoutInflater.from(parent.context).inflate(R.layout.calendar_section, parent, false)
                return SectionViewHolder(v)
            }
            return null
        }

        override fun getItemCount(): Int {
                Log.d("All Size",theEvents.size.toString())
                return theEvents.size
        }


        inner class RowViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var textView: TextView
            var titleView: TextView
            var datesView: TextView
            var sponsorView: TextView
            var ticketView: TextView

            init {
                textView = itemView.findViewById<View>(R.id.dateOfEvent) as TextView
                titleView = itemView.findViewById<View>(R.id.rowTitle) as TextView
                datesView = itemView.findViewById<View>(R.id.rowDates) as TextView
                sponsorView = itemView.findViewById<View>(R.id.rowSponsor) as TextView
                ticketView = itemView.findViewById<View>(R.id.rowTicketing) as TextView

            }
        }

        inner class SectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var textView: TextView
            init {
                textView = itemView.findViewById<View>(R.id.sectionHead) as TextView
            }
        }



    }
答案

在你的RecyclerViewAdapter中打电话给override fun onViewCreatedFragment课程

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val recyclerView=view?.findViewById(R.id.your_recycler_view) as RecyclerView
    recyclerView.layoutManager=LinearLayoutManager(context, LinearLayout.VERTICAL,false)
    var yourData=ArrayList<YourDataClass>()
    yourData.add(YourDataClass("passYourData"))


    val yourAdapterClassVariableName=YourRecyclerViewAdapterClass(yourData)
    recyclerView.adapter=yourAdapterClassVariableName
}
另一答案

你需要set your adapter in the UI thread。首先初始化您的适配器,然后将数据添加到它并调用adapter.notifyDataSetChanged()。

我还会引导你到kotlin extensions(导入合成属性部分),这样你就不必再调用那个混乱的findViewById调用了。

另一答案

有关此问题的解决方案,请参阅顶部...是将正确的类型放入正确行的问题。

以上是关于我似乎无法将我的Kotlin Recycler多视图代码整理出来的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 kotlin 实现分页

如何将我的 Recycler View 设计为与 Chanel App 的设计相似?

无法在 Kotlin 中构建发布版本

Recycler View Android中的Firestore数据[重复]

Recycler View 和 DialogFragment 苦苦挣扎(Kotlin)

如何在 kotlin 的 Recycler View Adapter 中添加更多数据列表而不清除先前的数据?