[Android] Tạo danh sách logo sử dụng gridView

Public on April 25, 2014









Hình ảnh Demo !
Tạo layout mới cho 1 ô trong gridview, gridview_mot_o.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"
    android:background="@drawable/border"
     >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:text="Large Text"
        android:textColor="#000000"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
2.       Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="auto_fit"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
         >
    </GridView>

</LinearLayout>
3.       Trong file java .
Khai báo 2 mảng chưa hình và tên
       String[]ten={"android","apple","blogger",
                           "chrome","dell","facebook",
                           "firefox","twitter","ie",
                           "microsoft","hp","xbox"};
      
       int[]hinh={R.drawable.android,R.drawable.apple,R.drawable.blogger
                       ,R.drawable.chrome,R.drawable.dell,R.drawable.facebook
                       ,R.drawable.firefox,R.drawable.twitter,R.drawable.ie
                       ,R.drawable.microsoft,R.drawable.hp,R.drawable.xbox};
Tạo ra 1 class (vẫn nằm trong class activity hiện tại)
/////////////class đại dien cho  1 ô //////   
    public static class View_Mot_O
    {
       public ImageView imageview;
       public TextView textview;
    }
 ///////ket thuc class View_Mot_O///////////
Xay mot lop my adapter (cung nằm trong class activity)
public class myadapter extends BaseAdapter
    {
       Context context;
       public myadapter(Context c)
       {
              context=c;
       }
      
              public int getCount() {
                     // TODO Auto-generated method stub
                     return hinh.length;
              }

              public Object getItem(int position) {
                     // TODO Auto-generated method stub
                     return hinh[position];
              }

              public long getItemId(int arg0) {
                     // TODO Auto-generated method stub
                     return 0;
              }

              public View getView(int arg0, View arg1, ViewGroup arg2) {
                     // TODO Auto-generated method stub
                     View_Mot_O mot_o;
                     LayoutInflater layoutinflater= ((Activity)context).getLayoutInflater();
                    
                     if(arg1==null
                     { 
                           mot_o = new View_Mot_O(); 
                           arg1 = layoutinflater.inflate(R.layout.gridview_mot_o, null); 
                           mot_o.textview = (TextView) arg1.findViewById(R.id.textView1); 
                           mot_o.imageview = (ImageView)arg1.findViewById(R.id.imageView1); 
                           arg1.setTag(mot_o); 
                     } 
                     else
                           mot_o=(View_Mot_O)arg1.getTag();
                    
                    
                    
                     mot_o.imageview.setImageResource(hinh[arg0]);
                     mot_o.textview.setText(ten[arg0]);
                    
                     return arg1;
              }
      
    }
Cuối cùng tạo gridview, gán adapter và bắt sự kiện.
  gv=(GridView)findViewById(R.id.gridView1);
        gv.setAdapter(new myadapter(this));
        gv.setOnItemClickListener(new OnItemClickListener() {

                     public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                  long arg3) {
                           // TODO Auto-generated method stub
                           Toast.makeText(GridviewlogoActivity.this,
                                         "ban chọn hình thứ "+position+ " logo "+ ten[position],
                                         Toast.LENGTH_SHORT).show();
                     }
              });

Download full Source Code Download

Chương trình tạo danh sách listview người đẹp !

Dùng adapter có thể kế thừa từ BaseAdapter hoặc từ ArrayAdapter, xem lại bài listviewnguoi đẹp để xem 2 cach.
Khi lót màu nền, nếu cuộn sẽ bị xuất hiện phần đen. Dùng cacheColorHint cho màu trong suốt #00000000 (chú ý: 8 số 0 nhé) Màu nền linearlayout là "#9BA841"

Co the do them theo dang selector , file nutdo.xml trong thu muc drawable trong bai button nang cao.
Trong getview truoc khi tra ve arg1 (hoac convertview) ta gan vao no nhu sau
arg1.setBackgroundResource(R.drawable.nutdo);


Download full Source Code Download

        






[Advertising]Faster Thinking - Speed up your brain


Faster Thinking Game



sentiment_satisfied Emoticon