在ScrollView中延迟加载ViewPagers

Rob*_*Rob 6 android listview lazy-loading scrollview android-viewpager

我试图将多个膨胀ViewPagersScrollView每个ViewPager使用FragmentStatePagerAdapter的地方.

ViewPager有三个组成片段的ImageViews每一页.

我使用UniversalImageLoader进行图像的异步加载,但我无法获得延迟加载.

问题是,当我的应用程序正在膨胀时,让每个人说10 ViewPagersImageViews,每个3 个TripplePosterFragment1,另外10个ViewPagers用3个ImageViews,TripplePosterFragment2因为mPager.setOffscreenPageLimit(1)踢了.

在我看来,总共有60个异步请求被触发progressLayout.

MainFragment用以下方法:

private void renderSubCarousels(ArrayList<Carousel> carousels) {

    LayoutInflater mInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int i = 1;

    LinearLayout subCarouselLayout = (LinearLayout) mScrollView.findViewById(R.id.index_subcategory_carousel_container);

    for (final Carousel carousel : carousels) {

        RelativeLayout carouselWrapper = (RelativeLayout) mInflater.inflate(R.layout.main_pager_layout, null);
        LinearLayout mContainer = (LinearLayout) carouselWrapper.findViewById(R.id.main_pager_container);
        mContainer.setId(Constants.BASEID_LINEARLAYOUT + i);

        ViewPager mPager = (ViewPager) mContainer.findViewById(R.id.main_pager);   
        mPager.setId(Constants.BASEID_VIEWPAGER + i);
        mPager.setAdapter(new PagerPosterAdapter(getFragmentManager(),Constants.TRIPLE_POSTERS, carousel));
        subCarouselLayout.addView(carouselWrapper);
        i++;
    }
    progressLayout.setVisibility(View.GONE);
}
Run Code Online (Sandbox Code Playgroud)

我的FragmentStatePagerAdapter:

public class PagerPosterAdapter extends FragmentStatePagerAdapter {
    private int mPageSize;
    private Carousel mCarousel;

    public PagerPosterAdapter(FragmentManager fm, int pageSize, Carousel carousel) {
        super(fm);
        this.mPageSize = pageSize;
        this.mCarousel = carousel;
    }

    @Override
    public int getCount() {
        int maxPageIndex = (int) Math.ceil((float) mCarousel.getAssets().size() / mPageSize);     
        return maxPageIndex;
    }

    @Override
    public Fragment getItem(int position) {
        ArrayList<Asset> assets = mCarousel.getAssets();
        int from = position*mPageSize;
        int to = (position+1)*(mPageSize);
        if(to > assets.size()-1)
            to = assets.size();

        return (TripplePosterFragment.newInstance(assets.subList(from,  to)));
    }
}
Run Code Online (Sandbox Code Playgroud)

TripplePosterFragment其中ImageView的渲染发生:

private void initViews() {

    ImageView mFirstImageView = (ImageView) mLinearLayout.findViewById(R.id.first_pager_poster);        
    ImageView mSecondImageView = (ImageView)mLinearLayout.findViewById(R.id.second_pager_poster);
    ImageView mThirdImageView = (ImageView)mLinearLayout.findViewById(R.id.third_pager_poster);

    for(int i = 0;i < assets.size(); i++){

        ImageView image = null;

        switch(i){
        case 0:
            image = (ImageView) mLinearLayout.findViewById(R.id.first_pager_poster);
            break;      
        case 1:
            image = (ImageView) mLinearLayout.findViewById(R.id.second_pager_poster);
                break;
        case 2:
            image = (ImageView) mLinearLayout.findViewById(R.id.third_pager_poster);
            break;
        default:
            continue;   
        }

        //The ScrollView in MainFragment

        CustomScrollView mParentScrollView = (CustomScrollView)getActivity().findViewById(R.id.index_scrollviewparent);

        Rect scrollBounds = new Rect();
        mParentScrollView.getHitRect(scrollBounds);
        if (image.getLocalVisibleRect(scrollBounds)) {
            // Any portion of the imageView, even a single pixel, is within the visible window
            ImageLoader.getInstance().displayImage(assets.get(i).getPoster(), image);
            System.out.println("Im within");
        else {
            // NONE of the imageView is within the visible window
            System.out.println("Im NOT within");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要检查图像是否可见并在它们出现时加载它们.但是System.out.println("Im NOT within");在垃圾邮件中Log.我确定有一种方法可以做到这一点,但我需要一些帮助.

我已经尝试过一种快速的方法,在ListView中做大量的肉,但它不是最佳的,至少在一些阅读后,ListView内的ViewPager

UPDATE

与以前相同的核心问题,但使用新方法Horizo​​ntalListViews Pagead的instad.

ListViews似乎是要走的路.通过使用ArrayAdapterListView多个HListViews延迟加载常规垂直来填充每个新行的垂直图像,并按照预期水平放置每个图像.

问题: 水平位置丢失.它horizontalAdapter从循环视图继承到下一个列表项.这会将新列表项定位到回收列表项的位置.

public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    View view = convertView;

    if (view == null) {
        holder = new ViewHolder();
        view = mInflater.inflate(R.layout.carousel_frame_layout, null);
        holder.carouselList = (HListView) view.findViewById(R.id.hListView);
        view.setTag(holder);
    }
    else {
        holder = (ViewHolder) view.getTag();
    }
    Carousel mCarousel = mData.get(position);

    // Problem: Re-uses adapters from convertview
    if (holder.carouselList.getAdapter() == null) {
        SubCategoryCarouselAdapter horizontalAdapter = new SubCategoryCarouselAdapter(mContext, mCarousel.getAssets());
        holder.carouselList.setAdapter(horizontalAdapter);
    } else {
        ((SubCategoryCarouselAdapter) holder.carouselList.getAdapter()).update(mCarousel.getAssets());
    }

    return view;
}
Run Code Online (Sandbox Code Playgroud)

解决方案可能是将内部保存horizontalAdapter到指定的列表项位置,但我不知道如何在HListView回收本身时执行此操作.

更新3

找到了有效的解决方案HListViews.但我仍然要测试这是否适用ViewPagers.

addHeaderViewListView每个内部使用父级时,它的水平和垂直延迟加载图像HListView.我在旋转木马上使用了Horizo​​ntalVariableListViewUniversalImageLoader.

LayoutInflater mInflater = (LayoutInflater) getActivity().getSystemService
            (Context.LAYOUT_INFLATER_SERVICE);

for (final Carousel carousel : carousels) {
    View carouselWrapper = mInflater.inflate(R.layout.carousel_sub_category_layout, null);
    listView.addHeaderView(carouselWrapper);
    listView.setAdapter(null);

    HListView horizontalList = (HListView) carouselWrapper.findViewById(R.id.hListView);
    SubCategoryCarouselAdapter mAdapter = new SubCategoryCarouselAdapter(getActivity(), carousel.getAssets());
    horizontalList.setAdapter(mAdapter);
}
Run Code Online (Sandbox Code Playgroud)

Cha*_*sit 3

为什么不使用ListView而不是ScrollView呢?它具有回收视图的功能,因此只有那些当前在屏幕上可见的寻呼机才会开始加载。否则,您应该对 ScrollView 进行子类化,以检查 Pager 现在是否可见