마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

gridview 커스텀.. 질문입니다.

0 추천
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    
    <GridView 
     android:id="@+id/product_gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:scrollbars="vertical"
        android:stretchMode="columnWidth"
        />
</LinearLayout>

그리드뷰 xml이구요

 

private List<String> sdcardpath;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     // TODO Auto-generated method stub
     setContentView(R.layout.activity_album);
     GridView gv_album = (GridView)findViewById(R.id.product_gridview);
     gv_album.setAdapter(new ImageGridAdapter(this));
 }

액티비티입니다

 

public class ImageGridAdapter extends BaseAdapter{

 private Context context = null;
 private List<String> sdcardpath;
 
 public ImageGridAdapter(Context con){
  this.context = con;
  try{
   sdcardpath = new ArrayList<String>();
   File[] file = new File(Environment.getExternalStorageDirectory()+"/newtalktalk/").listFiles();
   for(File filename : file){
    sdcardpath.add(Environment.getExternalStorageDirectory()+"/newtalktalk/" + filename.getName().toString());
   }
  }catch (Exception e) {
   // TODO: handle exception
   
   Log.d("ErrorLog", "ImageAdapter : " + e.toString() );
   
  }
 }
 
 // 이미지의 갯수 
 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return sdcardpath.size();
 }

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

 // 선택된 이미지ID를 반환
 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  View v = convertView;
  v = View.inflate(context, R.layout.testgridview, null);
  ImageView v_image = (ImageView)v.findViewById(R.id.photoimage);
  
  try{
   if(null != convertView){
    //v_image = (ImageView)convertView;
    Bitmap bmp = BitmapFactory.decodeFile(sdcardpath.get(position).toString());
    bmp = Bitmap.createScaledBitmap(bmp, 320, 240, false);
    v_image.setImageBitmap(bmp);
   }else{
    //이미지 사이즈 조정 320 * 240
    Bitmap bmp = BitmapFactory.decodeFile(sdcardpath.get(position).toString());
    bmp = Bitmap.createScaledBitmap(bmp, 320, 240, false);

    // 이미지뷰 재정의 
    //v_image = new ImageView(context);
    v_image.setAdjustViewBounds(true);
    v_image.setImageBitmap(bmp);
    
    //ImageViewOnClickListener imageviewclick = new ImageViewOnClickListener(context, Imageids[position]);
    //v_image.setOnClickListener(imageviewclick);
   }
  }catch (Exception e) {
   // TODO: handle exception
   Log.d("ErrorLog", "ImageAdapter GetView : " + e.toString());
  }
  
  
  return v;
 }

그리드뷰 아답터인데요..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
     <ImageView
         android:id="@+id/photoimage"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
          />
     <CheckBox
         android:id="@+id/chk_selectphoto"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:paddingRight="20dp"
         android:layout_alignParentRight="true"
         android:layout_alignParentBottom="true"
          />
</RelativeLayout>

커스텀레이아웃입니다..

 

layout은 잘나옵니다.. 체크박스 체크도 되구요..

근데.. =ㅅ= 이거 체크박스 체크를 해놓구.. 스크롤하게되면.. 체크박스 체크 해놧던게;

초기화 됩니다 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ

럴수.......... 도와주세요 고수님들..ㅜ.ㅜ

어느부분이 잘못됫을까여??

아니면 그리드뷰 속성이 잘못된건가요?ㅜㅜ

익명사용자 님이 2013년 4월 9일 질문

1개의 답변

0 추천
GridView의 각각의 아이템 View는 임시적인 View입니다. 화면에서 사라지면 아이템 View는 재활용을 위해 GridView에서 빠졌다가 다른 아이템을 위해서 재활용되게 됩니다. 체크 상태는 따로 변수를 만들어서 가지고 있다가 getView에서 체크를 다시 세팅해 주셔야 합니다. 체크박스가 변경되면 변수도 변경해 주셔야 하고요.
바부팅팅 (420 포인트) 님이 2013년 4월 9일 답변
...