3

由于厨房类已弃用,所以我尝试用 viewpager 类替换它,我已经创建了我的图像 viewpager并且工作得很好。

现在我试图在每张图片下添加文本描述,我在谷歌上搜索了很多没有结果,我尝试使用带有自定义布局的LayoutInflater但没有任何结果,它不显示文本。

注意:这是第一次使用 viewpager 并尝试自定义它,所以我下面的代码试图膨胀布局并使用 viewpager 自定义它,可能是错误的或需要更多调整,因为没有经验。

任何建议将不胜感激,谢谢。

我的代码:

图像页面:

 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, R.drawable.f,
                                 R.drawable.g, R.drawable.h, R.drawable.i, 
                                 R.drawable.j, R.drawable.k};}

图像页面适配器:

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

    layout.findViewById(R.id.caption);

 ImageView view = new ImageView(activity);                      
    view.setPadding(5, 25, 5, 5);
    view.setScaleType(ScaleType.FIT_START);
    view.setBackgroundColor(Color.RED);
    ((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;   }
                }
4

2 回答 2

2

这里

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

    layout.findViewById(R.id.caption);

 ImageView view = new ImageView(activity);                      
    view.setPadding(5, 25, 5, 5);
    view.setScaleType(ScaleType.FIT_START);
    view.setBackgroundColor(Color.RED);
    ((ViewPager) collection).addView(view, 0);
    return view;   }

cutome_pager像这样设计你的视图。

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/gallery_imageView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<TextView
    android:id="@+id/gallery_imageView_dsc"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="Hello...." />

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.gallery_imageView);
                    im.setBackgroundColor(Color.RED);
                    TextView dsc=(TextView) layout.findViewById(R.id.gallery_imageView_dsc);
                    ((ViewPager) collection).addView(layout, 0);
                    return layout;   }
于 2012-11-28T18:07:51.240 回答
0

instantiateItem()方法中,您创建一个ImageView并将其添加到查看寻呼机并返回它。在此方法中,创建一个LinearLayout垂直方向,然后为描述创建一个ImageView和一个TextView,然后添加ImageViewTextViewLineaLayout和添加LinearLayout到视图寻呼机并返回LinearLayout..

于 2012-11-28T18:07:29.317 回答