在gridview中添加Textview

时间:2013-05-27 09:29:58

标签: android bitmap android-gridview

我只想尝试实现http://developer.android.com/training/displaying-bitmaps/index.html中提供的位图代码。 我只想在每个缩略图附近添加图像名称.. 我要添加我的textview吗? 知道如何实现这个目标吗?

来自代码的getview方法

  @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;
    }

3 个答案:

答案 0 :(得分:0)

将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;
}

答案 1 :(得分: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;
    }

答案 2 :(得分: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