GridView项目列表选择器,用于在Android中无法使用的多个项目

int*_*thi 14 android android-layout android-gridview android-selector

我想在长按时画出选择器,如图所示.当我长按一个项目时,CAB菜单被激活.但是单击后列表选择器指示灯会熄灭一次.我希望列表选择器处于活动状态,直到CAB菜单处于活动状态以允许多个选择.如果我双击,颜色应切换.当我点击它时,这段代码就像闪烁一样.任何人都面临类似的事情?是否有一个hack来实现这个功能?

Gridview有多个选择:

在此输入图像描述

我的OnCreate中的GridView setchoice:

gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
gridView.setMultiChoiceModeListener(new MultiChoiceModeListener());
Run Code Online (Sandbox Code Playgroud)

这很好用:

gridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {

    ImageView iv = (ImageView) v
                        .findViewById(R.id.imageview2);
                ViewGroup gridChild = (ViewGroup) gridView
                        .getChildAt(position);
                ViewGroup gridGrandChild = (ViewGroup) gridChild
                        .getChildAt(0);
                ViewGroup gridGreatGrandChild = (ViewGroup) gridGrandChild
                        .getChildAt(0);
                int childSize = gridGreatGrandChild.getChildCount();
                if (iv.getId() == gridGreatGrandChild.getChildAt(1)
                        .getId()) {
                    if (iv.getVisibility() == 4)
                        iv.setVisibility(View.VISIBLE);
                    else
                        iv.setVisibility(View.INVISIBLE);
                }
}});
Run Code Online (Sandbox Code Playgroud)

我试图在激活CAB菜单后在MultiChoiceModeListener()类中实现相同的代码.在里面复制相同的代码

public void onItemCheckedStateChanged(ActionMode mode, int position,
            long id, boolean checked) {
}
Run Code Online (Sandbox Code Playgroud)

方法.但我无法访问视图组内的元素.但我能够完整地访问GridView.但不是Grid中的个别孩子.

这是我的gridview布局:

    <GridView
            android:id="@+id/grid_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:columnWidth="190dp"
            android:drawSelectorOnTop="true"
            android:choiceMode="multipleChoice"
            android:horizontalSpacing="3dp"
            android:listSelector="@drawable/list_selector"
            android:numColumns="auto_fit"
            android:paddingRight="4dp"
            android:stretchMode="spacingWidthUniform"
            android:verticalSpacing="3dp" >
        </GridView>
Run Code Online (Sandbox Code Playgroud)

GridView中的每个项都使用以下布局构建:

<FrameLayout
    android:id="@+id/frameLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/relativelayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ImageView
            android:id="@+id/imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ImageView>

        <ImageView
            android:id="@+id/imageview2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageview"
            android:layout_alignLeft="@+id/imageview"
            android:layout_alignRight="@+id/imageview"
            android:layout_alignTop="@+id/imageview"   
            android:visibility="invisible"
             android:background="#76f9a49c"            
             >
        </ImageView>
    </RelativeLayout>

    <ImageView
        android:id="@+id/tagimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|right"
        android:layout_marginRight="-4dp"
        android:src="@drawable/tag"
        android:visibility="invisible" />

    <ImageView
        android:id="@+id/countimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="5dp"
        android:src="@drawable/dealcount_overlay"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="11dp"
        android:layout_marginRight="24dp"
        android:text="100"
        android:textColor="#fff"
        android:visibility="invisible" >
    </TextView>

    <ProgressBar
        android:id="@+id/progressbar"
        style="@android:style/Widget.ProgressBar.Small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:visibility="visible" />
</FrameLayout>

<TextView
    android:id="@+id/textView_nodata"
    android:layout_width="wrap_content"
    android:layout_height="150dp"
    android:layout_below="@+id/frameLayout1"
    android:layout_centerHorizontal="true"
    android:text="No data available. Try different query!"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:visibility="gone" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是我的list_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@color/gridviewlistselector" android:state_focused="true" android:state_pressed="false"/>
<item android:state_focused="true"><shape>
        <solid android:color="#66000000" />
    </shape></item>
<item android:state_selected="true"><shape>
        <solid android:color="#66000000" />
    </shape></item>
<item android:state_pressed="true"><shape>
        <solid android:color="#66000000" />
    </shape></item>
<item android:state_enabled="false" android:state_focused="true"><shape>
        <solid android:color="#66000000" />
    </shape></item>

</selector>
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现这个功能?我对此感到困惑.我在选择器上尝试了各种组合.但似乎没有什么对我有用.我需要提供任何其他细节吗?

dok*_*ebi 1

我不确定这是正确的解决方案,但我将其设置为android:background我的列表项:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
        android:exitFadeDuration="@android:integer/config_mediumAnimTime" >

    <!-- I never see this one - the grid items are not focusable -->
    <item android:state_pressed="false" android:state_focused="true" android:drawable="@drawable/list_focused" />

    <!-- while pressed -->
    <item android:state_pressed="true" android:drawable="@drawable/pressed_background" />

    <!-- while selected in actionmode -->
    <item android:state_activated="true" android:drawable="@color/pressed" />

    <item android:drawable="@android:color/transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)

我没有android:listSelector在网格视图上设置属性。