【问题标题】:Swipe image with view adaptor class使用视图适配器类滑动图像
【发布时间】:2013-12-08 18:17:27
【问题描述】:

我尝试将图像连同其文本和 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>

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

请帮忙。

谢谢, 就在

【问题讨论】:

  • 发布您的animals.xml 文件。还有一点,如果你要setScaleType,你应该使用setImageResource,不要使用setImageBackgroundResource
  • 以上xml代码来自animal.xml
  • 我看到那个xml文件有ViewPager,没有ImageView。我错过了什么吗?
  • 实际上我已经尝试了两种解决方案,一种是使用 ViewPager,另一种是使用 ImageView。你可以看到 ImageView 被评论了。我也相应地粘贴了两者的代码....

标签: android image view swipe adaptor


【解决方案1】:

我建议您使用FragmentStatePagerAdapterFragmentPagerAdapter。这些适配器有一个getItem(),它返回一个Fragment;您可以为 Fragment 创建一个 xml,其中包含一个 ImageView 和一个 TextView

【讨论】:

    猜你喜欢
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    相关资源
    最近更新 更多