安卓preserve gridview的项目setChecked不能正确工作 [英] android preserve gridview item setChecked not working

查看:188
本文介绍了安卓preserve gridview的项目setChecked不能正确工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我似乎有一些麻烦用于GridView的自定义的RelativeLayout的setChecked方法要正确调用。我的做法至今:我是preserving旋转之间的布尔列表,这样,当GridView的基本适配器的getView方法被调用时,我可以根据是否布尔值检查或不设置一些数据。我们的想法是,我是preserving当设备改变方向的布尔数组。但是无论我怎么努力,我似乎​​无法让我的getView方法以实际正确使用setChecked。这里是我的code迄今:

ok, so I seem to be having some trouble getting the setChecked method of a custom RelativeLayout used for gridView to be called properly. My approach so far: I'm preserving a boolean list between rotations so that when the getView method of the gridview's base adapter is called, I can set some data depending on if the boolean value is checked or not. The idea is that I'm preserving a boolean array for when the device changes orientation. However no matter what I try, I can't seem to get my getView method to actually properly use setChecked. Here is my code so far:

自定义的RelativeLayout:

the custom 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:

the 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方法,我definetly调用setChecked方法看,但似乎没有发生。我仔细检查过的布尔值正在preserved正常,但setChecked似乎并没有做任何事情。没有人有希望有一个想法,这是怎么回事,如何解决这一问题?我确实AP preciate任何帮助,我可以得到^ _ ^我一直对这个code一个星期,现在没有运气=(

as you can see in the getView method I'm definetly calling the setChecked method, but nothing seems to happen. I've double checked that the boolean values are being preserved properly, but setChecked doesn't seem to do anything at all. Does anyone out there hopefully have an idea what's going on and how to fix this? I'd sure appreciate any help I can get ^_^ I've been working on this code for a week now with no luck =(

推荐答案

好吧!终于想通了,我认为=)是一个真正的脑部锻炼我。首先,我将看看这真的很令人困惑我的部分:

Ok! Finally figured it out I think =) Was a real brain-teaser for me. First I'll look at the part that was really confusing to me:

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

请注意我的setChecked电话。这是我很困惑,我的意思是从自定义的code RelativeLayout的无疑是改变背景颜色,因为删除setBackgroundColor行我的高亮不再工作了。
好吧,我把一些日志调用到的RelativeLayout的setChecked方法和一些在这里,我打电话setChecked,但我注意到有很多多次调用setChecked比我做的,经过我的所有调用。所以Android的回忆的意见。好吧,那么我回忆起我的GridView如何在XML设置:

Notice my setChecked calls. This is where I was confused, I mean the code from the custom RelativeLayout was assuredly changing the backgrounds colors, since after removing the setBackgroundColor line my highlighting no longer worked. Well, I threw some Log calls to the setChecked method of the RelativeLayout and some here where I was calling setChecked, but I noticed a lot more calls to setChecked than I was doing, and all those called after mine. So android's recalling the views. Well, then I recalled how my gridview is setup in the 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>

注意选择模式?所以,如果别的东西正在preference在setChecked调用,它可能是这一点。所以,也许我需要告诉gridview的父母发生了什么检查,不要试图在孩子来指定。在此之后,我回到我的活动,并已将此添加到我的OnCreateView(这是一个对话片段):

notice the choice mode? So, if something else is taking preference over the setChecked calls, it's probably this. So maybe I need to tell the gridview parent what's checked and not try to specify it in the child. After this I went back to my activity and added this to my OnCreateView(it's a dialog fragment):

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

解决了我的问题=)我只是希望我的回答可以帮助别人在未来的^ _ ^

problem solved for me =) I just hope my answer helps someone in the future ^_^

这篇关于安卓preserve gridview的项目setChecked不能正确工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆