2012-12-01 25 views
4

我使用viewpager類來顯示圖像集而不是圖庫類,因爲它的棄用和定製它通過使用下面的代碼來顯示文本,問題是相同的文本顯示所有圖像,即時試圖得到的是:不同的文字爲每個圖像,解釋它,可以說我們有5個圖像,它必須有5個不同的文字,每個人都描述它的形象。圖像viewpager中的每個圖像的不同文本

任何建議將理解的,由於

的代碼:

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>      

回答

6

你可以傳遞一個字符串數組到您的適配器對應於每個圖像.. 前。

在活動中聲明你的字符串數組與您的圖像列表..

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

然後分配來自陣列的文本值

​​
+0

確定,但如何很多正常使用指定每個圖像,這意味着每個圖像都會有不同的文字比其他圖像的某些文字,感謝 –

+0

我編輯我的答案,現在我明白你的問題越好.. – dymmeh

+0

感謝,+1投票和接受。 –

相關問題