BaseAdapter 不能通过 PagerView 在 Fragment 中工作
BaseAdapter doesn't work in Fragment via PagerView
所以我试图通过包含多个 BaseAdapter 的 Fragment 制作一个 PagerView,但是我 运行 遇到了 BaseAdapter 的一个小问题,我有多个 Fragment,但我会 post 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)
}
我最初使用的是 Activity,但对于 PagerView,我已将其更改为 Fragment。我所知道的是,我不能调用 this
等。我在 java 代码中发现的在某些情况下是 getActivity() 而不是 this
,我在科特林。所以说它可能在 BaseAdapter 的 viewGroup!!.context
中。
任何帮助将不胜感激,谢谢。
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>
我认为您需要在声明视图后进行适配器工作。在 activity 中,调用 onCreate 方法后,我们可以获取其他作品的视图。它的片段略有不同。给你的视图充气后我们就可以看到了。
首先,创建一个视图作为 onCreateView
函数的第一行。
val view = inflater!!.inflate(R.layout.activity_spells, container, false)
其次,return 从您的 onCreateView
功能来看。
最后,您可以在这些之间放置(使用)您的其他作品。这样,您就可以将 viwe(您的片段)用作变量 view
。您的 SpellManagement
class 将如下所示:
class SpellManagement : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.R.layout.activity_spells, container, false)
\**
Your other codes or works here
*\
listViewSpells?.adapter = AllSpells()
return view
}
}
我认为这可能会有所帮助。加油!
所以...这对我来说真的很奇怪,答案是用现在不太常用的旧 "view.FindViewById(...)" 东西创建一个新的 ListView,因为插件,我以前从未使用过它,这允许您直接写下组件的名称,例如 "listView.adapter = ..." 而不是通过 "view.findViewById(...)" thingie 外部定义它。 与 MinnKhant 的组合。最终代码如下所示:
class SpellManagement : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.activity_spell_managment, container, false)
val listView:ListView = view.findViewById(R.id.listViewSpells)
listView.adapter = AllSpells()
return view
}
}
干杯
编辑:或者只写 view.listViewSpells.adapter = AllSpells(),而不是 findViewById(...)
所以我试图通过包含多个 BaseAdapter 的 Fragment 制作一个 PagerView,但是我 运行 遇到了 BaseAdapter 的一个小问题,我有多个 Fragment,但我会 post 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)
}
我最初使用的是 Activity,但对于 PagerView,我已将其更改为 Fragment。我所知道的是,我不能调用 this
等。我在 java 代码中发现的在某些情况下是 getActivity() 而不是 this
,我在科特林。所以说它可能在 BaseAdapter 的 viewGroup!!.context
中。
任何帮助将不胜感激,谢谢。
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>
我认为您需要在声明视图后进行适配器工作。在 activity 中,调用 onCreate 方法后,我们可以获取其他作品的视图。它的片段略有不同。给你的视图充气后我们就可以看到了。
首先,创建一个视图作为 onCreateView
函数的第一行。
val view = inflater!!.inflate(R.layout.activity_spells, container, false)
其次,return 从您的 onCreateView
功能来看。
最后,您可以在这些之间放置(使用)您的其他作品。这样,您就可以将 viwe(您的片段)用作变量 view
。您的 SpellManagement
class 将如下所示:
class SpellManagement : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.R.layout.activity_spells, container, false)
\**
Your other codes or works here
*\
listViewSpells?.adapter = AllSpells()
return view
}
}
我认为这可能会有所帮助。加油!
所以...这对我来说真的很奇怪,答案是用现在不太常用的旧 "view.FindViewById(...)" 东西创建一个新的 ListView,因为插件,我以前从未使用过它,这允许您直接写下组件的名称,例如 "listView.adapter = ..." 而不是通过 "view.findViewById(...)" thingie 外部定义它。
class SpellManagement : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.activity_spell_managment, container, false)
val listView:ListView = view.findViewById(R.id.listViewSpells)
listView.adapter = AllSpells()
return view
}
}
干杯
编辑:或者只写 view.listViewSpells.adapter = AllSpells(),而不是 findViewById(...)