繁体   English   中英

如何获取不在ListView可见部分中的适配器类内部的视图? -科特林

[英]How get view inside adapter class that is not in visible part of ListView? - Kotlin

我有带铃声的listView

列表显示

每当音乐开始或stop时,我都需要更改play_arrow图像以stop图像。 当我单击一个图像上的播放时,它将变为stop图像,然后单击其他音乐项目图像上的播放,因此前一个图像play_arrow而单击后的图像play_arrow stop 问题在于按位置获取视图。 在前12个音乐视图中,一切都很好。 并且,如果我尝试使用previousListened > 12来获取类似于parent.getChildAt(previousListened)视图,则返回null。

编辑:添加适配器类

class SoundListAdapter constructor(
    private var context: Context,
    private val layout: Int,
    private var arrayList: ArrayList<Sound>,
    private val activity: FragmentActivity,
    private val mediaPlayer: MediaPlayer
) : BaseAdapter() {

    private var selectedPosition = -1
    private var currentListened = -1
    private var previousListened = -1
    private var isPlaying = false
    private var TAG = "SoundListAdapter"
    private val mSendSoundUri: SendSoundUri = activity as SendSoundUri

    interface SendSoundUri {
        fun sendSoundUri(input: String?)
    }

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

    override fun getItem(i: Int): Any {
        return ""
    }

    override fun getItemId(i: Int): Long {
        return i.toLong()
    }

    private inner class ViewHolder {
        internal var radioButton: RadioButton? = null
        internal var txtName: TextView? = null
        internal var ivPlay: ImageView? = null
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        val viewHolder: ViewHolder
        if (convertView == null) {
            viewHolder = ViewHolder()
            val layoutInflater = context.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater

            convertView = layoutInflater.inflate(layout, null)
            viewHolder.txtName = convertView!!.findViewById(R.id.sound_name)
            viewHolder.radioButton = convertView.findViewById(R.id.sound_radiobutton)
            viewHolder.ivPlay = convertView.findViewById(R.id.ivPlay) as ImageView

            convertView.tag = viewHolder
        } else {
            viewHolder = convertView.tag as ViewHolder
        }

        //check the radio button if both position and selectedPosition matches
        viewHolder.radioButton?.isChecked = (position === selectedPosition)
        // TODO add color to checked circle
        //Set the position tag to both radio button and label
        viewHolder.radioButton?.tag = position
        viewHolder.ivPlay?.tag = position
        viewHolder.txtName?.tag = position

        viewHolder.radioButton?.setOnClickListener { v -> itemCheckChanged(v) }

        viewHolder.txtName?.setOnClickListener { v -> itemCheckChanged(v) }

        val music = arrayList[position]

        viewHolder.txtName!!.text = music.title

        // play music
        viewHolder.ivPlay!!.setOnClickListener {
            previousListened = currentListened
            currentListened = it.tag as Int
            // TODO add black square when playing
            Log.d(TAG, "max items: ${parent.childCount}")
            Log.d(TAG, "previousListened: $previousListened")
            if (previousListened != -1 && previousListened == currentListened && mediaPlayer.isPlaying) {
                mediaPlayer.stop()
            } else {
                mediaPlayer.reset()
                mediaPlayer.setDataSource(context, Uri.parse(music.uri))
                mediaPlayer.prepare()
                mediaPlayer.start()
            }

        }

        return convertView
    }

    private fun getSelectedSound(): Sound? {
        Log.d(TAG, "sending selectedPosition: $selectedPosition")
        if (selectedPosition == -1)
            return null
        return arrayList[selectedPosition]
    }

    private fun itemCheckChanged(v: View) {
        selectedPosition = v.tag as Int
        mSendSoundUri.sendSoundUri(getSelectedSound()?.uri)
        Log.d(TAG, "selectedPosition changed to: $selectedPosition")
        notifyDataSetChanged()
    }
}

是否可以获得Adapter类中可见部分之外的ListView项目视图?

是否可以获得Adapter类中可见部分之外的ListView的项目视图?

不,你不能。 ViewHolder仅适用于可见项目。

但是,根据您的情况,您只需要在getView函数中设置ImageView图像。

    if (currentListened == position) {
       // set here your Stop image 
        viewHolder.ivPlay.setImageResource(R.drawable.stop);
    } else {
       // set here your Play image
        viewHolder.ivPlay.setImageResource(R.drawable.play);
    }

然后,调用notifyDataSetChanged

    viewHolder.ivPlay!!.setOnClickListener {
        ...
        ...
        notifyDataSetChanged();
    }

notifyDataSetChange将更新所有可见项。

另一方面,您无需将position保存在tag变量中。 您始终知道单击了哪个项目,因为在getView函数中设置了onClick事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM