2013-09-21 140 views
1

大家好,我使用一個GridView工具從Android要顯示的圖像作爲菜單如何添加標題到GridView細胞

我在一個類使用此代碼我命名它ImageAdapter擴展BaseAdapter

Screenshot Of The Gridview With Images Without Text

我已經使用此代碼婁

package com.example.gridviewtest; 

import com.example.gridviewtest.R; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.content.Context; 
import android.widget.GridView; 
import android.widget.ImageView; 

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 

    public Integer[] mThumbIds = 
     { 
      R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, 
      R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, 
      R.drawable.pic7, R.drawable.pic8, R.drawable.pic9 
     }; 

    public ImageAdapter(Context c){ 
     mContext = c; 
    } 

    @Override 
    public int getCount() { 
     return mThumbIds.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return mThumbIds[position]; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView = new ImageView(mContext); 
     imageView.setImageResource(mThumbIds[position]); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setLayoutParams(new GridView.LayoutParams(200, 200)); 
     return imageView; 
    } 
} 

我嘗試使用

imageView.setText("Something"); 

但並不可與我 的要求是在細胞

添加圖片下的標題你能幫助我在這...

最好的問候......

+0

那隻貓是可愛的:P – Metalhead1247

+0

@ Metalhead1247大聲笑:) – Loai

+0

+1 Mr.cat \ ,, /:P – Metalhead1247

回答

6

您可以使用自定義適配器(作爲一個ListView):

public class CustomAdapter extends ArrayAdapter<Integer> { 
    Activity context; 
    ArrayList<Integer> objects; 

    public CustomAdapter(Activity context, ArrayList<Integer> objects) { 
     super(context, R.layout.row, objects); 
     this.context = context; 
     this.objects = objects; 
    } 

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

     if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.row, parent, false); 


     } 

     ImageView i = (ImageView) convertView.findViewById(R.id.icon); 
     i.setBackgroundResource(objects.get(position)); 

     TextView t = (TextView) convertView.findViewById(R.id.title); 
     t.setText("title"); 

     return convertView; 

    } 

    } 

當然,你會SE將其作爲gridView的適配器。

編輯:當然,而不是ArrayAdapter<Integer>你可以延長ArrayAdapter<YourHolder>

class YourHolder { 
    public int ID; 
    public String title; 
} 

編輯2:添加功能性代碼段。

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <GridView 
     android:id="@+id/grid_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:numColumns="3" 
     android:gravity="center_horizontal" 
     android:horizontalSpacing="10dp" 
     android:verticalSpacing="10dp" 
     android:padding="10dp" 
     android:background="#600000ff"  
     /> 

</RelativeLayout> 

grid_item_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:background="#6000ff00" 
    android:padding="4dp"> 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="label" 
     android:layout_gravity="center_horizontal"/> 
    <FrameLayout 
     android:id="@+id/holder" 
     android:layout_width="100dp" 
     android:layout_height="100dp"> 

     <ImageView 
      android:id="@+id/image" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="centerCrop"/> 
    </FrameLayout>  

</LinearLayout> 

CustomAdapter.class

public class CustomAdapter extends ArrayAdapter<Integer> { 

    private Activity context; 
    private ArrayList<Integer> objects; 

    public CustomAdapter(Activity context, ArrayList<Integer> objects) { 
     super(context, R.layout.grid_item_layout, objects); 
     this.context = context; 
     this.objects = objects; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if(convertView == null){ 
      LayoutInflater i = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = (View) i.inflate(R.layout.grid_item_layout, parent, false); 
     } 

     TextView t = (TextView) convertView.findViewById(R.id.label); 
     ImageView i = (ImageView) convertView.findViewById(R.id.image); 

     t.setText("label " + position); 
     i.setImageResource(objects.get(position)); 

     return convertView; 
} 

MainActivity

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     GridView g = (GridView) findViewById(R.id.grid_view); 
     CustomAdapter adapter = new CustomAdapter(this, getData()); 
     g.setAdapter(adapter); 
    } 

    public ArrayList<Integer> getData(){ 
     ArrayList<Integer> list = new ArrayList<Integer>(); 
     list.add(R.drawable.pic1); 
     // add 2 - 11 
     list.add(R.drawable.pic12); 

     return list; 
    } 

} 

最後一眼:

enter image description here

注:根據我的經驗,當有大量的圖片在GridView滾動變得緩慢和laggy,但是這不是你的問題的話題,所以在情況下,它也一個問題,請參閱Lazy load of images in ListView

+0

感謝,但能否請您解釋更多我現在能做到,,做你的意思是我應該用你放的這個或者什麼來代替我的班級? – Loai

+0

@ lo2i更新了答案,希望它能從代碼中理解。如果沒有,請詢​​問。 –

+0

Spanty ,,謝謝你現在對我來說工作得很好。但我有一個問題,我怎樣才能爲每張圖片定義一個文本? ,因爲我只能將圖片綁定到列表\t'list.add(R.drawable。pic1);'<<在這種情況下,我無法直接將文本綁定到圖片。 另一個問題,我可以如何設置一個方法可以讓我被按下的圖片? 最好的問候 – Loai