2016-04-04 25 views
0

私のアプリには一連の画像を表示するためのビューページがあります。しかし、私がカスタム・アダプタ・クラスのgetCount()メソッドがデバッガを使用して1を返すことを確認したにもかかわらず、アクティビティを起動すると、ビュー・ページにはコンテンツが表示されません。 Android 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が私の活動に表示されていないので、助けてください。

答えて

1

ビューページャxmlにandroid:layout_height="match_parent"を設定します。

+0

ありがとう、それは完璧に機能しました! – Noodles

関連する問題