android保留gridview项setChecked无法正常工作

时间:2013-12-30 05:33:40

标签: android listview gridview

好的,所以我似乎在获取用于gridView的自定义RelativeLayout的setChecked方法时遇到了一些麻烦。到目前为止我的方法:我在旋转之间保留一个布尔列表,这样当调用gridview的基本适配器的getView方法时,我可以根据是否检查布尔值来设置一些数据。我的想法是,我在设备更改方向时保留一个布尔数组。但无论我尝试什么,我似乎无法让我的getView方法实际上正确使用setChecked。到目前为止,这是我的代码:

自定义RelativeLayout:

    public class CustomRelativeLayoutForGridView extends RelativeLayout implements
            Checkable {

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

        public CustomRelativeLayoutForGridView(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
        }

        public CustomRelativeLayoutForGridView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public CustomRelativeLayoutForGridView(Context context) {
            super(context);
        }

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

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

        @Override
        public void setChecked(boolean _checked) {
            checked = _checked;
            setBackgroundColor(checked ? 0xFF00FF00 : 0x00000000);
            refreshDrawableState();
        }

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

持有人:

    class myViewHolder {
        TextView text;
        ImageView icon;
        CustomRelativeLayoutForGridView rLayout;
        myViewHolder(View v) {
            text = (TextView) v.findViewById(R.id.grid_item_label);
            icon = (ImageView) v.findViewById(R.id.grid_item_image);
            rLayout = (CustomRelativeLayoutForGridView) v.findViewById(R.id.gridViewCellLayout);
        }
    }

getView:

    public View getView(final int position, View convertView, ViewGroup parent) {
            View gridView = convertView;
            myViewHolder holder = null;
            if (gridView == null) {
                gridView = new View(context);
                // get layout from mobile.xml
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                gridView = inflater.inflate(R.layout.file_manager_display_item,
                        null);
                holder = new myViewHolder(gridView);
                gridView.setTag(holder);
            } else {
                holder = (myViewHolder) convertView.getTag();
            }
            holder.text.setText(direcsPics[position].getName());
            if(imageSelected[position]) {
                holder.rLayout.setChecked(true);
            } else {
                holder.rLayout.setChecked(false);
            }
            return gridView;
        }

正如你在getView方法中看到的那样,我肯定会调用setChecked方法,但似乎没有任何事情发生。我已经仔细检查过布尔值是否正确保存,但是setChecked似乎根本没有做任何事情。有没有人希望知道发生了什么以及如何解决这个问题?我肯定感谢任何帮助,我可以得到^ _ ^我已经在这个代码上工作了一个星期,现在没有运气=(

1 个答案:

答案 0 :(得分:2)

确定!终于想通了我认为=)对我来说是一个真正的脑筋急转弯。首先,我会看一下让我感到困惑的部分:

if(imageSelected[position]) {
    holder.rLayout.setChecked(true);
} else {
    holder.rLayout.setChecked(false);
}

注意我的setChecked调用。这是我感到困惑的地方,我的意思是来自自定义RelativeLayout的代码肯定会改变背景颜色,因为在删除setBackgroundColor行后,我的突出显示不再有效。 好吧,我把一些Log调用扔给了RelativeLayout的setChecked方法,还有一些我调用setChecked的方法,但是我注意到调用setChecked的次数比我做的要多得多,而且所有调用的都是我的。所以android的召回意见。那么,我回想一下我的网格视图是如何在xml中设置的:

<GridView
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/fileManagerAcceptButton"
    android:columnWidth="100dp"
    android:drawSelectorOnTop="false"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center"
    android:horizontalSpacing="5dp"
    android:choiceMode="multipleChoice"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="5dp" >
</GridView>

注意选择模式?所以,如果其他东西优先于setChecked调用,那可能就是这样。所以也许我需要告诉gridview父母检查了什么,而不是试图在孩子中指定它。在此之后我回到我的活动并将其添加到我的OnCreateView(它是一个对话框片段):

for (int i = 0; i < gridView.getCount(); i++) {
    if (transferBoolArray[i]) {
        gridView.setItemChecked(i, true);
    } else {
        gridView.setItemChecked(i, false);
    }
}

问题解决了我=)我希望我的答案可以帮助将来的某个人^ _ ^