问题描述
我的GridView适配器,只需在gridView项中添加一张图片
class GridPhotoAdapter(context: Context) : BaseAdapter() {
private var imageArrayList = arrayOf(
com.example.b_my_friend.R.mipmap.ic_launcher)
private lateinit var inflater: LayoutInflater
private val context: Context = context
override fun getCount(): Int {
return imageArrayList.size
}
override fun getItem(position: Int): Any {
return imageArrayList[position]
}
override fun getItemId(position: Int): Long {
return imageArrayList[position].toLong()
}
override fun getView(position: Int,convertView: View,parent: ViewGroup?): View {
var view: View = convertView
if (inflater == null){
inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
}
if (view == null) {
view = inflater.inflate(com.example.b_my_friend.R.layout.item_grid_photo,null)
}
view.photo.setimageResource(imageArrayList[position])
view
}
}
当我在MainFragment中初始化
val gridPhotoAdapter = GridPhotoAdapter(activity!!)
gridPhoto.adapter = gridPhotoAdapter
我发现错误,我不知道为什么
gridPhoto不能为空
fragment_photo.xml与我的GridView(gridPhoto)
<LinearLayout
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"
tools:context=".ui.page.PhotoFragment">
<GridView
android:id="@+id/gridPhoto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="30dp"
android:numColumns="3"
android:stretchMode="columnWidth"/>
</LinearLayout>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)