Android TV 开发-->Leanback 中的 BrowseSupportFragment

Posted Kevin_小飞象

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android TV 开发-->Leanback 中的 BrowseSupportFragment相关的知识,希望对你有一定的参考价值。

当启动应用程序时,内容以网格结构对齐。左边的每个标题标题都有一个每个内容行,并且这个标题 - 内容行关系是一对一的。

效果图

具体使用

0. 清单文件中添加

<uses-feature
        android:name="android.software.leanback"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />

1. 添加依赖

implementation 'androidx.leanback:leanback:1.0.0'
implementation 'androidx.leanback:leanback-preference:1.0.0'

2. 布局文件
activity_test.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_browse_fragment"
    android:name="com.hkt.leanbackdemo.leanback.test.BrowseMainFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    tools:deviceIds="tv"
    tools:ignore="MergeRootFrame" />

3. 创建 Presenter

public class GridItemPresenter extends Presenter 
    private static final int GRID_ITEM_WIDTH = 300;
    private static final int GRID_ITEM_HEIGHT = 200;
    private final Context mContext;

    public GridItemPresenter(Context context) 
        mContext = context;
    

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) 
        TextView view = new TextView(parent.getContext());
        view.setLayoutParams(new ViewGroup.LayoutParams(GRID_ITEM_WIDTH, GRID_ITEM_HEIGHT));
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.setBackgroundColor(mContext.getResources().getColor(R.color.default_background));
        view.setTextColor(Color.WHITE);
        view.setGravity(Gravity.CENTER);
        return new ViewHolder(view);
    

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, Object item) 
        ((TextView) viewHolder.view).setText((String) item);
    

    @Override
    public void onUnbindViewHolder(ViewHolder viewHolder) 

    

4. BrowseMainFragment.java

public class BrowseMainFragment extends BrowseSupportFragment 
    private ArrayObjectAdapter mRowsAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setupUIElements();

        loadRows();
    

    private void setupUIElements() 
        setTitle("Hello Android TV!");
        setHeadersState(HEADERS_ENABLED);
        setHeadersTransitionOnBackEnabled(true);

        setBrandColor(getResources().getColor(R.color.fastlane_background));
        setSearchAffordanceColor(getResources().getColor(R.color.search_opaque));
    

    private void loadRows() 
        mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
        /* GridItemPresenter */
        HeaderItem gridItemPresenterHeader = new HeaderItem(0, "GridItemPresenter");
        GridItemPresenter mGridPresenter = new GridItemPresenter(getActivity());
        ArrayObjectAdapter gridRowAdapter = new ArrayObjectAdapter(mGridPresenter);
        gridRowAdapter.add("ITEM 1");
        gridRowAdapter.add("ITEM 2");
        gridRowAdapter.add("ITEM 3");
        mRowsAdapter.add(new ListRow(gridItemPresenterHeader, gridRowAdapter));
        setAdapter(mRowsAdapter);
    


关注我的视频号,每天都有优质视频教学推送。

微信扫一扫下方二维码即可关注:

以上是关于Android TV 开发-->Leanback 中的 BrowseSupportFragment的主要内容,如果未能解决你的问题,请参考以下文章

Android TV 开发-->Leanback 中的 BrowseSupportFragment

如何使用 Leanback 库在 Android TV 中创建顶部导航栏

Android TV (Leanback Launcher) - 来自 android 开发者的软键盘不起作用

Android TV Leanback Exoplayer 视频缩放问题

Aandroid TV 基于Leanback支持最新MD设计的TV开发框架

简化 Android TV / Leanback 的登录