问题
I want to implement a Row
from the Details screen of the Leanback
library into a customized screen. The row will be the one below. I have already implemented the HorizontalGridView
and have managed to get the items to be shown.
My layout:
<android.support.v17.leanback.widget.HorizontalGridView
android:id="@+id/detail_related"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
My Java:
RecyclerView.Adapter mAdapter = new SimilarAssetsAdapter();
mBinding.detailRelated.setAdapter(mAdapter);
The problem I am encountering is that I cannot focus on any of the items but the focus is on the whole HorizontalGridView
. How do I solve this?
回答1:
If your view is a custom one, make sure you add the following to your customized view to get focus:
setFocusable(true);
setFocusableInTouchMode(true);
回答2:
The problem in custom view is you need to add the focus change listener in the java code.Add the focus listener inside the adapter to the root view of the adapter items.
Add focusable for the rootview in the oncreate of the adapter items.
yourView.setFocusable(true);
yourView.setFocusableInTouchMode(true);
Then in the onBind add the focuschangeListener.
yourRootView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
//focus gained
}else {
//focus lost
}
}
});
回答3:
To solve this, use a VerticalGridView
instead of a HorizontalGridView
.
<android.support.v17.leanback.widget.VerticalGridView
android:id="@+id/detail_related"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
To set the ArrayObjectAdapter
to the VerticalGridView
, use an ItemBridgeAdapter
.
ItemBridgeAdapter adapter = new ItemBridgeAdapter();
adapter.setAdapter(mRowsAdapter);
adapter.setPresenter(new SinglePresenterSelector(new ListRowPresenter()));
mBinding.detailRelated.setAdapter(adapter);
来源:https://stackoverflow.com/questions/47554485/how-to-write-a-custom-leanbacks-verticalgridview-in-android-tv