2013-06-21 33 views
0

我有一個GridView,我已經把一個複選框和imageview。 問題是,當我檢查這個複選框並向下滾動時,checBox會被取消選中。雖然未選中的事件未被解僱,但它看起來沒有被選中。我沒有將任何樣式應用於複選框。可能是什麼原因?我怎樣才能解決這個問題。在gridview複選框未選中的同時向上和向下滾動gridview

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


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

「在其圖像上放置複選框」。我不明白這個問題的部分 – Raghunandan

回答

1

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

從上面的鏈接列表視圖的羅曼蓋伊的解決方案圖紙。

您的自定義適配器必須實現CompoundButton.OnCheckedChangeListener並使用SparseBooleanArray

然後

holder.chckbx.setOnCheckedChangeListener(this); 

然後

 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);  

} 

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> 

類似於一個回答在這裏。而不是listview使用gridview。

How to change the text of a CheckBox in listview?

+0

感謝您的答覆,讓我檢查 – raju

+0

非常感謝它的工作正常 – raju

+0

很高興它幫助。 – Raghunandan

0

我打了同樣的問題,找到了解決辦法。希望我省了幾分鐘的調試

問題是,當我檢查這個複選框並向下滾動時,checBox被取消選中。雖然未選中的事件未被解僱,但它看起來沒有被選中。

setOnCheckedChangeListener在這裏是錯誤的監聽器。當gridView中的項目被回收時,複選框會自動重置,因此可以看到錯誤的取消選中事件。

解決方案:使用OnClickListener代替