问题描述
我正在尝试制作图像传呼机,根据我查找的所有教程,看来我已经将其纠正了,但是我得到的只是一个空白屏幕.适配器的instantiateItem
方法上的断点告诉我正在调用它,并且所有正确的信息都已放入视图中,甚至可以滑动,但是我仍然看不到任何东西.这是我的代码
I'm trying to make an image pager and it looks like I've done it correct according to all the tutorials that I've looked up, but all I get is a blank screen. A breakpoint on the adapter's instantiateItem
method tells me that it is being called and all the right information is getting set into the views, and swiping even works, but I still don't see anything. Here is my code
activity_photos.xml
<RelativeLayout> // I'm not including that code, irrelevant.
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imagePager"
android:background="@android:color/black"/>
</RelativeLayout>
viewpager_itemx.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/imageView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageLabel"
android:textColor="@android:color/white"/>
</LinearLayout>
PhotosActivity.java
final String[] images = getResources().getStringArray(R.array.tips_images);
final String[] labels = getResources().getStringArray(R.array.tips_text);
final ViewPager viewPager = (ViewPager) findViewById(R.id.imagePager);
final ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(PhotoTipsActivity.this, images, labels);
viewPager.setAdapter(viewPagerAdapter);
最后是ViewPagerAdapter.java
and finally, ViewPagerAdapter.java
公共类ViewPagerAdapter扩展了PagerAdapter {
public class ViewPagerAdapter extends PagerAdapter {
private Context context;
private String[] images;
private String[] labels;
public ViewPagerAdapter(Context context, String[] images, String[] labels) {
this.context = context;
this.images = images;
this.labels = labels;
}
@Override
public int getCount() {
return images.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
final View itemView = LayoutInflater.from(context).inflate(R.layout.viewpager_item, container, false);
final ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
final TextView imageLabel = (TextView) itemView.findViewById(R.id.imageLabel);
// Get drawable image.
final int imageId = context.getResources().getIdentifier(images[position], "drawable", context.getPackageName());
imageView.setImageResource(imageId);
imageLabel.setText(labels[position]);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(((LinearLayout) object));
}
}
为什么我没有看到我的图像的任何明显原因?
Any blatantly obvious reason why I'm not seeing my images?
推荐答案
destroyItem()
需要从容器中删除视图的方式相同,instantiateItem()
需要将视图添加到容器中.
The same way destroyItem()
needs to remove the view from the container, instantiateItem()
needs to add the view to the container.
只需添加
container.addView(itemView);
从instantiateItem()
返回之前,您将开始业务.
before returning from instantiateItem()
and you'll be in business.
这篇关于PagerAdapter总是返回空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!