Closed6

ListAdapter を ViewBinding で書く

tkttkt
package com.example.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.example.model.Post
import com.example.databinding.PostListHeaderBinding
import com.example.databinding.PostListRowBinding


class PostListAdapter(
    private val context: Context,
    private val postList: List<Post>,
) : BaseAdapter() {

    override fun getCount(): Int {
        return postList.size
    }

    override fun getItem(position: Int): Post {
        return postList[position]
    }

    override fun getItemId(position: Int): Long {
        return postList[position].timestampMs.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val inflater = LayoutInflater.from(context)
        if (position == 0) {
            return PostListHeaderBinding.inflate(inflater, parent, false).root
        }
        val binding =
            if (convertView == null) {
                val tBinding: PostListRowBinding =
                    PostListRowBinding.inflate(inflater, parent, false)
                tBinding.root.tag = tBinding
                tBinding
            } else {
                convertView.tag as PostListRowBinding
            }
        binding.messageText.text =
            postList[position].message
        binding.datetimeText.text = postList[position].timestampMs.toString()
        binding.kindText.text = postList[position].kind.toString()

        return binding.root
    }
}
tkttkt
if (position == 0) {
    return PostListHeaderBinding.inflate(inflater, parent, false).root
}

ここが原因みたい

tkttkt
package com.example.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.example.model.Post
import com.example.databinding.PostListHeaderBinding
import com.example.databinding.PostListRowBinding


class PostListAdapter(
    private val context: Context,
    private val postList: List<Post>,
) : BaseAdapter() {

    override fun getCount(): Int {
        return postList.size
    }

    override fun getItem(position: Int): Post {
        return postList[position]
    }

    override fun getItemId(position: Int): Long {
        return postList[position].timestampMs.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val inflater = LayoutInflater.from(context)
        // 一行目はヘッダー
        if (position == 0) {
                val tBinding: PostListHeaderBinding = PostListHeaderBinding.inflate(inflater, parent, false)
                tBinding.root.tag = tBinding
                return tBinding.root
        }
        val binding =
            // null か header か row が入るはず
            if (convertView?.javaClass != PostListRowBinding::javaClass) {
                val tBinding: PostListRowBinding =
                    PostListRowBinding.inflate(inflater, parent, false)
                tBinding.root.tag = tBinding
                tBinding
            } else {
                convertView.tag as PostListRowBinding
            }
        binding.messageText.text =
            postList[position].message
        binding.datetimeText.text = postList[position].timestampMs.toString()
        binding.kindText.text = postList[position].kind.toString()

        return binding.root
    }
}
tkttkt
private val postList: List<PostViewModel>

binding.postViewModel = getItem(position)

これで vViewModel も使える

このスクラップは2021/04/23にクローズされました