Leanback BrowseFragment isShowingHeader() 方法行为

Posted

技术标签:

【中文标题】Leanback BrowseFragment isShowingHeader() 方法行为【英文标题】:Leanback BrowseFragment isShowingHeader() method behaviour 【发布时间】:2019-05-11 04:29:59 【问题描述】:

我正在为电视创建一个应用程序。

我的问题是当我按下电视遥控器的右键并从标题片段导航到 GridFragment(VerticalGridSupportFragment) 适配器的第一项,来自 Leanback 库的函数 isShowingHeaders() 返回 true。

即使当时我的 HeaderFragment 被隐藏或不可见。

但是,如果我从 header-fragment 导航到 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);
     

【问题讨论】:

【参考方案1】:

这是参考页面中的说明:

HeadersFragment 可以设置为默认显示或隐藏,也可以完全禁用。详情请见setHeadersState(int)

请参阅reference page 了解更多信息。

您可以检查 BrowseFragment 的 source code 以了解该行为。请看onCreateView方法的实现。

【讨论】:

我不是在尝试 setHeaderState()。我想检查 BrowseSupportFragment 中每个 HeadersTransition 的 isHeaderShowing() 请找到我正在使用的代码 public boolean isInHeadersTransition() boolean header_shown=super.isInHeadersTransition(); isShowingHeaders();返回header_shown; public boolean isShowingHeaders() boolean header_shown=super.isShowingHeaders();返回header_shown; 请发布完整且格式正确的代码。

以上是关于Leanback BrowseFragment isShowingHeader() 方法行为的主要内容,如果未能解决你的问题,请参考以下文章

如何始终在 Leanback BrowseFragment 中显示标题侧栏?

如何在leanback BrowseFragment中实现两个PageRow之间的转换?

在 BrowseFragment 中禁用行缩放/展开

膨胀 android.support.v17.leanback.widget.TitleView 时出错

Android TV:如何通知 Card Presenter 项目更改

Android TV 应用中搜索球旁边的图标