[英]Android: How to implement Google App Indexing Api for FragmentStatePagerAdapter?
I am working on a News reading Android application. 我正在研究新闻阅读Android应用程序。 Whenever the user clicks on any News List item, I open
NewsDetailActivity
and thus want to index that news. 每当用户单击任何“新闻列表”项目时,我都会打开
NewsDetailActivity
,因此希望对该新闻建立索引。 This activity contains a ViewPager
so that the user can swipe and keep on reading more news. 该活动包含一个
ViewPager
以便用户可以滑动并继续阅读更多新闻。 Since the number of articles the user can swipe are not fixed, I have used FragmentStatePagerAdapter for the implementation. 由于用户可以滑动的文章数量不是固定的,因此我使用FragmentStatePagerAdapter来实现。
Now according to the Official Google documentation 现在根据Google的官方文档
Regarding fragments: You structure fragments in the same way as the activity in the example above.
关于片段:片段的结构与上述示例中的活动相同。 But because fragments may execute many times within an activity, or there may be multiple fragments, you should take care to make the API call only once.
但是,由于片段可能在一个活动中执行多次,或者可能有多个片段,因此应注意只进行一次API调用。 Here are some guidelines: If the activity calls the API, then don't call the API again from any fragment within the activity.
以下是一些准则:如果活动调用了API,则不要再从活动中的任何片段中再次调用API。 If the activity doesn't call the API, and you want a fragment to call it instead, then make sure only one fragment calls the API and only one time.
如果活动未调用API,而您想让一个片段调用它,则请确保只有一个片段调用API,并且只有一次。
According to the official doc, if you are using fragments, then only one of the fragments should call the indexing API. 根据官方文档,如果您正在使用片段,则仅其中一个片段应调用索引API。 However in my case, every swipe creates a new
fragment
in the activity
. 但是就我而言,每次滑动都会在
activity
创建一个新fragment
。 And each fragment
contains a news article that I would like to index. 每个
fragment
包含我想索引的新闻文章。 So how should my implementation be like? 那么我的实现应该如何? Right now I can think of only two solutions here
现在我只能想到两个解决方案
Which is the better approach of the two? 两者中哪个更好? Or is there some other approach which can work out well in my case.
或者是否有其他方法可以很好地解决我的问题。
As long as you call the API for a new content (not the same content over and over) it is fine. 只要您为新内容调用API(一遍又一遍不相同的内容)就可以了。 You can have multiple API calls each for different content.
您可以针对不同的内容进行多个API调用。
Cheers, 干杯,
MB MB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.