在 BrowseFragment 之外重用 AndroidTV RowsFragment

Posted

技术标签:

【中文标题】在 BrowseFragment 之外重用 AndroidTV RowsFragment【英文标题】:Reusing AndroidTV RowsFragment outside BrowseFragment 【发布时间】:2015-04-28 14:55:51 【问题描述】:

我正在尝试在 BrowseFragment 之外重用 androidTV Leanback 提供的 RowsFragment,以便在不同的布局上获得类似的行交互外观,但我遇到了我无法做到的 XML 膨胀错误调试和解决。

实现本身与此处实现的类似:https://medium.com/building-for-android-tv,但在博客中,他正在替换 BrowseFragment 中相同类型的不同片段。我的方法有什么根本错误吗?如果是这样,为什么?我一直在阅读 Leanback 资源,但找不到任何我能理解的东西(尽管它是一个大型库),可以将片段耦合到那种程度。

相关代码复现:https://gist.github.com/orbitbot/c9070ed00961c3abe4ca

02-26 12:12:26.950    4465-4527/com.my.client E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: com.my.client, PID: 4465
      android.view.InflateException: Binary XML file line #18: Error inflating class <unknown>
              at android.view.LayoutInflater.createView(LayoutInflater.java:620)
              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
              at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:462)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
              at android.support.v17.leanback.widget.RowContainerView.<init>(RowContainerView.java:48)
              at android.support.v17.leanback.widget.RowContainerView.<init>(RowContainerView.java:37)
              at android.support.v17.leanback.widget.RowPresenter.onCreateViewHolder(RowPresenter.java:169)
              at android.support.v17.leanback.widget.ItemBridgeAdapter.onCreateViewHolder(ItemBridgeAdapter.java:247)
              at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:4121)
              at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3431)
              at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
              at android.support.v17.leanback.widget.GridLayoutManager.getViewForPosition(GridLayoutManager.java:573)
              at android.support.v17.leanback.widget.GridLayoutManager$2.createItem(GridLayoutManager.java:1057)
              at android.support.v17.leanback.widget.StaggeredGrid.appendItemToRow(StaggeredGrid.java:242)
              at android.support.v17.leanback.widget.StaggeredGridDefault.appendItems(StaggeredGridDefault.java:49)
              at android.support.v17.leanback.widget.GridLayoutManager.appendOneVisibleItem(GridLayoutManager.java:1263)
              at android.support.v17.leanback.widget.GridLayoutManager.appendVisibleItems(GridLayoutManager.java:1273)
              at android.support.v17.leanback.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:1554)
              at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1988)
              at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.support.v17.leanback.widget.ScaleFrameLayout.onLayout(ScaleFrameLayout.java:135)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
              at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
              at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1989)
              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1746)
              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5672)
              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:772

【问题讨论】:

我是该系列文章的作者。我不太清楚您要实现什么以及它与我对自定义 BrowseFragment 实现所做的不同。 好的,知道了。乍一看,它确实看起来不错。我要试一试,看看我能不能理解发生了什么。 酷,感谢您的努力!稍后我将在布局中添加更多片段,因此 RowsFragment 只是其中的一部分(大约是下半部分)。 【参考方案1】:

确保您的 ChangePicActivity 在 AndroidManifest.xml 中有正确的主题

android:theme="@style/Theme.Leanback" 添加到相关的 activity 标记中。 像这样

 <activity
        android:name=".ChangePicActivity"
        android:theme="@style/Theme.Leanback"
 >
[...]

【讨论】:

感谢您的提示!当您意识到这一点时很明显,但由于遗留原因,我们在每个活动的基础上都弄乱了主题。

以上是关于在 BrowseFragment 之外重用 AndroidTV RowsFragment的主要内容,如果未能解决你的问题,请参考以下文章

在 BrowseFragment 中禁用行缩放/展开

在 BrowseFragment 中禁用标题

Android TV 在 BrowseFragment 中重新加载数据

BrowseFragment 行中的最后一个元素

BrowseFragment 中 onFocusSearch 上的 NullPointerException?

如何使只有一行可见的 BrowseFragment