由于galley类已弃用,所以我尝试用viewpager类替换它,我已经创建了图像viewpager 并且运行良好。
现在我想尝试在每个图片下添加文字说明,我搜索了大量没有结果,我尝试使用 LayoutInflater 与自定义布局但没有结束,它没有显示文字。
注意:这是第一次使用viewpager并尝试自定义它,所以我的代码下面试图填充布局并使用viewpager自定义它,可能是错误的或需要更多调整,因为没有经验。
任何建议都将得到赞赏,谢谢。
我的代码:
ImagePager:
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};}
ImagePagerAdapter:
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; }
}
答案 0 :(得分: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; }
答案 1 :(得分:0)
在instantiateItem()
方法中,您创建ImageView
并将其添加到查看寻呼机并返回它。在此方法中,创建一个具有垂直方向的LinearLayout
,然后为描述创建一个ImageView
和一个TextView
,然后将ImageView
和TextView
添加到{ {1}}以及添加LineaLayout
以查看寻呼机并返回LinearLayout
..