2013-06-19 100 views
0

我正在使用自定義GridView和圖像&複選框。用圖像顯示gridview &複選框工作正常。但在這裏我的問題是,使一個複選框檢查後,如果我滾動gridview另一個複選框檢查&再次,如果我再次向下滾動顯示。此處複選框選中狀態正在維持。我Adapterclass代碼是..如何在GridView Android中保持CheckBox持久性(處理複選框狀態)?

MyGrid.java

public class ImageAdapter extends BaseAdapter { 
    private LayoutInflater mInflater; 

    public ImageAdapter() { 
     mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return count; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 
     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = mInflater.inflate(R.layout.galleryitem, null); 
      holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage); 
      holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox); 
      holder.textview = (TextView) convertView.findViewById(R.id.saved_image_name); 

      Drawable background = holder.textview.getBackground(); 
      background.setAlpha(150); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.checkbox.setId(position); 
     holder.imageview.setId(position); 
     holder.textview.setId(position); 

     holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 
       // TODO Auto-generated method stub 
       CheckBox cb = (CheckBox) holder.checkbox; 

       int id = cb.getId(); 

       if (thumbnailsselection[id]) { 
        cb.setChecked(false); 
        thumbnailsselection[id] = false; 
        selected_images.remove(String.valueOf(id)); 
       } else { 
        cb.setChecked(true); 
        thumbnailsselection[id] = true; 
        if (selected_images.contains(String.valueOf(id))) { 

        }else{ 
         selected_images.add(String.valueOf(id)); 
        } 
       } 
      } 
     }); 

     holder.imageview.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       int id = v.getId(); 
       Intent intent = new Intent(); 
       intent.setAction(Intent.ACTION_VIEW); 
       intent.setDataAndType(Uri.parse("file://" + arrPath[id]), 
       "image/*"); 
       startActivity(intent); 
      } 
     }); 
     if (arrPath[position] == null || arrPath[position].equals(null)) { 

     }else{ 
     image_name=extractString(arrPath[position]); 
     String[] splited_name = image_name.split("\\."); 

     for (int i = 0; i < selected_images.size(); i++) { 
      if (selected_images.get(i).equals(String.valueOf(position)) || selected_images.get(i) == String.valueOf(position)) { 
       holder.checkbox.setChecked(true); 
      } 
     } 

     holder.textview.setText(splited_name[0]); 
     holder.imageview.setImageBitmap(thumbnails[position]); 
     holder.checkbox.setChecked(thumbnailsselection[position]); 
     holder.id = position; 

     holder.imageview.setScaleType(ImageView.ScaleType.FIT_XY); 
     } 
     return convertView; 
    } 
} 

class ViewHolder { 
    ImageView imageview; 
    CheckBox checkbox,checkbox1; 
    TextView textview; 
    int id; 
} 

誰能幫我如何保持複選框(選中複選框)的持續狀態。

+0

你想勾選複選框來保持滾動狀態嗎?假設第一行的複選框(僅當您上下滾動時選中複選框,選中第一行的複選框。)這就是您要查找的內容嗎? – Raghunandan

+0

是的..我正在尋找該解決方案。 – user2384424

+0

什麼部分代碼你有困惑嗎? – Raghunandan

回答

0

細胞被重複使用。在getView()您設置ID的位置,您還應該設置複選框holder.checkbox..setChecked(thumbnailsselection[position]);的狀態。此外,不建議更改ID,如果要保存位置使用setTag()並將位置放在那裏,視圖已在佈局中具有ID。

1

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

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

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

然後

cb.setChecked(mCheckStates.get(position, false)); 
cb.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,而不是textview使用imageview。

How to change the text of a CheckBox in listview?

+0

因爲我有點困惑,你可以根據我的代碼解釋一下這些變化.. – user2384424

+0

在編輯之前看到代碼,你應該可以修改看看這個例子,你有什麼困惑? – Raghunandan