2

当我处于操作模式时,如何在网格视图中选择多个元素?

我已经实现了这种方式,但没有功能:

片段布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical"
    android:theme="@style/AppTheme">

    <GridView
        android:id="@+id/gridview1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="0dp"


        android:choiceMode="multipleChoiceModal"
        android:listSelector="@drawable/selected"
        android:drawSelectorOnTop="true"

        android:gravity="fill"
        android:horizontalSpacing="2dp"
        android:numColumns="3"
        android:paddingTop="5dp"
        android:stackFromBottom="true"
        android:stretchMode="columnWidth"
        android:verticalSpacing="2dp"
        android:visibility="visible"/>
</LinearLayout>

状态列表选择器 (selected.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- un click-->
    <item android:drawable="@color/pressed" android:state_pressed="true"/>
    <item android:drawable="@color/selected" android:state_selected="true"/>
    <!--checkatto-->
    <item android:drawable="@color/checkable" android:state_checkable="true"/>
    <!-- while selected in actionmode -->
    <item android:state_activated="true" android:drawable="@color/activate" />
    <item android:drawable="@color/standard"/>
</selector>

适配器(创建动态视图,如自定义 imageview()

public View getView(int position, View convertView, ViewGroup parent)
{
        CheckableImageView checkableImageView; 
        if (convertView == null) 
        {
            checkableImageView = new CheckableImageView(mContext);                
           checkableImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            checkableImageView.setMinimumHeight(300);
        } else {
            checkableImageView = (CheckableImageView) convertView;
        }

        Bitmap bitmap = cachePhoto.get((long) position);
        if (bitmap != null) {
            checkableImageView.setImageBitmap(cachePhoto.get((long) 
            position));

        } else {


            try {                   
//task async            
CaricaImmagine(listMedia.get(position),checkableImageView,position);

                } catch (Exception e)
                {}
            }

imageview 可检查状态的子类

public class CheckableImageView extends android.support.v7.widget.AppCompatImageView implements Checkable {
    private boolean mChecked;

    private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };

    public CheckableImageView(final Context context) {
        super(context);
    }

    @Override
    public int[] onCreateDrawableState(final int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked())
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        return drawableState;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void setChecked(final boolean checked) {
        if (mChecked == checked)
            return;
        mChecked = checked;
        refreshDrawableState();
    }
}

动作模式监听器 (CAB)

@Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {

        actionMode.getMenuInflater().inflate(R.menu.contexmenumultiselection, menu);

        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {

        return false;
    }

    @Override  
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {

        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode) {

    }

我可以在动作模式下完成,但多选不起作用(自定义图像视图没有改变 bgcolor)为什么?我哪里错了?

4

0 回答 0