【问题标题】:Infinite Scrolling Gallery View with text description带有文本描述的无限滚动画廊视图
【发布时间】:2012-03-21 09:44:34
【问题描述】:

我有基于这个例子的无限画廊:

http://blog.blundell-apps.com/infinite-scrolling-gallery/,

它运行得很好,现在我想让图库仍然滚动浏览图像,并且在每个图像下应该有一个文本标题

我在网上搜索没有结果,请您帮我编码,只是开发中的初学者。

================================================ ====================================

新更新

上传照片说明我需要什么,我通过普通图库得到它(将文本应用到每个图像,并且也能够自定义文本,如下图所示,每个图像的文本都比其他图像不同,但仍然没有成功用无限画廊做到这一点:

请提供任何帮助和建议。

非常感谢。

【问题讨论】:

  • 欢迎来到 Stack Overflow :)。您的问题目前有点含糊——如果您告诉我们您的尝试以及您遇到的问题,您更有可能得到有用的答案。
  • @Blundell 请您检查与您的博客代码之一相关的这篇文章,任何建议将不胜感激:stackoverflow.com/questions/13496413/…

标签: android gallery android-custom-view


【解决方案1】:

我阅读了 Blundell 的教程,感谢他,我知道如何制作无限滚动画廊 :)

为了回答这个问题,关于如何在每张图片下方添加文字标题,我对 Blundell 的漂亮 tut 进行了同样的小改动,并在上述答案中使用了他的一些建议,我认为我有一个很好的方法来完成这项任务。

下面的代码根本不会膨胀或使用gallery_item.xml,因此与每次膨胀的方式相比,它会显着提高性能。

从 Blundell 的教程中删减了类的代码(因为在问题中,您只使用资源而不是 sdcard)。

public class InfiniteScrollingGalleryActivity extends Activity {

public class GalleryItem{
      int imageId;
      String caption;
       public int getImageId() {
        return imageId;     }

    public String getCaption() {
        return caption;
    }
        public GalleryItem(int i,String s) {
        imageId=i;
        caption=s;  }   
}

int[] resourceImages = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
        R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     GalleryItem[] item = new GalleryItem[6];
        //initialising all items, change member variables according to needs
    for(int i=0;i<6;i++){
        item[i] = new GalleryItem(resourceImages[i], "pic no" +(i+1));          }                
    setContentView(R.layout.activity_main);
     InfiniteGallery galleryOne = (InfiniteGallery) findViewById(R.id.galleryOne);
    galleryOne.setResourceGallery(item);
     }  }

这里我已经添加了 GalleryItem 类数组并传递了它。

还在 InfiniteGalley 类中添加了以下代码。

public void setResourceGallery(GalleryItem[] item) {
        setAdapter(new InfiniteGalleryResourceAdapter(getContext(), item));
        setSelection((getCount() / 2));
    }

下面代码的 getView() 是 好事发生的地方:

 public class InfiniteGalleryResourceAdapter extends BaseAdapter {

    /** The context your gallery is running in (usually the activity) */
    private Context mContext;   
 GalleryItem[] myItems; 

    public InfiniteGalleryResourceAdapter(Context context, GalleryItem[] item) {
        this.mContext = context;
            myItems=item;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // convertView is always null in android.widget.Gallery
        TextView t = new TextView(mContext);
        try {           
            int itemPos = (position % myItems.length);
            t.setText(myItems[itemPos].getCaption());
            Drawable d = mContext.getResources().getDrawable(myItems[itemPos].getImageId());
            ((BitmapDrawable) d).setAntiAlias(true); // Make sure we set anti-aliasing otherwise we get jaggies (non-smooth lines)

            //d.setBounds(0,0,60,60); //use this to change dimens of drawable,if needed
            t.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);

        } catch (OutOfMemoryError e) {
            // a 'just in case' scenario
            Log.e("InfiniteGalleryResourceAdapter", "Out of memory creating imageview. Using empty view.", e);
        }

        return t;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }
    /** The width of each child image */
    private static final int G_ITEM_WIDTH = 120;
    /** The height of each child image */
    private static final int G_ITEM_HEIGHT = 80;
    private int imageWidth;
    private int imageHeight;
}

getView() 中,我只是创建一个textView 并使用方便的t.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null); 将drawable 分配给它。所以它排除了膨胀布局的需要,这是一项繁重的操作。

下面是输出图片:

【讨论】:

  • 非常感谢您的回答,但我真正想在每张图片下显示的描述不仅是与位置相关的图片,正如我在第一张图片展示前的意思:(在我家拍摄的图片),第二个PIC(我在公园里的漂亮照片),像这样,我必须为每张图片添加不同的描述,非常感谢“现在我在工作,当我回家生病时,我无法检查代码。
  • 不是我为位置设置的文本,而是为数组的每个画廊项提供一个不同的字符串。那应该不是问题。它完全按照您在问题中提出的要求工作。
  • 请你告诉我如何(而不是我为位置设置的文本,只为数组的每个画廊项提供一个不同的字符串。)以便我可以获得每个图像的不同描述,谢谢
  • 告诉我你从哪里得到的字符串(如果上述问题的答案有效,你可以奖励赏金)
  • @Akhil 我接受并给了你真正应得的赏金,非常感谢
【解决方案2】:

在adapter中可以看到方法:getView,可以看到这个方法返回一个ImageView,所以现在你想让getView方法返回一个imageview和textview...

你可以通过几种方式做到这一点,这里你可以用 LayoutInflater 做到这一点

View v = getLayoutInflater().inflate(R.layout.gallery_item, null);
((ImageView) v.findViewById(R.id.img)).setImageResource(imageIds[position]);
((TextView) v.findViewById(R.id.caption)).setText(captions[position]);

因此,在您的 res/layout 文件夹中,您应该有一个包含 ImageView (img) 和 TextView (caption) 的“gallery_item”布局

gallery_item.xml

 <LinearLayout>
      <ImageView ... />
      <TextView ... />
 </LinearLayout>

希望这对您有所帮助!

编辑

如上例所示,您需要两个数组,一个是 imageIds,另一个是 textCaptions。为了让您的适配器保持美观和清洁,它要求您制作一个对象。

 public class GalleryItem {
       int imageId;
       String caption

       // Constructor

       // getters and setters

 }

然后您可以将GalleryItems 的数组或列表传递给适配器(替换setAdapter 方法)。即:

 GalleryItem[] items = new GalleryItem[];

然后在上面概述的 getView 方法中,您将提取每个对象:

 GalleryItem item = items[position];
 View v = getLayoutInflater().inflate(R.layout.gallery_item, null);

((ImageView) v.findViewById(R.id.img)).setImageResource(item.getImageId());
((TextView) v.findViewById(R.id.caption)).setText(item.getCaption());

希望这很清楚

【讨论】:

  • 我会尝试通过应用上面的代码来实现它,非常感谢
  • 请您检查更新编辑我亲爱的朋友,感谢您的帮助,谢谢
  • 你应该打开一个新问题。 同样在该代码中你永远不会调用 preparelist();
  • 如果我打开新问题,我会被重复并投反对票,你在做什么,以及如何将赏金转移到新问题上,我真的需要你的帮助,谢谢
  • @Blundell 亲爱的,请您检查第二次更新并给我您的建议,谢谢
猜你喜欢
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多