So I am working with AndroidStudio and writing my code in Kotlin. I have a Fragment with a TabLayout and ViewPager that has 5 Tabs. Each tab contains another Fragment with a ListView and a ToggleButton. The ListViews all contain 10 items. Here is a screenshot, I'm not allowed to use embed images yet
The problem is that the Fragment doesn't open smoothly, especially when opened first.
I think the problem lies within the implementation of the ListViewAdapter for the ListViews in the 5 Tabs, especially the getView function. Here is the code for them:
inner class TimetableListAdapter(
private val _context : Context,
private var fulldata : TimetableUtility.TimetableDay,
var bWeek : Boolean,
private var dayData : ArrayList<TimetableUtility.TimetableSubject> = fulldata[0],
private val _resource : Int = R.layout.timetable_subject_element
) : ArrayAdapter<TimetableUtility.TimetableSubject>(_context, _resource, dayData){
init {
dayData = if(bWeek){
fulldata[1]
}else{
fulldata[0]
}
Log.d("Timetable" , "Adapter init ")
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
Log.d("Timetable" , "getView: $bWeek")
val s : TimetableUtility.TimetableSubject = if(bWeek){
dayData = fulldata[1]
fulldata[1][position]
}else{
dayData = fulldata[0]
fulldata[0][position]
}
val v =
convertView
?: LayoutInflater.from(_context).inflate(
_resource,
parent,
false
)
v.editIcon.setOnClickListener {
openEditDialog(this@TimetableDayFragment.context!!,s)
}
v.timetable_hour_number.text = (position + 1).toString()
v.timetable_subject_title.text = s.name
v.timetable_subject_room.text = s.room
return v
}
}
I know that the implementation of a ViewHolder pattern would reduce the count of view searching calls, but I think that this wouldn't affect the initial generation.
I played a little bit with the Profiler in AndroidStudio and found out, that the getView method of my adapter takes about 20-30ms when running the application on a virtual machine on my pc. When running the application on my Samsung device (Galaxy A6, not that old) it more, as the delay is bigger. I didn't use the profiler with it yet.
Is my assumption, that the lag results from the getView method right and if yes, do you have any suggestions or ideas how to improve that?
If that will help, here is the code of the adapter for the ViewPager:
inner class TimetableFragmentAdapter(
context : Context,
fm : FragmentManager,
private val totalTabs : Int
) : FragmentPagerAdapter(fm,totalTabs){
private val dayTitles = arrayOfNulls<String>(5)
private var sdf =
SimpleDateFormat("EEE", Locale.getDefault())
private var calendar = Calendar.getInstance()!!
init{
for (i in 0..4) {
calendar.set(Calendar.DAY_OF_WEEK, i + 2)
dayTitles[i] = sdf.format(calendar.time)
}
}
override fun getCount(): Int {
return totalTabs
}
override fun getPageTitle(position: Int): CharSequence? {
return dayTitles[position]
}
@RequiresApi(Build.VERSION_CODES.O)
override fun getItem(position: Int): Fragment {
when(position){
0 -> return TimetableDayFragment(DayOfWeek.MONDAY, this@TimetableFragment)
1 -> return TimetableDayFragment(DayOfWeek.TUESDAY, this@TimetableFragment)
2 -> return TimetableDayFragment(DayOfWeek.WEDNESDAY, this@TimetableFragment)
3 -> return TimetableDayFragment(DayOfWeek.THURSDAY, this@TimetableFragment)
4 -> return TimetableDayFragment(DayOfWeek.FRIDAY, this@TimetableFragment)
}
return TimetableDayFragment(DayOfWeek.MONDAY, this@TimetableFragment)
}
and the on create code of the inner fragments, that are used each of the tabs
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.timetable_inner_fragment,container,false)
adapter = TimetableListAdapter(context!!, TimetableUtility.timetable.get(day), root.weekSwitch.isChecked)
root.timetable_list.adapter = adapter
root.weekSwitch.setOnCheckedChangeListener{
it, selected ->
(root.timetable_list.adapter as TimetableListAdapter).bWeek = selected
(root.timetable_list.adapter as TimetableListAdapter).notifyDataSetChanged()
}
return root
}
Thanks a lot for your help and ideas in advance. If you need any more code or information, please just let me know.