【问题标题】:Creating new Object from resource and modifing it changes resource从资源创建新对象并修改它会更改资源
【发布时间】:2013-06-06 06:37:35
【问题描述】:

我有以下问题。

我有两个片段,每个片段都有一个带适配器的 ListView。通过从第一个列表中选择元素,我将包含第一个列表的片段替换为包含第二个列表的片段。我想让每行背景只在第二个 Fragment 中透明,但是在第二个适配器的 getView 中使用了以下代码,看起来它更新了资源?谁能解释为什么?

代码:

第一个片段:

public View getView(int position, View convertView, ViewGroup parent) {
(...)
    v.setBackgroundResource(R.drawable.abstract_gray);
(...)
}

第二个片段:

public View getView(int position, View convertView, ViewGroup parent) {
(...)
    Drawable backgroundDrawable = context.getResources().getDrawable(R.drawable.abstract_gray);
    backgroundDrawable.setAlpha(ToolsAndConstants.BACKGROUND_TRANSPARENCY);
    v.setBackgroundDrawable(backgroundDrawable);
    // I call this to check if when I call the drawable from resurce it will not be transparent
    v.setBackgroundResource(R.drawable.abstract_gray);
    // But it is...So previous fragment background, when I go back with back Button
(...)
}

也许这个问题真的很基础,但是当我从资源创建新对象时,我实际上是在使用资源本身,对新对象所做的所有更改都会影响整个应用程序,即使其他类无法访问该对象? ?

编辑:

对不起,我刚刚意识到我没有创建新的 Drawable,我只是做一个参考。那我怎样才能创建一个新的呢?如果我不能,我怎样才能改变只有第二个列表的背景?

【问题讨论】:

    标签: android resources drawable transparent alpha


    【解决方案1】:

    Marek 看了这篇文章,它解释了你的疑惑http://android-developers.blogspot.com/2009/05/drawable-mutations.html

    【讨论】:

    • 我得到的最具针对性的答案。谢谢!
    • 我还有一个问题。当我滚动列表时,有时项目会突出显示。我想避免它。我应该使用哪个监听器?
    • “项目被突出显示”是什么意思?
    • 好吧,当我用手指点击它,然后将手指从中抽出时(onClick 不会触发),它首先是:用 HOLO_DARK 蓝色突出显示,然后当我抬起手指时,突出显示消失,但不透明度为 255...
    • 我的意思是当我按下它时我会强制列表不突出显示。我只需要知道当您按下列表元素时调用哪个监听器(但不是单击,因为我将手指移开)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2022-07-24
    • 2019-07-15
    • 1970-01-01
    相关资源
    最近更新 更多