Android在ViewPager适配器中加载更多Listview

时间:2016-02-07 09:56:49

标签: android

我的ViewPager在我的片段中有4个页面并在页面转换器适配器中对ListView进行了充气,​​它工作正常,现在我为ListView实现了Load More并且Load More正在工作只有当ListView项目滚动结束时才会自动第一次,但是当ListView项目滚动结束时,第二次会自动运行。请任意帮助我解决此问题:)。

以下是我在PagerAdapter中实施加载的代码。

@Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        final View view = getActivity().getLayoutInflater().inflate(R.layout.world_fragment_list, container, false);
        listview = (LoadMoreListView) view.findViewById(R.id.listView);
        listview.setAdapter(wfadapter);
        listview.setOnLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                Log.e("Calling: ", "onLoadMore()");
                                  }
            }
        });
        container.addView(view);
        return view;
    }

2 个答案:

答案 0 :(得分:0)

我按照this教程解决了上述问题。

感谢@meynety帮助我。

答案 1 :(得分:0)

我在很多项目中都这样做,并且它的工作非常好:

首先使用以下代码创建 ViewPager 类:

public class CategoryFragment extends Fragment {

    public static CategoryFragment newInstance() {
        return new CategoryFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_category, container, false);

        initViews(rootView);

        return rootView;
    }

    private void initViews(View rootView) {

        ViewPager mPager = (ViewPager) rootView.findViewById(R.id.vp_pager);
        mPager.setOffscreenPageLimit(4);
        CategoryAdapter mAdapter = new CategoryAdapter(getChildFragmentManager());
        mPager.setAdapter(mAdapter);

        PagerSlidingTabStrip mIndicator = (PagerSlidingTabStrip) rootView.findViewById(R.id.vp_indicator);
        mIndicator.setTypeface(FontHelper.getInstance(getActivity()).getPersianTextTypeface(), 0);
        mIndicator.setViewPager(mPager);
    }

    private class CategoryAdapter extends FragmentPagerAdapter {

        public CategoryAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return AllCategoryFragment.newInstance();
                case 1:
                    return LatestVideoFragment.newInstance();
                case 2:
                    return PopularVideoFragment.newInstance();
                default:
                    return MostLikeVideoFragment.newInstance();
            }
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position){
                case 0:
                    return getResources().getString(R.string.drawer_title_category);
                case 1:
                    return getResources().getString(R.string.drawer_title_latest_video);
                case 2:
                    return getResources().getString(R.string.drawer_title_popular_video);
                default:
                    return getResources().getString(R.string.drawer_title_most_liked_video);
            }
        }
    }
}

然后创建 BaseFragment 类(这里我只放了其中一个):

public abstract class BaseListFragment extends Fragment implements  AdapterView.OnItemClickListener {

    protected boolean loadingMore, callingRequest ,isVisibleHint, firstRequest = true;
    private PagingListView mListView;
    private ProgressView mCircularProgress;
    private ProgressView mLinearProgress;
    private CustomPage mCustomPage;
    private BaseListAdapter mAdapter;
    private int mPageRequest = 1;

    public abstract String getType();

    public abstract String getCategorySlug();

    public abstract String getCategoryId();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_base_list, container, false);

        initViews(rootView);


        if (isVisibleHint && firstRequest) {
            firstRequest = false;
            enable();
        }

        return rootView;
    }



    protected void initViews(View rootView) {

        mLinearProgress = (ProgressView) rootView.findViewById(R.id.pvl_loading);
        mCircularProgress = (ProgressView) rootView.findViewById(R.id.pvc_loading);
        mCircularProgress.start();

        mListView = (PagingListView) rootView.findViewById(R.id.lv_paging);
        mAdapter = new BaseListAdapter(getActivity());
        mListView.setOnItemClickListener(this);


    }



    public void performSearch() {

        callingRequest = true;

        if (!loadingMore) {
            if (mCircularProgress != null)
                mCircularProgress.start();

            mCustomPage.setVisibility(View.GONE);

            if (mAdapter != null)
                mAdapter.removeAllItems();

            mPageRequest = 1;
        }

        if (mPageRequest > 1) {
            mLinearProgress.start();
        }

        ServiceHelper.getInstance().searchQuery(getCategoryId(), getCategorySlug(), null, null, getType(), null, mPageRequest).enqueue(new Callback<List<ShortVideoModel>>() {
            @Override
            public void onResponse(Response<List<ShortVideoModel>> response) {
                callingRequest = false;
                processResponse(response);
            }

            @Override
            public void onFailure(Throwable t) {
                processResponse(null);
            }
        });

    }


    public void resetPerformSearch(boolean callService) {
        loadingMore = false;
        mPageRequest = 1;
        if (callService)
            performSearch();
    }

    private void enable() {
        mListView.setPagingableListener(new PagingListView.Pagingable() {
            @Override
            public void onLoadMoreItems() {
                loadingMore = true;
                if (!callingRequest)
                    performSearch();
            }
        });
        mListView.setAdapter(mAdapter);
        mListView.setHasMoreItems(true);
    }

    private void processResponse(Response<List<ShortVideoModel>> response) {


        if (response != null && response.body() != null && response.body().size() > 0) {
            mListView.onFinishLoading(true, response.body());
        } else if (mPageRequest == 1) {
            //show error
        } else {
            mListView.setHasMoreItems(false);
            loadingMore = false;
        }

        mPageRequest++;
        //stop loading 

    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        startActivity(VideoDetailsActivity.createIntent(getActivity(), mAdapter.getItem(position).getId()));
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            Log.d("Fragments", "fragment visible: " + getClass().getSimpleName());
            isVisibleHint = true;
            if (firstRequest && getView() != null) {
                firstRequest = false;
                enable();
            }
        }
    }
}

现在只需创建扩展 BaseFrgament ;

的类

您还可以查看here到我的 LoadMoreListView