从列表视图位置更改图像Alpha并保存

问题描述 投票:0回答:2

我正在制作音乐列表视图。我有一个基本适配器,音乐课音乐课具有3个属性;名称:字符串点:字符串图片:Int如何更改图像Alpha选中了哪个项目?

        listMusics.setOnItemClickListener { parent, view, position, id ->
        val preference = getSharedPreferences("PREFS",0)
        val editor = preference.edit()
        best1 = preference.getInt("best1", 0)
        if(best1>=position*100) {
            music = position
            view.imgMusic.alpha=1F
            editor.putInt("music", music)
            editor.apply()
        }

    }

我正在使用view.imgMusic.alpha=1F更改图像Alpha,但是将图像更改为Alpha,但我无法保存项目Alpha。我想将alpha更改为1F,选择并保存哪个项目,还将其他项目更改为0.5F,我该怎么做?

android android-studio listview kotlin baseadapter
2个回答
0
投票

我认为如果您使用ImageView显示图像,则可以使用此属性:

<ImageView
    android:layout_width="30dp"
    android:layout_height="35dp"
    android:id="@+id/imageView"
    android:alpha="0.4" // <-----------------  this
    android:background="@drawable/imagename"
/> 

希望对您有帮助


0
投票

在适配器onBindViewHolder中,您可以使用此代码

 holder.container.setOnClickListener(v -> {

        lastSelectedSong = position;

        //reset the color of all items and change color of new selected item
        notifyDataSetChanged();
    });

    //change icon and color of last Selected Song
    if (position == lastSelectedSong) {
        holder.container.setBackgroundColor(your_color);

           } else { //default color and icon of items
        holder.imageView.setBackgroundColor(Color.TRANSPARENT);
    }
© www.soinside.com 2019 - 2024. All rights reserved.