, поэтому я пытаюсь создать PagerView через фрагменты, содержащие несколько BaseAdapter, но у меня возникла небольшая проблема с BaseAdapter, у меня есть несколько фрагментов, но я опубликую 1 в качестве примера
class SpellManagement : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
listViewSpells?.adapter = AllSpells()
return inflater.inflate(R.layout.activity_spell, container, false)
}
}
class AllSpells: BaseAdapter() {
override fun getCount(): Int {
return player.learnedSpells.size/5+1
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItem(position: Int): Any {
return "TEST STRING"
}
override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View {
val rowMain: View
val index:Int = if(position == 0) 0 else{
position*5
}
if (convertView == null) {
val layoutInflater = LayoutInflater.from(viewGroup!!.context)
rowMain = layoutInflater.inflate(R.layout.row_spells_managment, viewGroup, false)
val viewHolder = ViewHolder(rowMain.buttonSpellsManagment1,rowMain.buttonSpellsManagment2,rowMain.buttonSpellsManagment3,rowMain.buttonSpellsManagment4)
rowMain.tag = viewHolder
} else rowMain = convertView
val viewHolder = rowMain.tag as ViewHolder
viewHolder.buttonSpellsManagement1.setBackgroundResource(player.learnedSpells[index]!!.drawable)
viewHolder.buttonSpellsManagement2.setBackgroundResource(player.learnedSpells[index+1]!!.drawable)
viewHolder.buttonSpellsManagement3.setBackgroundResource(player.learnedSpells[index+2]!!.drawable)
viewHolder.buttonSpellsManagement4.setBackgroundResource(player.learnedSpells[index+3]!!.drawable)
viewHolder.buttonSpellsManagement1.setOnClickListener {
}
viewHolder.buttonSpellsManagement2.setOnClickListener {
}
viewHolder.buttonSpellsManagement3.setOnClickListener {
}
viewHolder.buttonSpellsManagement4.setOnClickListener {
}
return rowMain
}
private class ViewHolder(val buttonSpellsManagement1: Button, val buttonSpellsManagement2: Button, val buttonSpellsManagement3: Button, val buttonSpellsManagement4: Button)
}
Первоначально я использовал действия, но в случае PagerView я изменил его на Fragment.Что я знаю, так это то, что я не могу назвать this
и т. Д., Что я нашел в java-кодах, в некоторых случаях было getActivity () вместо this
, которое я не смог найти в Kotlin.Итак, говоря, что это может быть в viewGroup!!.context
в BaseAdapter.Любая помощь будет оценена, спасибо.
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:screenOrientation="landscape">
<android.support.constraint.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.8" />
<android.support.constraint.Guideline
android:id="@+id/guideline30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:orientation="horizontal"
app:layout_constraintEnd_toStartOf="@+id/listViewSpells"
app:layout_constraintGuide_percent="0.4"
app:layout_constraintStart_toStartOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="87dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@+id/guideline7"
app:layout_constraintGuide_percent="0.1925"
app:layout_constraintStart_toEndOf="@+id/guideline11" />
<android.support.constraint.Guideline
android:id="@+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginEnd="83dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@+id/guideline10"
app:layout_constraintGuide_percent="0.05"
app:layout_constraintStart_toStartOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="86dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@+id/guideline14"
app:layout_constraintGuide_percent="0.4825"
app:layout_constraintStart_toEndOf="@+id/guideline7" />
<android.support.constraint.Guideline
android:id="@+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="7dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@+id/imageView3"
app:layout_constraintGuide_percent="0.63"
app:layout_constraintStart_toEndOf="@+id/guideline13" />
<android.support.constraint.Guideline
android:id="@+id/guideline15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.774" />
<android.support.constraint.Guideline
android:id="@+id/guideline16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.92" />
<android.support.constraint.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="83dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@+id/guideline13"
app:layout_constraintGuide_percent="0.34"
app:layout_constraintStart_toEndOf="@+id/guideline10" />
<ListView
android:id="@+id/listViewSpells"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline14"
app:layout_constraintStart_toEndOf="@+id/guideline30"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewInfoSpell"
android:layout_width="0dp"
android:layout_height="141dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline9"
app:layout_constraintDimensionRatio="H,1:1.25"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline14"
app:layout_constraintTop_toTopOf="@+id/guideline30" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="75dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline30"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline14"
app:layout_constraintTop_toBottomOf="@+id/imageView3" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="0dp"
android:layout_height="47dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="7dp"
app:layout_constraintDimensionRatio="H,"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/guideline14"
app:layout_constraintStart_toStartOf="@+id/guideline14"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>