我似乎无法得到我的Kotlin回收多重查看代码整理出来
所以我知道这是代码升降,但我有问题得到我的RecyclerView在一个片段排序出来。 我读过一大堆教程,每个人似乎都有不同的做事方式。 只是不能把它钉死…所以这里是我的代码:
FRAGMENT XML
数据类
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 lateinit var event2Info: JsonArray val eventList0 = ArrayList() 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() Log.d("Towns error ",error) } is Result.Success -> { val data = result.getAs() val events : Parser = Parser() val eventBuilder: StringBuilder = StringBuilder(data) val eventsjson = events.parse(eventBuilder) as JsonObject Log.d("JOO",eventsjson.toString()) var eventList0 = ArrayList() 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? = 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):RecyclerView.Adapter(){ 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(R.id.sectionHead) as TextView } } inner class SectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val textView: TextView init { textView = itemView.findViewById(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):RecyclerView.Adapter(){ 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\nMM/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(R.id.dateOfEvent) as TextView titleView = itemView.findViewById(R.id.rowTitle) as TextView datesView = itemView.findViewById(R.id.rowDates) as TextView sponsorView = itemView.findViewById(R.id.rowSponsor) as TextView ticketView = itemView.findViewById(R.id.rowTicketing) as TextView } } inner class SectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { var textView: TextView init { textView = itemView.findViewById(R.id.sectionHead) as TextView } } }
调用你的RecyclerViewAdapter
类,在你的Fragment
中的override fun onViewCreated
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() yourData.add(YourDataClass("passYourData")) val yourAdapterClassVariableName=YourRecyclerViewAdapterClass(yourData) recyclerView.adapter=yourAdapterClassVariableName }
您需要在UI线程中设置您的适配器 。 首先初始化适配器,然后将数据添加到它并调用adapter.notifyDataSetChanged()。
我也会指导你kotlin扩展 (导入合成属性部分),所以你不必再调用那个混乱的findViewById调用了。
请参阅顶部解决这个问题…是一个正确的types获取正确的行的问题。
- 如何用RecyclerView实现无尽的列表?
- GridLayout在Kotlin?
- 如何在Android的Activity中使用onItemLongClickListener?
- 只有在RecycleView / List大于屏幕的情况下,才能使用setStackFromEnd
- Espresso,当NestedScrollView或RecyclerView在CoordinatorLayout中时,滚动不起作用
- 第一次尝试刷新的SwipeRefreshLayout动画无休止地运行
- 在适配器中不能识别Kotlin扩展
- Kotlin抽象通用视图持有者的一类types的争论
- 在Android Studio中转换后,Kotlintypes不匹配