У меня есть список класса с именем Opportunity, который заполнен объектами, расширяющими Opportunity. Список может иметь либо CommunityOpportunites, либо SponsorshipOpportunities.
Я переопределяю getItemViewType и присваиваю значение каждому элементу в зависимости от того, какой подкласс находится в соответствующем посте, и для каждого из них создается отдельный ViewHolder:
override fun getItemViewType(position: Int): Int {
return if (opportunityList[position] is SponsorshipOpportunity){
Log.i(TAG,"Item type is sponsorshipId")
SPONSORSHIP
} else{
Log.i(TAG,"Item type is community")
COMMUNITY
}
}
inner class CommunityViewHolder(var view: CommunityTileBinding):RecyclerView.ViewHolder(view.root)
inner class SponsorshipViewHolder(var view: SponsorshipTileBinding):RecyclerView.ViewHolder(view.root)
companion object{
private const val COMMUNITY = 0
private const val SPONSORSHIP = 1
}
В onCreateViewHolder () я создаю правильный ViewHolder для класса элемента, а в onBindViewHolder () я пытаюсь привести элементы в списке (типа Opportunity в конструкторе) к подклассу элемента в представлении :
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return when (viewType){
COMMUNITY->CommunityViewHolder(DataBindingUtil.inflate(inflater, R.layout.community_tile, parent, false))
SPONSORSHIP-> SponsorshipViewHolder(DataBindingUtil.inflate(inflater, R.layout.sponsorship_tile, parent, false))
else-> throw IllegalArgumentException()
}
}
override fun getItemCount(): Int = opportunityList.size
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when(holder.itemViewType){
COMMUNITY-> {
(holder as CommunityViewHolder).view.communityOpportunity = opportunityList[position] as CommunityOpportunity
}
SPONSORSHIP->{
(holder as SponsorshipViewHolder).view.sponsorship = opportunityList[position] as SponsorshipOpportunity
holder.view.postActionText.text = context.resources.getString(R.string.watch_respond)
}
}
}
Тем не менее, я получаю следующее исключение приведения класса
java.lang.ClassCastException: com.example.model.Opportunity cannot be cast to com.example.model.CommunityOpportunity
, когда я пытаюсь выполнить соответствующую строку в onBindViewHolder, даже если оператор журнала подтверждает, что элемент является CommunityOpportunity в getItemViewType () печатается.
Есть ли лучший способ задать вопрос или есть лучший способ для меня отображать несколько типов ViewHolder / Object в RecyclerView?
Редактировать: Здесь соответствующие xml макеты:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="sponsorship"
type="com.example.weare8sample.model.SponsorshipOpportunity"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:elevation="2dp"
android:orientation="vertical"
android:background="@drawable/tile_background">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="@drawable/tile_background"
android:imageUrl="@{sponsorship.coverTileUri}">
</ImageView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:padding="10dp">
<TextView
android:id="@+id/postActionText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato_bold"
tools:text="@string/watch_respond">
</TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_chevron_right_black_36dp">
</ImageView>
</RelativeLayout>
</LinearLayout>
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="communityOpportunity"
type="com.example.weare8sample.model.CommunityOpportunity" />
</data>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/tile_background"
android:imageUrl="@{communityOpportunity.mediaImageUri}">
</ImageView>