我尝试将图像连同其文本和 tts 一起滑动。我用过 ViewPageAdapter。代码是
public class ViewPagerAdapter extends PagerAdapter{
Activity activity;
int imageArray[];
String[] arrToDisplay;
public ViewPagerAdapter(Activity act, int[] imgArra, String[] arrayObject) {
imageArray = imgArra;
activity = act;
arrToDisplay = arrayObject;
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView(activity);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
view.setScaleType(ScaleType.FIT_XY);
view.setBackgroundResource(imageArray[position]);
((ViewPager) collection).addView(view, 0);
return view;
}
@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;
}
}
xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical" >
<!--ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/a_elephant" /-->
<android.support.v4.view.ViewPager
android:id="@+id/myfivepanelpager"
android:layout_width="match_parent"
android:layout_height="494dp" />
</LinearLayout>
主.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, arrAnimals1, arrAnimals);
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
上面的代码正确滑动图像。问题是我想连同 tts 一起显示文本。我已经尝试使用此代码在适配器类中显示文本,但它不起作用:
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.animals, null);
ImageView im=(ImageView) layout.findViewById(R.id.imageView1);
im.setBackgroundResource(imageArray[position]);
TextView dsc=(TextView) layout.findViewById(R.id.txtObjName);
dsc.setText("asdfasdf");
((ViewPager) collection).addView(layout, 0);
return layout;
}
请帮忙。
谢谢,伊丁