Android 使用视图适配器类滑动图像
我试着将图像连同文本和tts一起滑动。我使用了ViewPageAdapter。 代码是 xml: 以上代码正确地扫描图像。问题是我想显示文本以及tts。我已经尝试过在Adapter类中显示文本的代码,但它不起作用:Android 使用视图适配器类滑动图像,android,image,view,swipe,adaptor,Android,Image,View,Swipe,Adaptor,我试着将图像连同文本和tts一起滑动。我使用了ViewPageAdapter。 代码是 xml: 以上代码正确地扫描图像。问题是我想显示文本以及tts。我已经尝试过在Adapter类中显示文本的代码,但它不起作用: public Object instantiateItem(View collection, int position) { LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystem
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;
}
请帮忙
谢谢,
我建议您使用
FragmentStatePagerAdapter
或FragmentPagerAdapter
。这些适配器有一个getItem()
,它返回一个片段
;您可以为片段
创建一个xml,其中包含图像视图
和文本视图
发布您的动物.xml
文件。另一点,如果您想要setScaleType
,您应该使用setImageResource
,不要使用setImageBackgroundResource
。上面的xml代码来自animal.xml我看到xml文件有ViewPager,没有ImageView。我遗漏了什么吗?实际上我已经尝试了两种解决方案,一种是使用ViewPager,另一种是使用ImageView。您可以看到ImageView的注释。我也相应地粘贴了两者的代码。。。。
<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>
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);
}
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;
}