不同的文本图像viewpager每个图像 [英] Different text for each image in image viewpager

查看:150
本文介绍了不同的文本图像viewpager每个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用viewpager类来显示图像集,而不是画廊类作为其德precated和定制它表明通过使用下面的code中的文本,问题是相同的文字显示所有图像,什么即时试图得到的是:不同的文字每个图片,这说明它,可以说我们有5个图像,它必须有5个不同的文本每一个描述它的形象

任何意见将AP preciated,谢谢

在code:

ImagePager

 公共类ImagePager延伸活动{

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    ImagePagerAdapter适配器=新ImagePagerAdapter(这一点,imageArra);
    ViewPager myPager =(ViewPager)findViewById(R.id.myimagepager);
    myPager.setAdapter(适配器);
    myPager.setCurrentItem(0);}

私人诠释imageArra [] = {R.drawable.a,R.drawable.b,R.drawable.c,
                              R.drawable.d,R.drawable.e}; }
 

ImagePagerAdapter

 公共类ImagePagerAdapter扩展PagerAdapter {

活动活性;
INT imageArray [];

公共ImagePagerAdapter(活动行为,INT [] imgArra){
    imageArray = imgArra;
    活动=行为;}

公众诠释getCount将(){
    返回imageArray.length;}

公共对象instantiateItem(查看收集,INT位置){
    LayoutInflater充气=(LayoutInflater)collection.getContext
                 ().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    查看布局= inflater.inflate(R.layout.custom_pager,NULL);

    ImageView的IM =(ImageView的)layout.findViewById(R.id.myimage);
    im.setImageResource(imageArray [位置]);

    TextView的TXT =(TextView中)layout.findViewById(R.id.image_text);
    ((ViewPager)集合).addView(布局,0);
       返回布局; }

@覆盖
公共无效destroyItem(查看为arg0,INT ARG1,ARG2对象){
    ((ViewPager)为arg0).removeView((查看)ARG2);
            }

@覆盖
公共布尔isViewFromObject(查看为arg0,对象ARG1){
    返回将arg0 ==((查看)ARG1);
                      }

@覆盖
公共Parcelable saveState和(){
    返回null;
              }}
 

activity_main.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
   机器人:layout_width =FILL_PARENT
   机器人:layout_height =FILL_PARENT
   机器人:方向=垂直>
 < android.support.v4.view.ViewPager
   机器人:ID =@ + ID / myimagepager
   机器人:layout_width =match_parent
   机器人:layout_height =match_parent/>
< / LinearLayout中>
 

custom_pager.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
   机器人:layout_width =match_parent
   机器人:layout_height =match_parent
   机器人:方向=垂直
   机器人:后台=#FFDAB9
   机器人:重力=center_horizo​​ntal>
 < ImageView的机器人:ID =@ + ID / MYIMAGE
   机器人:layout_width =match_parent
   机器人:layout_height =0dp
   机器人:layout_margin =5DP
   机器人:layout_weight =2/>
 < TextView的机器人:ID =@ + ID / Image_Text的
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =0dp
    机器人:文字颜色=#B22222
    机器人:layout_weight =1
    机器人:文本=文字图片/>

 < / LinearLayout中>
 

解决方案

您可以传递字符串数组到您的适配器对应于每个图像.. 恩。

在活动与您的图片列表声明的字符串数组。

 私人诠释imageArra [] = {R.drawable.a,R.drawable.b,R.drawable.c,
                              R.drawable.d,R.drawable.e};

私有String []字符串数组=新的String [] {图片一,图像B,图像C,图像D,图像E};
 

然后实例化适配器时..

  ImagePagerAdapter适配器=新ImagePagerAdapter(这一点,imageArra,字符串数组);
 

在您的适配器:

  INT imageArray [];
的String []字符串数组;
公共ImagePagerAdapter(活动行为,INT [] imgArra,字符串[] stringArra){
    imageArray = imgArra;
    活动=行为;
    字符串数组= stringArra;
}
 

然后从数组分配文本值

  TextView的TXT =(TextView中)layout.findViewById(R.id.image_text);
txt.setText(字符串数组[位置]);
 

I used viewpager class to display sets of images instead of gallery class as its deprecated and customized it to show the text by using the below code , the problem is the same text showed for all images , what im trying to get is : Different text for each image , which explain it , lets say we have 5 images , it must has 5 different text each one describe its image .

any advice will be appreciated , thanks

The code:

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>                       

解决方案

You could pass an array of Strings into your adapter that correspond to each image.. ex.

In the activity declare your string array with your list of images..

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

Then when instantiating your adapter..

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

In your adapter:

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

Then assign the text value from the array

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

这篇关于不同的文本图像viewpager每个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆