###FragmentにListViewを表示した理由
iOS先行でAndroidでもデザインを模倣したくて、
BottomNavigationView
を採用しました。
その際に不慣れなFragment
にListView
を設定してハマったので備忘録として残しておきます。
NotificationFragment.kt
package com.android.yoichi.ui.notification
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.android.yoichi.CustomAdapter
import com.android.yoichi.R
import com.android.yoichi.model.notificationModel
import kotlinx.android.synthetic.main.fragment_notification.*
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [NotificationFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class NotificationFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
lateinit var mCustomAdapter: CustomAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_notification, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val noticationList = listOf(
notificationModel("▼サーバーメンテナンス","明日からメンテナンスを行います"),
notificationModel("▼サーバーメンテナンス","メンテナンス完了しました")
)
mCustomAdapter = CustomAdapter(this.context, noticationList)
ListView.adapter = mCustomAdapter
super.onViewCreated(view, savedInstanceState)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment NotificationFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
NotificationFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
こちらに処理を記載しなかった事で表示にアプリがクラッシュしていました。
おまけ:CustomAdapter
package com.android.yoichi
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import com.android.yoichi.model.notificationModel
class CustomAdapter(context: Context?, var notificationList: List<notificationModel>) : ArrayAdapter<notificationModel>(
context!!, 0, notificationList) {
private val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
// レイアウトの設定
var view = convertView
if (convertView == null) {
view = layoutInflater.inflate(R.layout.notification_item, parent, false)
}
// 各Viewの設定
val notificationTitle = view?.findViewById<TextView>(R.id.notificationTitle)
notificationTitle?.text = "title"
val notificationContent = view?.findViewById<TextView>(R.id.notificationContent)
notificationContent?.text = "Content"
return view!!
}
}