在我的应用程序中有一个viewpager,用于显示一组图片。但是,当我启动活动时,即使我验证了自定义适配器类中的getCount()方法使用调试器返回1,viewpager也没有显示任何内容。
我的适配器布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_picture"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id ="@+id/iv_picture"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Picture"/>
</LinearLayout>
我的ViewPager布局:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vp_pictures"
android:visibility="visible"/>
我的自定义适配器类:
public class PicturePagerAdapter extends PagerAdapter{
Context context;
LayoutInflater inflater;
ArrayList<Bitmap> data;
ViewPager pager;
private boolean deleteEnable;
public PicturePagerAdapter(Context ctx, ArrayList<Bitmap> data, ViewPager p, boolean delete_enable){
this.context = ctx;
this.data = data;
this.pager = p;
this.deleteEnable = delete_enable;
}
@Override
public int getCount() {
return data.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout)object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
ImageView picture;
LinearLayout layout_picture;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.pager_adapter_pictures, null);
picture = (ImageView)v.findViewById(R.id.iv_picture);
layout_picture = (LinearLayout)v.findViewById(R.id.layout_picture);
BitmapDrawable drawable = new BitmapDrawable(data.get(position));
picture.setImageDrawable(drawable);
if(deleteEnable) {
layout_picture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder b = new AlertDialog.Builder(context);
b.setTitle(context.getResources().getString(R.string.actions_title));
b.setMessage(context.getResources().getString(R.string.actions_picture));
b.setNegativeButton(context.getResources().getString(R.string.delete_picture), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
removeBitmapAt(pager, position);
}
});
}
});
}
container.addView(v);
return v;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager)container).removeView((LinearLayout) object);
}
public void addBitmap(Bitmap map){
pager.setAdapter (null);
this.data.add(map);
pager.setAdapter(this);
}
public void removeBitmapAt(ViewPager pager, int index){
pager.setAdapter (null);
this.data.remove(index);
pager.setAdapter (this);
}
最后,我在我的活动中设置了viewpager:
this.picturePager = (ViewPager) findViewById(R.id.vp_pictures);
this.pagerAdapter = new PicturePagerAdapter(context, activityController.getCurrentBook().getPictures(), picturePager, false);
this.picturePager.setAdapter(this.pagerAdapter);
Toast.makeText(this, "Size:"+picturePager.getAdapter().getCount(),Toast.LENGTH_LONG).show();
上面代码中的Toast()将返回1,这是正确的。但是ViewPager没有显示在我的活动中,请帮忙。
答案 0 :(得分:1)
在viewpager xml中设置android:layout_height="match_parent"
。