0

I do not know how to implement refreshing listview and adding new items to list if it reach end of list in my parent fragment. I think I should explain my fragments structure first, because I found some tutorials and even libraries on githup but could not use them. The hierarchy of my fragments is like below. And I am using fragment pager adapter as implemented in This Question. Please take this in consideration. I start my async task to bring list items from remote server in Parent Fragment and i start my child fragments with this data. After that this data become static until user start the activity again. I do not want that. I need user could refresh the list and also bring new items if scroll reach end of list. I appreciate anybody who knows a tutorial or a piece of code that can inspire me.

MainActivity -> MainFragment --> Child Fragment1
                             --> Child Fragment2

UPDATED :

pieace of code form my MainFragment

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

        ...
        rootView = inflater.inflate(R.layout.fragmentfeed2, container, false);
        ...
         startAsyncTask();// to bring List Object whish will be show in feed list and map
        return rootView;
}

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mViewPager.setAdapter(new PlacesDisplayTypePagerAdapter(getChildFragmentManager()));
        mViewPager.setOffscreenPageLimit(2);
        mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setViewPager(mViewPager);
        mSlidingTabLayout.setHostingFragment(this);
        mSlidingTabLayout.setSelectedIndicatorColors(getActivity().getResources().getColor(R.color.sow_actionbar_bg_color));

        if(getArguments() != null)
        {
            int currentTab = getArguments().getInt("1");
            mViewPager.setCurrentItem(currentTab);
        }
    }

class PlacesDisplayTypePagerAdapter extends FragmentPagerAdapter {
        public PlacesDisplayTypePagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return message(R.string.tab_list);
                case 1:
                    return message(R.string.tab_map);
                default:
                    return message(R.string.tab_list);
            }
        }

        @Override
        public Fragment getItem(int position) {
            Bundle bundle = new Bundle();
            bundle.putParcelableArrayList(Consts.bundle_adList, (ArrayList<? extends Parcelable>) events);
            bundle.putInt("startFrom", startFrom);
            bundle.putInt("listType", listType);
            bundle.putDoubleArray(Consts.bundle_locArray, targetLoc);
            if (position == 0) {
                return FeedListFragment.newInstance(bundle);
            } else
                return FeedMapFragment.newInstance(bundle);
        }
    }

As you can see from my code, Main fragments has 2 children named FeedListFragment and FeedMapFragment. I now how to detect scrool reach end of listview and then star a new asyntask to bring new items and the notify your list adapter. But how my main fragment can manage that? Listfragment can trigger main frament in event of refresh or load new items by getParentFragment. how main fragment will send this fresh data to child fragments whisch was already inflated? Thansk in advance. help me to figure out that.

Community
  • 1
  • 1
eskimoo
  • 71
  • 1
  • 9

1 Answers1

0

Here's my implementation of loading more items when the end of a listview is reached. This should help you get going!

myListView.setOnScrollListener(new OnScrollListener() {
        private int lastVisibleItem;
        private int totalItemCount;
        private boolean isEndOfList;

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            this.totalItemCount = totalItemCount;
            this.lastVisibleItem = firstVisibleItem + visibleItemCount - 1;
            // prevent checking on short lists
            if (totalItemCount > visibleItemCount)
                checkEndOfList();
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // state doesn't matter
        }

        private synchronized void checkEndOfList() {
            // trigger after 2nd to last item
            if (lastVisibleItem >= (totalItemCount - 2)) {
                if (!isEndOfList) {
                    // LOAD MORE ITEMS HERE!
                }
                isEndOfList = true;
            } else {
                isEndOfList = false;
            }
        }
    });
Mr. Kevin Thomas
  • 837
  • 2
  • 13
  • 21