【问题标题】:Change image alpha from listview position and save从列表视图位置更改图像 alpha 并保存
【发布时间】:2020-09-19 04:49:03
【问题描述】:

我正在制作音乐列表视图。我有一个基本适配器,音乐类音乐类有 3 个属性;名称:字符串 点:字符串 图片:诠释 如何更改所选项目的图像 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


    【解决方案1】:

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

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

    希望对你有帮助

    【讨论】:

    • 我想更改选择的项目。你的方法改变了每个项目
    【解决方案2】:

    在适配器 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);
        }
    

    【讨论】:

      猜你喜欢
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 2017-04-17
      • 2015-11-03
      • 1970-01-01
      • 2016-04-03
      • 2021-11-09
      • 1970-01-01
      相关资源
      最近更新 更多