2013-05-27 297 views
0

我只是想實現http://developer.android.com/training/displaying-bitmaps/index.html中提供的位圖代碼。 我只想在每個縮略圖圖像附近添加圖像名稱。 是否需要添加我的textview? 任何想法如何實現這一目標?從代碼在gridview中添加Textview

@Override 
    public View getView(int position, View convertView, ViewGroup container) { 
     // First check if this is the top row 
     if (position < mNumColumns) { 
      if (convertView == null) { 
       convertView = new View(mContext); 
      } 
      // Set empty view with height of ActionBar 
      convertView.setLayoutParams(new AbsListView.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight)); 
      return convertView; 
     } 

     // Now handle the main ImageView thumbnails 
     ImageView imageView; 
     TextView textView; 
     if (convertView == null) { // if it's not recycled, instantiate and initialize 
      imageView = new RecyclingImageView(mContext); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setLayoutParams(mImageViewLayoutParams); 
      imageView.setPadding(20, 20, 20, 20); 


     } else { // Otherwise re-use the converted view 
      imageView = (ImageView) convertView; 
     } 

     // Check the height matches our calculated column width 
     if (imageView.getLayoutParams().height != mItemHeight) { 
      imageView.setLayoutParams(mImageViewLayoutParams); 
     } 

     // Finally load the image asynchronously into the ImageView, this also takes care of 
     // setting a placeholder image while the background thread runs 
     mImageFetcher.loadImage(Images.imageThumbUrls[position - mNumColumns], imageView); 
     return imageView; 
    } 

回答

0

getview方法緊裹的LinearLayout外

@Override 
    public View getView(int position, View convertView, ViewGroup container) { 
     // First check if this is the top row 
     if (position < mNumColumns) { 
      if (convertView == null) { 
       convertView = new View(mContext); 
      } 
      // Set empty view with height of ActionBar 
      convertView.setLayoutParams(new AbsListView.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight)); 
      return convertView; 
     } 

    // Now handle the main ImageView thumbnails 
    LinearLayout layout=new LinearLayout(context); 
    layout.add(imageView); 
    layout.add(textView) 

    ImageView imageView; 
    TextView textView; 
    if (convertView == null) { // if it's not recycled, instantiate and initialize 
     imageView = new RecyclingImageView(mContext); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setLayoutParams(mImageViewLayoutParams); 
     imageView.setPadding(20, 20, 20, 20); 


    } else { // Otherwise re-use the converted view 
     converView= (ImageView) layout; 
    } 

    // Check the height matches our calculated column width 
    if (imageView.getLayoutParams().height != mItemHeight) { 
     imageView.setLayoutParams(mImageViewLayoutParams); 
    } 

    // Finally load the image asynchronously into the ImageView, this also takes care of 
    // setting a placeholder image while the background thread runs 
    mImageFetcher.loadImage(Images.imageThumbUrls[position - mNumColumns], imageView); 
    return layout; 
} 
+0

thnx ..但你能告訴我是在XML中創建textview嗎? – user2291423

0

那樣創建自定義。

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

     final ViewHolder holder; 

     if (convertView == null || convertView.getTag() == null) { 
      holder = new ViewHolder(); 
      convertView = mInflater.inflate(R.layout.custom_row, null); 
      holder.image = (ImageView) convertView.findViewById(R.id.image_List); 
      holder.url = (TextView) convertView.findViewById(R.id.text_List); 

      convertView.setTag(holder); 

     } 

       holder.url.setText("use name"); 
      if (bitmap != null) {    
       // image download now compress and set on Image view 
       bitmap = Bitmap.createScaledBitmap(bitmap, 60, 60, true); 
       imageView.setImageBitmap(bitmap); 
      }   



     return convertView; 
    } 

    private static class ViewHolder { 
     ImageView image; 

     TextView url; 
    } 
0

嘗試這個例子:

layout_gridview.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/item" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:gravity="center_horizontal" 
    tools:ignore="UseCompoundDrawables" > 

    <ImageView 
     android:id="@+id/iv_thumb" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/tv_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

VideoAdaper.java

public class VideoAdapter extends BaseAdapter { 
    private List<Video> list; 
    private Context context; 
    private LayoutInflater inflater; 
    public VideoAdapter(Context context, List<Video> list) { 
     this.context = context; 
     this.list = list; 
     inflater = LayoutInflater.from(context); 
    } 
    @Override 
    public int getCount() { 
     return list.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return list.get(position); 
    } 

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

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     View view = inflater.inflate(R.layout.layout_video_gridview, parent, false); 
     ImageView iv_thumb = (ImageView) view.findViewById(R.id.iv_thumb); 
     new DownloadImageTask(iv_thumb).execute(list.get(position).getThumbnail()); 

     TextView tv_title = (TextView) view.findViewById(R.id.tv_title); 
     tv_title.setText(list.get(position).getTitle()); 

     LinearLayout item = (LinearLayout) view.findViewById(R.id.item); 
     item.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(context, Detail.class); 
       intent.putExtra("ID", list.get(position).getId()); 
       context.startActivity(intent); 
      } 
     }); 
     return view; 
    } 

} 

而結果:

enter image description here