2012-05-24 6 views
0

기본적으로 작성해야합니다 : 오른쪽의 확인란과 오른쪽의 이미지보기. 이런 식으로 뭔가 :GridView 용 BaseAdapter 만들기 android

enter image description here

세로 이러한 조합은 3XN해야한다, N은 행 수의 INT이다. 이것을 위해 나는 격자보기를 사용하는 것이 좋을 것이라고 생각했지만 그리드에 대한 많은 경험이 없다. 그래서 내가 어댑터를 쓰기 시작 :

나는이 방법을 가지고 :

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    CheckBox check = null; 
    ImageView pic = null; 

    LinearLayout view = new LinearLayout(mContext); 
    view.setOrientation(LinearLayout.VERTICAL); 

     check = new CheckBox(mContext); 
     check.setTag(position); 

     view.addView(check); 

     pic = new ImageView(mContext); 
     pic.setImageResource(R.drawable.btn_star); 

     view.addView(pic); 


    return view; 
} 

을 어쩌면 거기에 내가 확인란 및 이미지보기 중 하나 개보기를 만들어야합니다. 이 방법은 효과가 있지만 이미지가 확인란보다 낮 으면 한 줄로 만드는 방법은 무엇입니까?

감사합니다.

답변

2

예를 들어 griview 내용

에 대한 별도의 레이아웃을 만듭니다 gridview_custom.xml

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

<CheckBox 
    android:id="@+id/checkBox1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" android:layout_marginLeft="10dip"/> 

</LinearLayout> 

이제 GridView에 대한 사용자 정의 어댑터를 작성하고 getView()

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    View v = convertView; 
    CheckBox check = null; 
      ImageView pic = null; 


    if (convertView == null) { // if it's not recycled, initialize some 
           // attributes 
    LayoutInflater vi = 
      (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.gridview_custom, null); 
    } 

    pic = (ImageView)v.findViewById(R.id.imageView1); 
    check = (CheckBox) v.findViewById(R.id.checkBox1); 
    return v; 

} 
+0

감사이 당신을 :) 감사 잘 했어 – Streetboy

+0

좋아요! 다른 사람들에게 도움이 될 수 있도록 upvote하십시오. – Nishant

0

레이아웃이 고정되어 있으므로 XML 파일에서 레이아웃을 지정하고이를 사용하여보기를 만드는 것이 더 쉽습니다. 그래도 HORIZONTAL 오리엔테이션을 사용해야합니다. 또한보기를 재사용 할 수 있도록 처리합니다 (재사용 가능한 인스턴스는 convertView 매개 변수로 전달됩니다. null이 아닌 경우 사용하십시오).

0

이 체크 박스를 추가하지 마십시오 무시하고를 이미지를 동적으로. 대신 당신이, 당신이 필요로 체크 박스 및 수평 방향으로 이미지 뷰를 포함하는 XML 파일을 만들 수 있습니다 할 수있는 일의의 getView 기능은 다음과 같이 될 것입니다 :

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

if (CONTEXT != null) { 


     final LayoutInflater lInflater = (LayoutInflater) CONTEXT 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final View view = lInflater.inflate(R.layout.your_xml, null); 
     final CheckBox checkBox = (CheckBox) view 
       .findViewById(R.id.checkbox); 

     final ImageView ivIcon = (ImageView) view.findViewById(R.id.icon_image); 
     ivIcon.setImageResource(A.this.getResources().getDrawables(R.id.image)); 
     return view; 
     } 
     else { 
     return null; 
     } 
}