java - 图像查看器中每个图像的不同文本

标签 java android android-viewpager layout-inflater

我使用 viewpager 类来显示图像集而不是 gallery 类,因为它已被弃用,并使用以下代码自定义它以显示文本,问题是所有图像显示的文本相同,我试图得到的是: 每张图片都有不同的文字,解释它,假设我们有 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>                       

最佳答案

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

在 Activity 中用你的图像列表声明你的字符串数组..

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]);

关于java - 图像查看器中每个图像的不同文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13664662/

相关文章:

android - 我如何在我的 ScrollView 中调整我的 ImageView 的大小,因为它在这个图像中完成?

android-viewpager - Android - 如何从右到左查看寻呼机?

java - "Home Screen"安卓切换 View 时的效果

java - 使用 Spring MVC 获取 JR 报告时出现异常

java - 如何在java中的线程之间传递整数/其他变量?

Android 选项卡滚动延迟

java - 为回收器 View 项目装饰线添加厚度

android - ViewPager - FragmentStatePagerAdapter 和处理旋转

Android:Fragment 和 ViewPager 总是从第一个位置开始

java - Java中模式匹配中的Stackoverflow