我有一个工作ViewPager
使用通用图像加载器和ChrisBanes' PhotoView来捏合和缩放图像。在PagerAdapter中,将创建PhotoView并将其添加到instantiateItem()
对象中的ViewGroup。我正在尝试添加TextView
来显示图像说明。膨胀现有的TextView似乎不起作用,因为不显示描述。以下代码中的LogCat没有错误,只是不显示。在我弄乱ViewPager时,我发现TextView不能与ViewPagers ImageView或PhotoView分开布局。如果TextView和ImageView是单独的布局,则文本不会与ImageView同步。所以我的问题是如何动态地将TextView添加到以下代码的“容器”ViewGroup
中?
@Override
public Object instantiateItem(ViewGroup container, int position) {
PhotoView phView = new PhotoView(container.getContext());
// Attach a PhotoViewAttacher, which takes care of all of the
// zooming functionality.
mAttacher = new PhotoViewAttacher(phView);
final ProgressBar spinner = new ProgressBar(container.getContext());
imageLoader.displayImage(images[position], phView, options,
new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
String message = null;
switch (failReason.getType()) {
case IO_ERROR:
message = "Input/Output error";
break;
case DECODING_ERROR:
message = "Image can't be decoded";
break;
case NETWORK_DENIED:
message = "Downloads are denied";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
Toast.makeText(getBaseContext(), message,
Toast.LENGTH_SHORT).show();
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri,
View view, Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
}
});
//--- adding the TextView
View textviewLayout = inflater.inflate(R.layout.pager_t, container,false);
TextView txt = (TextView) textviewLayout.findViewById(R.id.redtext_pagertxml_tv);
txt.setText(imTitlesStr[position]);
container.addView(phView, LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
return phView;
}
答案 0 :(得分:0)
事实证明,动态添加TextView
不是答案。我最终为要使用的FrameLayout
创建了ViewGroup
。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<uk.co.senab.photoview.PhotoView
android:id="@+id/uilpv_pv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/uilpv_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:background="#A0449def"
android:gravity="center"
android:padding="10dp"
android:textColor="@android:color/white"
android:textSize="18sp" />
</FrameLayout>
正如您在XML中看到的那样,我使用的是ChrisBanes' PhotoView
而不是ImageView
,因此在instantiateItem()
中调用PhotoView
就像这样:
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater
.inflate(R.layout.uil_pager_item, view, false);
PhotoView pHview = (PhotoView) imageLayout
.findViewById(R.id.uilpv_pv);
mAttacher = new PhotoViewAttacher(pHview);
imageLoader.displayImage(images[position], pHview, options,
new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
// spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
String message = null;
switch (failReason.getType()) {
case IO_ERROR:
message = "Input/Output error";
break;
case DECODING_ERROR:
message = "Image can't be decoded";
break;
case NETWORK_DENIED:
message = "Downloads are denied";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
Toast.makeText(UILPager.this, message,
Toast.LENGTH_SHORT).show();
// spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri,
View view, Bitmap loadedImage) {
// spinner.setVisibility(View.GONE);
}
});
TextView txt = (TextView) imageLayout.findViewById(R.id.uilpv_tv);
txt.setText(imTitlesStr[position]);
((ViewPager) view).addView(imageLayout, 0);
return imageLayout;
}
对于我的项目,这个解决方案完全按照预期的方式工作。