我正在为电视创建应用。
我的问题是当我从电视遥控器按向右按钮并从标题片段导航到 GridFragment(VerticalGridSupportFragment) 适配器的第一项功能是Leanback Library中的isShowingHeaders()函数返回true。
即使我的HeaderFragment当时被隐藏或不可见。
但是,如果我从标头片段导航到RowSupportFragment,则第一个行为是预期的,即isShowingHeaders()方法返回false。
请在下面找到代码
public class PageAndListRowFragment extends BrowseSupportFragment {
private static final long HEADER_ID_1 = 1;
private static final String HEADER_NAME_1 = "Page Fragment";
private static final long HEADER_ID_2 = 2;
private static final String HEADER_NAME_2 = "Rows Fragment";
private static final long HEADER_ID_3 = 3;
private BackgroundManager mBackgroundManager;
private ArrayObjectAdapter mRowsAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupUi();
loadData();
mBackgroundManager = BackgroundManager.getInstance(getActivity());
mBackgroundManager.attach(getActivity().getWindow());
getMainFragmentRegistry().registerFragment(PageRow.class,
new PageRowFragmentFactory(mBackgroundManager));
}
private void setupUi() {
setHeadersState(HEADERS_ENABLED);
setHeadersTransitionOnBackEnabled(true);
setBrandColor(getResources().getColor(R.color.fastlane_background));
setTitle("Title goes here");
setOnSearchClickedListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
prepareEntranceTransition();
}
private void loadData() {
mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
setAdapter(mRowsAdapter);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
createRows();
startEntranceTransition();
}
}, 2000);
}
@Override
public boolean isInHeadersTransition() {
boolean header_shown1=super.isInHeadersTransition();
Log.e("header_shown1",header_shown1+"val12");
isShowingHeaders();
return header_shown1;
}
@Override
public boolean isShowingHeaders() {
boolean header_shown=super.isShowingHeaders();
Log.e("header_shown",header_shown+"val12");
return header_shown;
}
private void createRows() {
HeaderItem headerItem1 = new HeaderItem(HEADER_ID_1, HEADER_NAME_1);
PageRow pageRow1 = new PageRow(headerItem1);
mRowsAdapter.add(pageRow1);
HeaderItem headerItem2 = new HeaderItem(HEADER_ID_2, HEADER_NAME_2);
PageRow pageRow2 = new PageRow(headerItem2);
mRowsAdapter.add(pageRow2);
}
private static class PageRowFragmentFactory extends BrowseSupportFragment.FragmentFactory {
private final BackgroundManager mBackgroundManager;
PageRowFragmentFactory(BackgroundManager backgroundManager) {
this.mBackgroundManager = backgroundManager;
}
@Override
public Fragment createFragment(Object rowObj) {
Row row = (Row)rowObj;
mBackgroundManager.setDrawable(null);
if (row.getHeaderItem().getId() == HEADER_ID_1) {
return new SampleFragmentA();
} else if (row.getHeaderItem().getId() == HEADER_ID_2) {
return new SampleFragmentB();
}
throw new IllegalArgumentException(String.format("Invalid row %s", rowObj));
}
}
public static class PageFragmentAdapterImpl extends MainFragmentAdapter<SampleFragmentA> {
public PageFragmentAdapterImpl(SampleFragmentA fragment) {
super(fragment);
}
}
/**
* Simple page fragment implementation.
*/
public static class SampleFragmentA extends VericalGridSupportFragment {
private static final int COLUMNS = 4;
private final int ZOOM_FACTOR = FocusHighlight.ZOOM_FACTOR_SMALL;
private ArrayObjectAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupAdapter();
loadData();
getMainFragmentAdapter().getFragmentHost().notifyDataReady(getMainFragmentAdapter());
}
private void setupAdapter() {
VerticalGridPresenter presenter = new VerticalGridPresenter(ZOOM_FACTOR);
presenter.setNumberOfColumns(COLUMNS);
setGridPresenter(presenter);
CardPresenterSelector cardPresenter = new CardPresenterSelector(getActivity());
mAdapter = new ArrayObjectAdapter(cardPresenter);
setAdapter(mAdapter);
setOnItemViewClickedListener(new OnItemViewClickedListener() {
@Override
public void onItemClicked(
Presenter.ViewHolder itemViewHolder,
Object item,
RowPresenter.ViewHolder rowViewHolder,
Row row) {
Card card = (Card)item;
}
});
}
private void loadData() {
String json = Utils.inputStreamToString(getResources().openRawResource(
R.raw.grid_example));
CardRow cardRow = new Gson().fromJson(json, CardRow.class);
mAdapter.addAll(0, cardRow.getCards());
}
}
/**
* Page fragment embeds a rows fragment.
*/
public static class SampleFragmentB extends RowsSupportFragment {
private final ArrayObjectAdapter mRowsAdapter;
public SampleFragmentB() {
mRowsAdapter = new ArrayObjectAdapter(new ShadowRowPresenterSelector());
setAdapter(mRowsAdapter);
setOnItemViewClickedListener(new OnItemViewClickedListener() {
@Override
public void onItemClicked(
Presenter.ViewHolder itemViewHolder,
Object item,
RowPresenter.ViewHolder rowViewHolder,
Row row) {
}
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createRows();
getMainFragmentAdapter().getFragmentHost().notifyDataReady(getMainFragmentAdapter());
}
private void createRows() {
String json = Utils.inputStreamToString(getResources().openRawResource(
R.raw.page_row_example));
CardRow[] rows = new Gson().fromJson(json, CardRow[].class);
for (CardRow row : rows) {
if (row.getType() == CardRow.TYPE_DEFAULT) {
mRowsAdapter.add(createCardRow(row));
}
}
}
private Row createCardRow(CardRow cardRow) {
PresenterSelector presenterSelector = new CardPresenterSelector(getActivity());
ArrayObjectAdapter adapter = new ArrayObjectAdapter(presenterSelector);
for (Card card : cardRow.getCards()) {
adapter.add(card);
}
HeaderItem headerItem = new HeaderItem(cardRow.getTitle());
return new CardListRow(headerItem, adapter, cardRow);
}} }
答案 0 :(得分:0)
这是参考页中所述的内容:
HeadersFragment可以设置为默认显示或隐藏,也可以完全禁用。有关详细信息,请参见setHeadersState(int)
。
有关更多信息,请参见reference page。
您可以检查BrowseFragment的source code以了解其行为。请查看onCreateView
方法的实现。