ViewPager 不显示视图
ViewPager not showing the views
我创建了一个没有片段的视图寻呼机。
这是我的适配器class
public class CustomPagerAdapter extends PagerAdapter {
private ArrayList<POJOThingsToFocus> thingsToFocusArrayList;
private ImageLoader imageLoader;
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
private DisplayImageOptions options;
public CustomPagerAdapter(ArrayList<POJOThingsToFocus> thingsToFocusArrayList) {
this.thingsToFocusArrayList=thingsToFocusArrayList;
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.build();
}
@Override
public int getCount() {
return thingsToFocusArrayList.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater mInflater = (LayoutInflater)
container.getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View layout = mInflater.inflate(R.layout.element_things_to_focus, null);
ImageView ivThingsToFocus = (ImageView) layout.findViewById(R.id.imageViewThingsToFocus);
TextView tvvThingsToFocus = (TextView) layout.findViewById(R.id.textViewThingsToFocus);
imageLoader = ImageLoader.getInstance();;
if (!imageLoader.isInited()) {
imageLoader.init(ImageLoaderConfiguration.createDefault( container.getContext()));
}
imageLoader.displayImage(thingsToFocusArrayList.get(position).getThingsToFocusImage(), ivThingsToFocus, options, animateFirstListener);
tvvThingsToFocus.setText(thingsToFocusArrayList.get(position).getThingsToFocusDescription());
container.addView(layout);
return container;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View container, Object obj) {
return container == obj;
}
private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}
}
这是我正在充气的布局
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="@+id/imageViewThingsToFocus"
android:layout_width="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewThingsToFocus"
android:layout_width="match_parent"
android:textColor="@android:color/black"
android:text="xxxxx"
android:layout_height="wrap_content" />
</LinearLayout>
但是我在屏幕上看不到任何视图。我可以滑动但是没有显示。
这是我设置适配器的方式
thingsToFocusArrayList = new ArrayList<>();
thingsToFocusArrayList.add(new POJOThingsToFocus(1,"https://1800hocking.files.wordpress.com/2011/07/hi-ohio-logo.jpg","Hey 1"));
thingsToFocusArrayList.add(new POJOThingsToFocus(2,"https://1800hocking.files.wordpress.com/2011/07/hi-ohio-logo.jpg","Hey 2"));
thingsToFocusArrayList.add(new POJOThingsToFocus(3,"https://1800hocking.files.wordpress.com/2011/07/hi-ohio-logo.jpg","Hey 3"));
thingsToFocusArrayList.add(new POJOThingsToFocus(4,"https://1800hocking.files.wordpress.com/2011/07/hi-ohio-logo.jpg","Hey 4"));
viewPager.setAdapter(new CustomPagerAdapter(thingsToFocusArrayList));
已解决
刚好需要
retrun layout;
而不是
retrun container;
我创建了一个没有片段的视图寻呼机。
这是我的适配器class
public class CustomPagerAdapter extends PagerAdapter {
private ArrayList<POJOThingsToFocus> thingsToFocusArrayList;
private ImageLoader imageLoader;
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
private DisplayImageOptions options;
public CustomPagerAdapter(ArrayList<POJOThingsToFocus> thingsToFocusArrayList) {
this.thingsToFocusArrayList=thingsToFocusArrayList;
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.build();
}
@Override
public int getCount() {
return thingsToFocusArrayList.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater mInflater = (LayoutInflater)
container.getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View layout = mInflater.inflate(R.layout.element_things_to_focus, null);
ImageView ivThingsToFocus = (ImageView) layout.findViewById(R.id.imageViewThingsToFocus);
TextView tvvThingsToFocus = (TextView) layout.findViewById(R.id.textViewThingsToFocus);
imageLoader = ImageLoader.getInstance();;
if (!imageLoader.isInited()) {
imageLoader.init(ImageLoaderConfiguration.createDefault( container.getContext()));
}
imageLoader.displayImage(thingsToFocusArrayList.get(position).getThingsToFocusImage(), ivThingsToFocus, options, animateFirstListener);
tvvThingsToFocus.setText(thingsToFocusArrayList.get(position).getThingsToFocusDescription());
container.addView(layout);
return container;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View container, Object obj) {
return container == obj;
}
private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}
}
这是我正在充气的布局
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="@+id/imageViewThingsToFocus"
android:layout_width="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewThingsToFocus"
android:layout_width="match_parent"
android:textColor="@android:color/black"
android:text="xxxxx"
android:layout_height="wrap_content" />
</LinearLayout>
但是我在屏幕上看不到任何视图。我可以滑动但是没有显示。
这是我设置适配器的方式
thingsToFocusArrayList = new ArrayList<>();
thingsToFocusArrayList.add(new POJOThingsToFocus(1,"https://1800hocking.files.wordpress.com/2011/07/hi-ohio-logo.jpg","Hey 1"));
thingsToFocusArrayList.add(new POJOThingsToFocus(2,"https://1800hocking.files.wordpress.com/2011/07/hi-ohio-logo.jpg","Hey 2"));
thingsToFocusArrayList.add(new POJOThingsToFocus(3,"https://1800hocking.files.wordpress.com/2011/07/hi-ohio-logo.jpg","Hey 3"));
thingsToFocusArrayList.add(new POJOThingsToFocus(4,"https://1800hocking.files.wordpress.com/2011/07/hi-ohio-logo.jpg","Hey 4"));
viewPager.setAdapter(new CustomPagerAdapter(thingsToFocusArrayList));
已解决
刚好需要
retrun layout;
而不是
retrun container;