在GridView的复选框被选中,而GridView的滚动向上和向下 [英] in gridview checkbox is unchecked while scrolling gridview up and down

查看:95
本文介绍了在GridView的复选框被选中,而GridView的滚动向上和向下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView和我已经把一个复选框和ImageView的。 问题是,当我选择此复选框,然后向下滚动时,checBox变得听之任之。虽然取消选中事件不会触发,它看起来选中。我没有申请任何风格的复选框。什么原因?我怎样才能解决这个问题。

  holder.chckbx.setOnCheckedChangeListener(新OnCheckedChangeListener(){
                        @覆盖
                        公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){


                            如果(器isChecked){
                                     Log.e(选中,选中);
                                }
                                其他{
                                     Log.e(未登记,未登记);
                                }
                        }
                    });
 

解决方案

https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M

从列表视图在上面的链接罗曼盖伊的解决方案图。

您自定义适配器必须实现 CompoundButton.OnCheckedChangeListener 和使用 SparseBooleanArray

然后

  holder.chckbx.setOnCheckedChangeListener(本);
 

然后

 公共布尔器isChecked(INT位置){
        返回mCheckStates.get(位置,假);
    }

    公共无效setChecked(INT位置,布尔器isChecked){
        mCheckStates.put(位置,器isChecked);

    }

    公共无效切换(INT位置){
        setChecked(位置,器isChecked(位置)!);
    }
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,
        布尔器isChecked){
     mCheckStates.put((整数)buttonView.getTag(),器isChecked);

}
 

示例

 公共类MainActivity扩展活动器具
AdapterView.OnItemClickListener {
    诠释计数;
私人CheckBoxAdapter mCheckBoxAdapter;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    最终的GridView的GridView =(GridView控件)findViewById(R.id.lv);
    gridView.setTextFilterEnabled(真正的);
    gridView.setOnItemClickListener(本);
    mCheckBoxAdapter =新CheckBoxAdapter(本);
           gridView.setAdapter(mCheckBoxAdapter);

   }

公共无效onItemClick(适配器视图父,查看查看,INT
位置,长的id){
    mCheckBoxAdapter.toggle(位置);
}

类CheckBoxAdapter扩展ArrayAdapter实现CompoundButton.OnCheckedChangeListener
{私人SparseBooleanArray mCheckStates;
   LayoutInflater mInflater;
    ImageView的imageview1,ImageView的;
    复选框CB;

    CheckBoxAdapter(MainActivity上下文)
    {
        超级(上下文,0);
        mCheckStates =新SparseBooleanArray(10);
        mInflater =(LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }
    @覆盖
    公众诠释getCount将(){
        // TODO自动生成方法存根
        返回10;
    }

    @覆盖
    公共对象的getItem(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }

    @覆盖
    众长getItemId(INT位置){
        // TODO自动生成方法存根

        返回0;
    }

    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
        // TODO自动生成方法存根
        查看VI = convertView;
        如果(convertView == NULL)
         VI = mInflater.inflate(R.layout.checkbox,NULL);
         ImageView的=(ImageView的)vi.findViewById(R.id.textView1);

         CB =(复选框)vi.findViewById(R.id.checkBox1);

         cb.setTag(位置);
         cb.setChecked(mCheckStates.get(位置,FALSE));
        cb.setOnCheckedChangeListener(本);
        返回六;
    }
     公共布尔器isChecked(INT位置){
            返回mCheckStates.get(位置,假);
        }

        公共无效setChecked(INT位置,布尔器isChecked){
            mCheckStates.put(位置,器isChecked);

        }

        公共无效切换(INT位置){
            setChecked(位置,器isChecked(位置)!);
        }
    @覆盖
    公共无效onCheckedChanged(CompoundButton buttonView,
            布尔器isChecked){
        // TODO自动生成方法存根
         mCheckStates.put((整数)buttonView.getTag(),器isChecked);

    }

}

}
 

activity_main.xml

 < RelativeLayout的
     的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
     机器人:layout_width =FILL_PARENT
     机器人:layout_height =FILL_PARENT>
     < GridView控件
     机器人:ID =@ + ID / LV
     机器人:layout_width =WRAP_CONTENT
     机器人:为numColumns =2
      机器人:layout_height =WRAP_CONTENT
      />

    < / RelativeLayout的>
 

checkbox.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    < ImageView的
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:layout_marginLeft =15dp
        机器人:layout_marginTop =34dp
        机器人:SRC =@可绘制/ ic_launcher/>

    <复选框
        机器人:ID =@ + ID / checkBox1
        机器人:可聚焦=假
        机器人:focusableInTouchMode =假
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_below =@ + ID / textView1
        机器人:layout_marginRight =22dp
        机器人:layout_marginTop =23dp/>

< / RelativeLayout的>
 

在这里的人回答类似。而不是列表视图使用GridView控件。

<一个href="http://stackoverflow.com/questions/17168814/how-to-change-the-text-of-a-checkbox-in-listview/17169411#17169411">How以更改CheckBox的文本列表视图?

I have a GridView and I have put a checkbox and imageview. The problem is that when I check this checkbox and scroll down, the checBox gets unchecked. Though the uncheck event is not fired, it looks unchecked. I have not applied any style to the checkbox. What can be the reason? How can I fix this.

 holder.chckbx.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                            if(isChecked){
                                     Log.e("checked","checked");
                                }
                                else{
                                     Log.e("unchecked","unchecked");
                                }
                        }
                    });

解决方案

https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M

Drawing from romain guy's solution of listview in the above link.

Your Custom Adapter must implement CompoundButton.OnCheckedChangeListener and use a SparseBooleanArray

Then

 holder.chckbx.setOnCheckedChangeListener(this);

Then

     public boolean isChecked(int position) {
        return mCheckStates.get(position, false);
    }

    public void setChecked(int position, boolean isChecked) {
        mCheckStates.put(position, isChecked);

    }

    public void toggle(int position) {
        setChecked(position, !isChecked(position));
    }
@Override
public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {
     mCheckStates.put((Integer) buttonView.getTag(), isChecked);    

}

Example

public class MainActivity extends Activity implements
AdapterView.OnItemClickListener {
    int count;
private CheckBoxAdapter mCheckBoxAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final GridView gridView = (GridView) findViewById(R.id.lv);
    gridView.setTextFilterEnabled(true);
    gridView.setOnItemClickListener(this);
    mCheckBoxAdapter = new CheckBoxAdapter(this);
           gridView.setAdapter(mCheckBoxAdapter);

   }

public void onItemClick(AdapterView parent, View view, int
position, long id) {
    mCheckBoxAdapter.toggle(position);
}

class CheckBoxAdapter extends ArrayAdapter implements CompoundButton.OnCheckedChangeListener
{  private SparseBooleanArray mCheckStates;
   LayoutInflater mInflater;
    ImageView imageview1,imageview;
    CheckBox cb;

    CheckBoxAdapter(MainActivity context)
    {
        super(context,0);
        mCheckStates = new SparseBooleanArray(10);
        mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 10;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub

        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi=convertView;
        if(convertView==null)
         vi = mInflater.inflate(R.layout.checkbox, null); 
         imageview= (ImageView) vi.findViewById(R.id.textView1);

         cb = (CheckBox) vi.findViewById(R.id.checkBox1);

         cb.setTag(position);
         cb.setChecked(mCheckStates.get(position, false));
        cb.setOnCheckedChangeListener(this);
        return vi;
    }
     public boolean isChecked(int position) {
            return mCheckStates.get(position, false);
        }

        public void setChecked(int position, boolean isChecked) {
            mCheckStates.put(position, isChecked);

        }

        public void toggle(int position) {
            setChecked(position, !isChecked(position));
        }
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        // TODO Auto-generated method stub
         mCheckStates.put((Integer) buttonView.getTag(), isChecked);    

    }

}

}

activity_main.xml

<RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent">
     <GridView
     android:id="@+id/lv"
     android:layout_width="wrap_content"
     android:numColumns="2"
      android:layout_height="wrap_content"
      />

    </RelativeLayout>

checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="34dp"
        android:src="@drawable/ic_launcher"/>

    <CheckBox
        android:id="@+id/checkBox1"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginRight="22dp"
        android:layout_marginTop="23dp" />

</RelativeLayout>

Similar to the one answered here. Instead of listview use gridview.

How to change the text of a CheckBox in listview?

这篇关于在GridView的复选框被选中,而GridView的滚动向上和向下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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