在viewpager中setRefreshing(false)之后,SwiperefreshLayout指示继续显示?

时间:2016-02-26 14:25:27

标签: android android-viewpager swiperefreshlayout

我使用刷卡刷新布局来刷新数据,但问题是刷新指示器连续显示刷新,即使我在viewpager中设置了setRefreshing(false)?

这是我的代码:

private static class DayPagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener,
             SwipeRefreshLayout.OnRefreshListener
    {
        private Context context;
        private boolean isDragging;
        private final ActionsState actionsState;
        private int currentPageIndex;
        private ListView listView;
        private boolean isWeekChanging;
        public ActionsListAdapter listAdapter;
        private SwipeRefreshLayout swipeRefreshLayout;
        private final List<String> DAYS_LIST = ImmutableList.of(
                "SUN",
                "MON",
                "TUE",
                "WED",
                "THU",
                "FRI",
                "SAT"
        );

    public DayPagerAdapter(Context context, ActionsState actionsState)
    {
        this.context = context;
        this.actionsState = actionsState;
        listAdapter = new ActionsListAdapter(actionsState);
        //save the current page index to determine the left and right swipe
        currentPageIndex = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(R.layout.actions_days_pager_item, container, false);
        listView = (ListView) layout.findViewById(R.id.actions_list_view);
        swipeRefreshLayout = (SwipeRefreshLayout) layout.findViewById(R.id.actions_swipe_refresh_container);
        swipeRefreshLayout.setOnRefreshListener(this);
        listAdapter.bindToView(listView);
        if (Throttle.REFRESH_CUSTOMER_TASKS_ON_ACTIONS_OPEN.shouldRun())
        {
            refreshCustomerTasksForCurrentDate();
        }

        container.addView(layout);
        return layout;
    }

    @Override
    public int getCount()
    {
        return DAYS_LIST.size();
    }

    @Override
    public CharSequence getPageTitle(int position)
    {
        return DAYS_LIST.get(position);
    }

    @Override
    public boolean isViewFromObject(View view, Object object)
    {
        return object == view;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
    {
        int nextPosition = position + 1;
        if (isDragging)
        {
            if (positionOffset > 0.0001 && nextPosition < getCount())
            {
                isWeekChanging = false;
            }
            else
            {
                isWeekChanging = true;
            }

        }
    }

    @Override
    public void onPageSelected(int position)
    {
        actionsState.invalidateOptionMenu();
        if (isWeekChanging)
        {
            isWeekChanging = false;
            listAdapter.recalculateListItems();
        }
        else if (position > getCurrentPageIndex())
        {
            if (listView != null)
            {
                actionsState.setLocalDate(actionsState.getLocalDate().plusDays(position - getCurrentPageIndex()));
                listAdapter.recalculateListItems();
            }
        }
        else if (getCurrentPageIndex() > position)
        {
            if (listView != null)
            {
                actionsState.setLocalDate(actionsState.getLocalDate().minusDays(getCurrentPageIndex() - position));
                listAdapter.recalculateListItems();
            }
        }

        currentPageIndex = position;
    }

    public final int getCurrentPageIndex()
    {
        return currentPageIndex;
    }

    @Override
    public void onPageScrollStateChanged(int state)
    {
        if (isDragging && getCurrentPageIndex() == 0 && isWeekChanging)
        {
            isDragging = false;
            actionsState.setLocalDate(actionsState.getLocalDate().minusDays(1));
            actionsState.setCurrentItem(6);
        }
        else if (isDragging && getCurrentPageIndex() == 6 && isWeekChanging)
        {
            isDragging = false;
            actionsState.setLocalDate(actionsState.getLocalDate().plusDays(1));
            actionsState.setCurrentItem(0);
        }
       // actionsState.getSwipeRefreshLayout().setEnabled(state == ViewPager.SCROLL_STATE_IDLE);
        isDragging = state == ViewPager.SCROLL_STATE_DRAGGING;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object)
    {
        container.removeView((View) object);
    }

    public void stopWeekChanging()
    {
        this.isWeekChanging = true;
    }

    public void scrollToFirstBooleanTask()
    {
        listView.setSelection(listAdapter.getPositionOfFirstBooleanTask());
    }

    @Subscribe
    public void onFoodLogUpdated(FoodLogUpdatedEvent event)
    {
        if (event.getDateRange().includesDate(actionsState.getLocalDate()))
        {
            if (listAdapter != null)
            {
                // this is weaker than recalculateListItems, and is used because an update to the food log can't add/remove items
                notifyDataSetChanged();
            }
        }
    }

    @Subscribe
    public void onFitbitLinkChanged(FitbitLinkChangedEvent event)
    {
        if (listAdapter != null)
        {
            listAdapter.recalculateListItems();
        }
    }

    @Subscribe
    public void onIHealthAuthUpdated(IHealthAuthUpdatedEvent event)
    {
        if (listAdapter != null)
        {
            listAdapter.recalculateListItems();
        }
    }

    @Subscribe
    public void onScheduledCallsChanged(ScheduledCallsChangedEvent event)
    {
        if (listAdapter != null)
        {
            listAdapter.recalculateListItems();
        }
    }

    @Subscribe
    public void onCustomerTaskUpdated(CustomerTaskUpdatedEvent event)
    {
        if (swipeRefreshLayout != null)
        {
            swipeRefreshLayout.setRefreshing(false);
        }

        if (event.getDateRange().includesDate(actionsState.getLocalDate()))
        {
            if (listAdapter != null)
            {
                listAdapter.recalculateListItems();
            }
        }
    }

    @Override
    public void onRefresh()
    {
        refreshCustomerTasksForCurrentDate();
    }

    private void refreshCustomerTasksForCurrentDate()
    {
        swipeRefreshLayout.setRefreshing(true);
        Vida.getCustomerTaskManager().forceRefreshCustomerTasksForDate(actionsState.getLocalDate());
    }
}

提前致谢..

1 个答案:

答案 0 :(得分:0)

似乎你在这个方法中刷新了在从这个方法加载数据后刷新假。

private void refreshCustomerTasksForCurrentDate()
    {
        swipeRefreshLayout.setRefreshing(true);
        Vida.getCustomerTaskManager().forceRefreshCustomerTasksForDate(actionsState.getLocalDate());

//todo: swipeRefreshLayout.setRefreshing(false); after data load
    }