在 Android 中向自定义图库视图添加描述

发布于 2024-12-02 20:03:30 字数 1424 浏览 2 评论 0原文

有谁知道如何添加图像的名称或图像的描述,以便它也会在每个图像的投掷时发生变化。

我可以通过获取单独的文本视图并在适配器的 getView() 中膨胀此文本视图来做到这一点。但其效率不高。 所以请帮我找出其他方法...

我的代码:

**public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;
    private Vector<ListObjectWithTwoTextAndOneImage> data;
    public ImageAdapter(Context c,
            Vector<ListObjectWithTwoTextAndOneImage> d)
{
    super();
        mContext = c;
        data = d;

    }
    public int getCount() {
        return data.size();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        **fishName.setText(data.get(position).getFirstItem());**
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);

        imageView.setImageBitmap((Utils.getInstance().getImageFromAssets(
                PhotoGalleryGallery.this, data.get(position)
                        .getThumbNailImageUrl() + ".jpg"))); // TODO: change
        imageView.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        // imageView.setBackgroundResource(mGalleryItemBackground);
        return imageView;
    }
}**

提前致谢

do anyone know how to add the name of image or a description of the image so that it will also get change on flinging of every image.

i am able to do it by taking a separate text view and inflating this text view in the getView() of the adapter. But its not efficient .
so please help me to find out the other way...

my code:

**public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;
    private Vector<ListObjectWithTwoTextAndOneImage> data;
    public ImageAdapter(Context c,
            Vector<ListObjectWithTwoTextAndOneImage> d)
{
    super();
        mContext = c;
        data = d;

    }
    public int getCount() {
        return data.size();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        **fishName.setText(data.get(position).getFirstItem());**
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);

        imageView.setImageBitmap((Utils.getInstance().getImageFromAssets(
                PhotoGalleryGallery.this, data.get(position)
                        .getThumbNailImageUrl() + ".jpg"))); // TODO: change
        imageView.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        // imageView.setBackgroundResource(mGalleryItemBackground);
        return imageView;
    }
}**

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满意归宿 2024-12-09 20:03:30

谢谢...我也得到了其他替代解决方案..
我刚刚在 get 动态中创建了一个线性布局,然后添加一个图像和文本视图,......

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

        LinearLayout layout = new LinearLayout(getApplicationContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


        ImageView imageView = new ImageView(mContext);
        imageView.setImageBitmap((Utils.getInstance().getImageFromAssets(
                PhotoGalleryGallery.this, data.get(position)
                        .getThumbNailImageUrl() + ".jpg"))); // TODO: change

        if (Configuration.ORIENTATION_PORTRAIT==PhotoGalleryGallery.this.getResources().getConfiguration().orientation) {

            imageView.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            TextView tv = new TextView(mContext);
            tv.setText(data.get(position).getFirstItem());
            tv.setTextColor(Color.WHITE);
            tv.setTypeface(null, Typeface.BOLD);
            tv.setPadding(20, 20, 0, 5);
            tv.setMaxLines(2);
            tv.setMinLines(2);

            ImageView divider= new ImageView(mContext);
            divider.setBackgroundResource(R.drawable.devider);
            divider.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            divider.setScaleType(ImageView.ScaleType.FIT_XY);

            TextView tv1 = new TextView(mContext);
            tv1.setText(data.get(position).getSecondItem());
            tv1.setTextColor(Color.WHITE);
            tv1.setTypeface(null, Typeface.BOLD);
            tv1.setPadding(20, 5, 0, 0);
            tv1.setMinLines(2);
            tv1.setMaxLines(2);

            layout.addView(imageView);
            layout.addView(tv);
            layout.addView(divider);
            layout.addView(tv1);

        } else {
            imageView.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            layout.addView(imageView);

        }
        picPosition=position;

        return layout;
    }

thanks ...i got the other alternate solution also..
I have just created one Linear layout inside the get dynamically and then adding one image and text view,....

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

        LinearLayout layout = new LinearLayout(getApplicationContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


        ImageView imageView = new ImageView(mContext);
        imageView.setImageBitmap((Utils.getInstance().getImageFromAssets(
                PhotoGalleryGallery.this, data.get(position)
                        .getThumbNailImageUrl() + ".jpg"))); // TODO: change

        if (Configuration.ORIENTATION_PORTRAIT==PhotoGalleryGallery.this.getResources().getConfiguration().orientation) {

            imageView.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            TextView tv = new TextView(mContext);
            tv.setText(data.get(position).getFirstItem());
            tv.setTextColor(Color.WHITE);
            tv.setTypeface(null, Typeface.BOLD);
            tv.setPadding(20, 20, 0, 5);
            tv.setMaxLines(2);
            tv.setMinLines(2);

            ImageView divider= new ImageView(mContext);
            divider.setBackgroundResource(R.drawable.devider);
            divider.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            divider.setScaleType(ImageView.ScaleType.FIT_XY);

            TextView tv1 = new TextView(mContext);
            tv1.setText(data.get(position).getSecondItem());
            tv1.setTextColor(Color.WHITE);
            tv1.setTypeface(null, Typeface.BOLD);
            tv1.setPadding(20, 5, 0, 0);
            tv1.setMinLines(2);
            tv1.setMaxLines(2);

            layout.addView(imageView);
            layout.addView(tv);
            layout.addView(divider);
            layout.addView(tv1);

        } else {
            imageView.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            layout.addView(imageView);

        }
        picPosition=position;

        return layout;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文