gpt4 book ai didi

AndroidTV - 为 X 行数自定义 ListRow。失去焦点

转载 作者:行者123 更新时间:2023-11-29 02:24:47 30 4
gpt4 key购买 nike

我在我的 Android TV 项目中使用了 Leanback 库。我想自定义 ListRow,这样我就可以更改要显示的内容行的数量(默认为 1)。

我遵循了指南:https://corochann.com/browsefragment-listrow-customization-android-tv-application-hands-on-tutorial-18-705.html

我想到的是:

class CustomListRowPresenter : ListRowPresenter() {

override fun onBindRowViewHolder(holder: RowPresenter.ViewHolder, item: Any) {
/* This two line codes changes the number of rows of ListRow */
val numRows = (item as CustomListRow).numRows
(holder as ListRowPresenter.ViewHolder).gridView.setNumRows(numRows)

super.onBindRowViewHolder(holder, item)
}

override fun initializeRowViewHolder(holder: RowPresenter.ViewHolder) {
super.initializeRowViewHolder(holder)

/* Disable Shadow */
shadowEnabled = false
}

companion object {
private val TAG = CustomListRowPresenter::class.java.simpleName
}

}




class CustomListRow : ListRow {
var numRows = 1

constructor(header: HeaderItem, adapter: ObjectAdapter) : super(header, adapter) {}

constructor(id: Long, header: HeaderItem, adapter: ObjectAdapter) : super(id, header, adapter) {}

constructor(adapter: ObjectAdapter) : super(adapter) {}

companion object {

private val TAG = CustomListRow::class.java.simpleName
}

}

和:

override onCreate() {
val adapter = SparseArrayObjectAdapter(CustomListRowPresenter())
setAdapter(adapter)
}

private fun setContent(content: List<Data>) {
content.forEachIndexed { index, data ->
val position = index.toLong()
val headerItem = HeaderItem(position, data.title)
val presenterSelector = getMediaObjectPresenterSelector()
val dataAdapter = ArrayObjectAdapter(presenterSelector)
val listRow = CustomListRow(position, headerItem, dataAdapter)
data.content.forEach { x ->
dataAdapter.add(x)
}
listRow.numRows = 5
dataAdapters.add(dataAdapter)
adapter.set(position.toInt(), listRow)
}
refresh()
}

所以,一切都很好。我得到了我想要的行,但是有一个问题。例如,如果我想显示 2 行,它就完美了。如果我想显示超过 2 个(这样您在 View 中看不到第二个 customListRow),它就没有焦点。基本上,如果我有 2+ 行并且我尝试从左到右从下到上移动,焦点仅在前 2 行上工作完美。什么可能导致此问题以及如何解决?

想要的输出:

enter image description here

数据:

BIG-OBJ:
title: "1"
list-of-small-objs: small1, small2, small3
BIG-OBJ2:
title: "2"
list-of-small-objs: small1, small2, small3
...

最佳答案

请在java中找到我的代码。

这里的 ma​​inList 包含标题名称和将添加到特定标题下的数据列表。

在我的场景中,我想要连续 10 个数据,所以使用 j 的 for loop 我已经做到了,并且在同一标题下添加了多行。

for (int i = 0; i < mainList.size(); i++) {
ArrayList<HashMap<String,String>> listDataMap = (ArrayList<HashMap<String,String>>) mainList.get(i).get("dataList");

if(!listDataMap.isEmpty()){
CardPresenter cardPresenter = new CardPresenter();

String categoryName = (String) mainList.get(i).get("categoryName");
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);

List<Movie> list = MovieList.setupGames(listDataMap,i+2);
for (int j = 1; j <= listDataMap.size(); j++) {
listRowAdapter.add(list.get(j-1));
if (j%10 == 0){
HeaderItem header = new HeaderItem( categoryName);
mAdapter.add(new ListRow(header, listRowAdapter));
categoryName = "";
listRowAdapter = new ArrayObjectAdapter(cardPresenter);
}else if ( j == listDataMap.size() ){
HeaderItem header = new HeaderItem( categoryName);
mAdapter.add(new ListRow(header, listRowAdapter));
}
}
}
}

关于AndroidTV - 为 X 行数自定义 ListRow。失去焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52892051/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com