【问题标题】:Different text for each image in image viewpager图像查看器中每个图像的不同文本
【发布时间】:2012-12-01 23:05:59
【问题描述】:

我使用 viewpager 类来显示图像集而不是画廊类,因为它已被弃用并使用以下代码对其进行自定义以显示文本,问题是所有图像都显示相同的文本,我试图得到的是: 每张图片有不同的文字,用来解释它,假设我们有 5 张图片,它必须有 5 种不同的文字,每一张都描述它的图片。

任何建议将不胜感激,谢谢

代码:

ImagePager

   public class ImagePager extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra);
    ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);}

private int imageArra[] = { R.drawable.a, R.drawable.b, R.drawable.c,
                              R.drawable.d, R.drawable.e};        }

ImagePagerAdapter

   public class ImagePagerAdapter extends PagerAdapter {

Activity activity;
int imageArray[];

public ImagePagerAdapter(Activity act, int[] imgArra) {
    imageArray = imgArra;
    activity = act;}

public int getCount() {
    return imageArray.length;}

public Object instantiateItem(View collection, int position) {
    LayoutInflater inflater = (LayoutInflater)collection.getContext
                 ().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.custom_pager, null);   

    ImageView im=(ImageView) layout.findViewById(R.id.myimage);             
    im.setImageResource(imageArray[position]);

    TextView txt=(TextView) layout.findViewById(R.id.image_text);
    ((ViewPager) collection).addView(layout, 0);
       return layout;   }

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
            }

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
                      }

@Override
public Parcelable saveState() {
    return null;
              }   }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:orientation="vertical">
 <android.support.v4.view.ViewPager 
   android:id="@+id/myimagepager" 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" /> 
</LinearLayout>  

custom_pager.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:orientation="vertical" 
   android:background="#FFDAB9" 
   android:gravity="center_horizontal">
 <ImageView android:id="@+id/myimage" 
   android:layout_width="match_parent" 
   android:layout_height="0dp" 
   android:layout_margin="5dp" 
   android:layout_weight="2" /> 
 <TextView android:id="@+id/image_text" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:textColor="#B22222" 
    android:layout_weight="1" 
    android:text="text to image" />

 </LinearLayout>                       

【问题讨论】:

    标签: java android android-viewpager layout-inflater


    【解决方案1】:

    您可以将字符串数组传递到与每个图像对应的适配器中。 例如。

    在活动中用您的图像列表声明您的字符串数组..

    private int imageArra[] = { R.drawable.a, R.drawable.b, R.drawable.c,
                                  R.drawable.d, R.drawable.e};  
    
    private String[] stringArray = new String[] { "Image a", "Image b","Image c","Image d","Image e"}; 
    

    然后在实例化你的适配器时..

    ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra, stringArray );
    

    在您的适配器中:

    int imageArray[];
    String[] stringArray;
    public ImagePagerAdapter(Activity act, int[] imgArra, String[] stringArra) {
        imageArray = imgArra;
        activity = act;
        stringArray = stringArra;
    }
    

    然后从数组中赋值文本值

    TextView txt=(TextView) layout.findViewById(R.id.image_text);
    txt.setText(stringArray[position]);
    

    【讨论】:

    • 好的,但是如何为每张图片指定特定的文字,这意味着每张图片的文字与另一张图片不同,谢谢
    • 请说明我想了解的一点,当我想将文本添加到我的图像之前,我使用了带有画廊类的查看器,但是在 viewpager 中我们没有使用它,或者 viewpager 类的行为不需要那个,你能解释一下吗,谢谢。
    • 这是一个解释 ViewHolder 使用的链接-jmanzano.es/blog/?p=166 我不认为 ViewHolder 在 ViewPager 中使用很重要,因为回收 View 并不重要(性能不会受到影响)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多