我試圖使用PagerAdapter該視圖尋呼機從可繪製顯示圖片,我已經寫了下面的代碼 -ViewPager與PagerAdapter不工作
//調用到PagerAdapter
public void showAwardsBadgesAlert()
{
// custom dialog
final Dialog dialog = new Dialog(context, R.style.MyAlertDlgTheme);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.awards_badges_dlg_layout);
dialog.setCanceledOnTouchOutside(false);
dialog.setCanceledOnTouchOutside(true);
ViewPager awardsBadgesPager = (ViewPager) dialog.findViewById(R.id.awardsBadgesPager);
AwardsBadgesAdapter awardsBadgesAdapter = new AwardsBadgesAdapter(context, feedObject.getAwardStatistics().getAwardsBdgesList());
awardsBadgesPager.setAdapter(awardsBadgesAdapter);
dialog.show();
}
//下面是我的適配器分類 -
public class AwardsBadgesAdapter extends PagerAdapter
{
private Context context;
private ArrayList<AwardsBadges> awardsList;
public AwardsBadgesAdapter(Context context, ArrayList<AwardsBadges> awardsList)
{
this.context = context;
this.awardsList = awardsList;
}
@Override
public int getCount()
{
if(awardsList != null)
{
return awardsList.size();
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return view == object;
}
@Override
public Object instantiateItem(final View container, final int position)
{
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.awards_badges_dlg_item, null);
ImageView ivAwards = (ImageView) layout.findViewById(R.id.ivAwardsBadges);
AwardsBadges currBadge = awardsList.get(position);
if(currBadge.getImageName()!=null && currBadge.getImageName().equalsIgnoreCase("pink star diamond"))
{
Picasso.with(context).load(R.drawable.pinkstar).into(ivAwards);
}
else if(currBadge.getImageName()!=null && currBadge.getImageName().equalsIgnoreCase("tanzanite"))
{
Picasso.with(context).load(R.drawable.tanzanite).into(ivAwards);
}
else if(currBadge.getImageName()!=null && currBadge.getImageName().equalsIgnoreCase("painite"))
{
Picasso.with(context).load(R.drawable.painite).into(ivAwards);
}
else if(currBadge.getImageName()!=null && currBadge.getImageName().equalsIgnoreCase("taaffeite"))
{
Picasso.with(context).load(R.drawable.taaffeite).into(ivAwards);
}
return layout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
((ViewPager) container).removeView((View) object);
}
@Override
public float getPageWidth(int position)
{
return (0.33f);
}
}
//下面是我的對話框xml--
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/awardsBadgesPager"
android:layout_width="match_parent"
android:layout_height="@dimen/sz_seventy"
android:layout_below="@+id/txtPlace"
android:layout_margin="10dp"
android:background="@color/tipstransperent"
android:padding="10dp"
android:visibility="visible" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
//下面是ViewPager項目xml--
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" >
<ImageView
android:id="@+id/ivAwardsBadges"
android:layout_width="@dimen/sz_fifty"
android:layout_height="@dimen/sz_fifty"
android:layout_centerInParent="true" />
</RelativeLayout>
//下面是屏幕截圖當前如何解釋顯示爲空ViewPager--
當我調試應用程序時,一切似乎都很好,但不知道爲什麼圖像沒有顯示在ViewPager中。請幫助..
感謝您的答覆。我試過,但它給「的方法addView(查看)是未定義的類型查看」錯誤.. –
@DnyaneshM請閱讀第一句話在我的答案。如果你使用了正確的方法,那麼'container.addView(layout)'不需要強制轉換'container'。爲ViewGroup定義'addView()'方法。 –
你跟着我嗎?你所要做的就是將'instantiateItem()'方法的第一個參數從'final View container'改爲'最終的ViewGroup容器'。 –