Note that there are some explanatory texts on larger screens.

plurals
  1. POListView not drawing every listitem
    primarykey
    data
    text
    <p>I am having a problem and I really can't find what's causing it. </p> <p>I have a ListView that has 4 item types (getViewTypeCount() returns 4). View types 0, 1, and 3 are working perfectly. However, the problem with View type 2 is that only some of the items with type 2 are drawn. The View that represents type 2 is a ViewPager. </p> <p>The ViewPagers that are drawn work perfectly. The ViewPagers that are not drawn, just show white. When I try to scroll through that ViewPager, it shows the blue glow at the side to show that you are on the first or last page. The amount of pages is correct every time.</p> <p>The ViewPager contain one NormalSectionFragment and the rest of the ViewPager are pages with a photo on each page PhotoSectionFragments.</p> <p>There are a few things I am sure of:</p> <ul> <li>The onCreateView method of the Fragments in the ViewPager is called every time the ViewPager appears (or should appear) </li> <li>When I scroll all the way down and back up, every ViewPager is drawn and working perfectly</li> </ul> <p>I could solve it by programmatically scrolling down and back up, but I don't think this is the correct way to solve it.</p> <p><img src="https://i.stack.imgur.com/FY6tu.png" alt="Screenshot with item view types"></p> <p>getItemViewType method:</p> <pre><code>@Override public int getItemViewType(int position) { NieuwsItem item = getItem(position); if (item instanceof NormalNieuwsItem) return VIEW_TYPE_NORMAL; else if (item instanceof TeaserNieuwsItem) return VIEW_TYPE_TEASER; else return ((FacebookNieuwsItem) item).isPhoto() ? VIEW_TYPE_FACEBOOK_PHOTO : VIEW_TYPE_FACEBOOK; } </code></pre> <p>Here is the getView() method of the list adapter:</p> <pre><code>@Override public View getView(int position, View convertView, ViewGroup parent) { NieuwsItem item = getItem(position); switch (getItemViewType(position)) { case 0: if (convertView == null) convertView = LayoutInflater.from(context).inflate(R.layout.listitem_nieuwsitem, null); TextView titleView = (TextView) convertView.findViewById(R.id.titleView); TextView subTitleView = (TextView) convertView.findViewById(R.id.subTitleView); titleView.setText(item.getTitle()); subTitleView.setText(item.getSubTitle()); TextView createdAtView = (TextView) convertView.findViewById(R.id.createdAtView); createdAtView.setText(Tools.getDateString(getContext(), ((NormalNieuwsItem) item).getCreatedAt())); return convertView; case 1: if (convertView == null) convertView = LayoutInflater.from(context).inflate(R.layout.listitem_teaseritem, null); TextView teaserTitleView = (TextView) convertView.findViewById(R.id.teaserTitle); ImageView teaserImageView = (ImageView) convertView.findViewById(R.id.teaserImage); teaserTitleView.setText(item.getTitle()); Bitmap bmp = ((TeaserNieuwsItem) item).getImage(); if (bmp != null) teaserImageView.setImageBitmap(bmp); return convertView; case 2: FacebookNieuwsItem fbPhotoItem = (FacebookNieuwsItem) item; if (convertView == null) convertView = LayoutInflater.from(context).inflate(R.layout.listitem_facebookitem_photo, null); ViewPager pager = (ViewPager) convertView.findViewById(R.id.facebookPhotoPager); PhotoPagerAdapter adapter = new PhotoPagerAdapter(fm, fbPhotoItem); pager.setAdapter(adapter); pager.setPageMargin(-40); pager.setCurrentItem(0, true); pager.setOffscreenPageLimit(5); return convertView; case 3: FacebookNieuwsItem fbItem = (FacebookNieuwsItem) item; convertView = LayoutInflater.from(context).inflate(R.layout.listitem_facebookitem, null); TextView content = (TextView) convertView.findViewById(R.id.facebook_content); TextView likeCount = (TextView) convertView.findViewById(R.id.facebook_likecount); TextView commentCount = (TextView) convertView.findViewById(R.id.facebook_commentcount); TextView createdAt = (TextView) convertView.findViewById(R.id.facebook_date); if (fbItem.isPhoto()) content.setText(fbItem.getTitle()); else content.setText(fbItem.getContent()); likeCount.setText(Integer.toString(fbItem.getLikeCount())); commentCount.setText(Integer.toString(fbItem.getCommentCount())); createdAt.setText(Tools.getDateString(getContext(), fbItem.getCreatedAt())); return convertView; default: return null; } } </code></pre> <p>Adapter for the ViewPager:</p> <pre><code>public class PhotoPagerAdapter extends FragmentStatePagerAdapter { private FacebookNieuwsItem item; public PhotoPagerAdapter(FragmentManager fm, FacebookNieuwsItem item) { super(fm); this.item = item; } @Override public Fragment getItem(int i) { Fragment fragment; if (i == 0) fragment = NormalSectionFragment.newInstance(item); else { String url = Tools.PHOTO_URL_PREFIX + item.getPhoto(i - 1) + Tools.PHOTO_URL_SUFFIX; fragment = PhotoSectionFragment.newInstance(url); } return fragment; } @Override public CharSequence getPageTitle(int position) { return Integer.toString(position); } @Override public int getCount() { return item.getPhotoCount() + 1; } } </code></pre> <p>PhotoSectionFragment:</p> <pre><code>public static class PhotoSectionFragment extends Fragment { private ImageView image; private String url; public static PhotoSectionFragment newInstance(String url) { PhotoSectionFragment fragment = new PhotoSectionFragment(); fragment.setUrl(url); return fragment; } private void setUrl(String url) { this.url = url; } public String getUrl() { return url; } public PhotoSectionFragment() { } public void setImage(Bitmap bmp) { Bitmap scaled = Bitmap.createScaledBitmap(bmp, 100, 60, true); bmp.recycle(); image.setImageBitmap(scaled); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d("photo_oncreateview_start", "url: " + url); View view = inflater.inflate(R.layout.fb_photo_item, null); image = (ImageView) view.findViewById(R.id.facebookPhoto); return view; } } </code></pre> <p>NormalSectionFragment:</p> <pre><code>public static class NormalSectionFragment extends Fragment { private FacebookNieuwsItem item; public NormalSectionFragment() { } public static NormalSectionFragment newInstance(FacebookNieuwsItem item) { NormalSectionFragment section = new NormalSectionFragment(); section.setItem(item); return section; } private void setItem(FacebookNieuwsItem item) { this.item = item; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d("reg_oncreateview_start", item.getTitle()); View view = inflater.inflate(R.layout.listitem_facebookitem_viewpager, null); TextView content = (TextView) view.findViewById(R.id.facebook_content); TextView likeCount = (TextView) view.findViewById(R.id.facebook_likecount); TextView commentCount = (TextView) view.findViewById(R.id.facebook_commentcount); TextView createdAt = (TextView) view.findViewById(R.id.facebook_date); content.setText(item.getTitle()); likeCount.setText(Integer.toString(item.getLikeCount())); commentCount.setText(Integer.toString(item.getCommentCount())); createdAt.setText(DateTimeUtils.getInstance(getActivity()).getTimeDiffString(item.getCreatedAt().getTimeInMillis())); view.setClickable(true); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { ((MainActivity) getActivity()).requestNieuwsDetailPage(item); } }); return view; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload