简体   繁体   English

在首次加载ViewPager时显示内容

[英]Show content on first load of ViewPager

I'm not sure how else to word the title, but the viewpager in my application works as it should - instantiating the current view along with left and right views. 我不确定标题该如何写,但是应用程序中的viewpager可以正常工作-实例化当前视图以及左右视图。

What I would like to know is if I can display content of a non-viewpager element (in this case a listview) on just the first load of the viewpager and then update accordingly as the viewapager scrolls. 我想知道的是,是否可以仅在首次加载viewpager时显示非viewpager元素(在本例中为listview)的内容,然后在viewapager滚动时相应地进行更新。

Relevant code: 相关代码:

_calendar.set(Calendar.MONTH, m);
_calendar.set(Calendar.YEAR, y);
holdTodaysDate.set(Calendar.MONTH, m);
holdTodaysDate.set(Calendar.YEAR, y);

_calendar = holdTodaysDate;

_calendar.add(Calendar.MONTH, offset-1); 
month = _calendar.get(Calendar.MONTH)+1;
year = _calendar.get(Calendar.YEAR);
Log.d("Calendar", "Month Year" + month + " " + year);

eventsSummary.onEventsNavigation(month, year, date_value);

--calendar adapter--

The problem is eventsSummary (non-viewpager element) has to load using the initial month value when the viewpager loads, but since the viewpager loads thrice, month+1 is displayed in eventsSummary . 问题在于,当viewpager加载时, eventsSummary (非viewpager元素)必须使用初始月份值进行加载,但是由于viewpager加载三次,因此在eventsSummary显示month + 1。

For example, February is the current month and it does display February the first time the viewpager loads as I can see in the log, but the viewpager instantiates the next and previous views too - so January is loaded and March. 例如,2月是当前月份,并且正如我在日志中看到的那样,它确实会在第一次加载viewpager时显示2月,但是viewpager也会实例化下一个和上一个视图-因此加载了一月和三月。 The eventsSummary finally displays the events for March while the main calendar (which is the ViewPager element) displays February. eventsSummary最终显示3月的事件,而主日历(这是ViewPager元素)显示2月。

How can I get eventsSummary to display the events for February, rather than March and then update its view with the month the user has swiped to? 如何获取事件eventsSummary以显示2月而不是3月的事件,然后用用户刷卡到的月份更新其视图?

I'm passing information between the fragments eventsSummary and the Calendar ViewPager container using the approach shown here . 我正在使用此处显示的方法在片段eventsSummary和Calendar ViewPager容器之间传递信息。

Am I correct in understanding that you are setting the date in each fragment's onCreate() ? 我是否理解您要在每个片段的onCreate()中设置日期是正确的?

Perhaps you should instead observe what page the ViewPager is displaying, and trigger events through an OnPageChangeListener . 也许您应该改为观察ViewPager显示的页面,并通过OnPageChangeListener触发事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM