Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用视图适配器类滑动图像_Android_Image_View_Swipe_Adaptor - Fatal编程技术网

Android 使用视图适配器类滑动图像

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

我试着将图像连同文本和tts一起滑动。我使用了ViewPageAdapter。 代码是

xml:

以上代码正确地扫描图像。问题是我想显示文本以及tts。我已经尝试过在Adapter类中显示文本的代码,但它不起作用:

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