简体   繁体   English

Android:如何为FragmentStatePagerAdapter实现Google App索引Api?

[英]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 现在我只能想到两个解决方案

  • Call index api on each fragment. 在每个片段上调用索引api。 But then it will be against the google documentation and maybe app indexing will not work properly in that case. 但这会违反Google文档,在这种情况下,应用索引可能无法正常工作。
  • Do indexing at activity level itself. 在活动级别本身进行索引。 But in this case, the activity should keep a track of all the fragments that were called. 但是在这种情况下,活动应跟踪所有被调用的片段。

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.

相关问题 努力在Android应用程序中实现谷歌方向api - Struggling to implement google directions api in Android App Google App索引API贬值 - Google App indexing API deprication 如何在Android Studio中关闭新生成代码的自动更新:“这是自动生成的,用于实现App Indexing API。”? - How to turn off auto-updating of the new generated code in Android Studio: “This was auto-generated to implement the App Indexing API.”? 如何在 Android 中实现谷歌路线 API - How to implement google directions API in Android 如何在Android应用中实施Google跟踪代码管理器 - How to implement Google Tag Manager In android app Google App Indexing API标题与关键字 - Google app Indexing API title versus keywords 如何使用库项目实现应用索引? - How to implement app indexing with library project? 没有Google Speech API的情况下如何实现语音识别android应用? - How can I implement a Voice Recognition android app without Google Speech API? 如何为我当前的Live Application Android实现应用内更新Google API - How to Implement In-app Update Google API for my current Live Application Android Android Google App索引编制:要编制什么索引? - Android Google App Indexing: What To Index?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM