1

我有一个问题,我真的找不到是什么原因造成的。

我有一个具有 4 种项目类型的 ListView(getViewTypeCount() 返回 4)。视图类型 0、1 和 3 运行良好。但是,View type 2 的问题是,只绘制了一些 type 2 的项目。表示类型 2 的 View 是 ViewPager。

绘制的 ViewPager 可以完美地工作。未绘制的 ViewPager 仅显示为白色。当我尝试滚动浏览该 ViewPager 时,它会在侧面显示蓝色光晕,表明您位于第一页或最后一页。每次的页数都是正确的。

ViewPager 包含一个 NormalSectionFragment,ViewPager 的其余部分是每个页面上都有一张照片的页面 PhotoSectionFragment。

有几件事我很确定:

  • ViewPager中Fragments的onCreateView方法在每次ViewPager出现(或应该出现)时被调用
  • 当我一直向下滚动并向后滚动时,每个 ViewPager 都被绘制并完美运行

我可以通过以编程方式向下和向上滚动来解决它,但我认为这不是解决它的正确方法。

带有项目视图类型的屏幕截图

getItemViewType 方法:

@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;
}

这是列表适配器的 getView() 方法:

@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;
    }

}

ViewPager 的适配器:

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;
    }

}

PhotoSectionFragment:

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;
    }
}

正常部分片段:

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;
    }
}
4

1 回答 1

0

我通过使用普通视图而不是片段来修复它。不知道这是可能的

于 2013-09-27T12:17:53.030 回答