【问题标题】:GridView selector overlappingGridView 选择器重叠
【发布时间】:2013-06-20 15:18:30
【问题描述】:

我正在尝试实现卡片样式GridView,例如 Google 音乐应用。

为此,我创建了以下选择器:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        android:state_pressed="true" 
        android:drawable="@drawable/card_grid_bg_pressed">
    </item>
    <item 
        android:state_pressed="false" 
        android:drawable="@drawable/card_grid_bg_normal">
    </item>
</selector>

第一个drawable是,第二个是。

它正在工作,但问题是我无法实现我想要的行为;因为当您按下 Google Music GridView 的项目时,按下的选择器将与正常项目重叠。

这是我想要的截图:

有什么想法吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    为了做这样的事情,我使用了一个简单的可绘制对象,并在我的视图上使用了setForeground 方法。 我使用的 drawable 是 ActionBarSherlock 库的一部分(我会让你找到它 abs__list_focused_holo.9.png)。

    因此,在我的自定义适配器中,我有一个名为 setItemChecked(int position, int boolean, View v) 的方法,它使用 setForeground 来更改视图 v 的 ForeGround。

    请注意,该视图是 FrameLayout,因为据我所知,这些视图或唯一使用此方法的视图。

    public void setItemChecked(int position, boolean checked, View v) {
            if(checked) {
                if(v!=null)
                    ((FrameLayout) v).setForeground(mContext.getResources().getDrawable(R.drawable.abs__list_focused_holo));
                else
                    Log.w(LOGCAT_TAG, "v is null. Can't change foreground");
            }
            else {
                if(v!=null)
                    ((FrameLayout) v).setForeground(mContext.getResources().getDrawable(R.color.transparent));
                else
                    Log.w(LOGCAT_TAG, "v is null. Can't change foreground");
            }
    }
    

    【讨论】:

    • 谢谢,但我喜欢通过我当前的选择器来更改它,而不是通过代码。
    • 那么,您是否使用选择器作为背景?也许您可以将其用作前景,因为背景将位于视图的后面,而前景应位于视图的顶部。
    • 好吧,您的自定义项目视图需要是 FrameLayout,我认为这是唯一可以使用 ForeGround 属性的视图,并为其分配此可绘制对象。但是,我不确定这项工作。如果没有,我会说像这样以编程方式进行可能是这样做的唯一方法!
    猜你喜欢
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多