gpt4 book ai didi

android - 在 leanback 的标题 View 中使用多个 Orb 按钮或其他按钮

转载 作者:行者123 更新时间:2023-12-05 00:10:01 27 4
gpt4 key购买 nike

是否可以在 Leanback 展示应用的自定义标题 View 中使用多个 SearchOrb 按钮或其他可聚焦按钮?问题是我只能关注一个按钮...

更新:

我做了什么:

有自定义标题 View 类,其中包含搜索球、标题 TextView 、自定义球和模拟时钟。

问题是,当我尝试关注 Search Orb 时,焦点转到自定义 Orb 上......仅此而已。仅关注自定义 Orb。

主搜索 Orb 的点击监听器已设置。我不知道问题出在哪里。如果需要,我可以按需发布其他代码。

感谢您的帮助!

标题 View .xml:

<?xml version="1.0" encoding="utf-8"?>
<com.olegmart.smart.base.CustomTitleView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/browse_title_group"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

自定义标题 View .java:

package com.olegmart.smart.base;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v17.leanback.widget.SearchOrbView;
import android.support.v17.leanback.widget.TitleViewAdapter;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ImageView;

import com.olegmart.smart.R;

public class CustomTitleView extends RelativeLayout implements TitleViewAdapter.Provider {
private final TextView mTitleView;
private final View mAnalogClockView;
private final View mGridOrbView;
private final View mSearchOrbView;

private final TitleViewAdapter mTitleViewAdapter = new TitleViewAdapter() {
@Override
public View getSearchAffordanceView() {
return mSearchOrbView;
}

@Override
public void setTitle(CharSequence titleText) {
CustomTitleView.this.setTitle(titleText);
}

@Override
public void setBadgeDrawable(Drawable drawable) {
//CustomTitleView.this.setBadgeDrawable(drawable);
}

@Override
public void updateComponentsVisibility(int flags) {
/*if ((flags & BRANDING_VIEW_VISIBLE) == BRANDING_VIEW_VISIBLE) {
updateBadgeVisibility(true);
} else {
mAnalogClockView.setVisibility(View.GONE);
mTitleView.setVisibility(View.GONE);
}*/

int visibility = (flags & SEARCH_VIEW_VISIBLE) == SEARCH_VIEW_VISIBLE
? View.VISIBLE : View.INVISIBLE;
mSearchOrbView.setVisibility(visibility);
}

private void updateBadgeVisibility(boolean visible) {
if (visible) {
mAnalogClockView.setVisibility(View.VISIBLE);
mGridOrbView.setVisibility(View.VISIBLE);
mTitleView.setVisibility(View.VISIBLE);

} else {
mAnalogClockView.setVisibility(View.GONE);
mGridOrbView.setVisibility(View.GONE);
mTitleView.setVisibility(View.GONE);
}
}
};

public CustomTitleView(Context context) {
this(context, null);
}

public CustomTitleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public CustomTitleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
View root = LayoutInflater.from(context).inflate(R.layout.custom_titleview, this);
mTitleView = (TextView) root.findViewById(R.id.title_tv);
mAnalogClockView = root.findViewById(R.id.clock);
mGridOrbView = root.findViewById(R.id.grid_orb);

mSearchOrbView = root.findViewById(R.id.search_orb);
}

public void setTitle(CharSequence title) {
if (title != null) {
mTitleView.setText(title);
mTitleView.setVisibility(View.VISIBLE);
mGridOrbView.setVisibility(View.VISIBLE);
mAnalogClockView.setVisibility(View.VISIBLE);
}
}


public void setBadgeDrawable(Drawable drawable) {
if (drawable != null) {
mTitleView.setVisibility(View.GONE);
mGridOrbView.setVisibility(View.VISIBLE);
mAnalogClockView.setVisibility(View.VISIBLE);
}
}

@Override
public TitleViewAdapter getTitleViewAdapter() {
return mTitleViewAdapter;
}
}

custom_titleview.xml:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v17.leanback.widget.SearchOrbView
android:id="@+id/search_orb"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:transitionGroup="true"
android:layout_gravity="center_vertical|start"
android:layout_marginTop="8dp"
android:paddingStart="48dp"
android:focusable="true"
android:nextFocusRight="@+id/grid_orb"
/>

<AnalogClock
android:id="@+id/clock"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding="6dp"
android:layout_alignParentEnd="true"
android:layout_gravity="center_vertical|end"
android:layout_marginEnd="24dp" />

<android.support.v17.leanback.widget.SearchOrbView
android:id="@+id/grid_orb"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="24dp"
android:layout_centerVertical="true"
android:layout_toStartOf="@id/clock"
android:nextFocusLeft="@+id/search_orb"
android:focusable="true"
/>



<TextView
android:id="@+id/title_tv"
android:textAppearance="@android:style/TextAppearance.Large"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_centerVertical="true"
android:layout_toStartOf="@id/grid_orb" />

</merge>

最佳答案

Teng-pao Yu 是正确的....BrowseFragment 中的 OnFocusSearchListener 是焦点变化没有如您预期的那样发生的原因。

先介绍一下背景......

当发生焦点搜索时,它将首先调用当前 ViewGroup 中的 focusSearch 方法。在这种情况下,虽然搜索球体具有当前焦点,并且用户按下方向键将焦点放在不同的方向,但焦点搜索的第一个回调将在您的 CustomTitleView 类中。只有在 CustomTitleView 将 focusSearch 传播到 BrowseFragment 的 OnFocusSearchListener 的父级之后。

这里真正的问题是(在撰写本文时),Leanback BrowseFragment 的 OnFocusSearchListener 已编码,因此您添加到 CustomTitleLayout 的其他 View 实际上永远无法获得焦点。

但是,有一种方法可以在您的 CustomTitleLayout 中聚焦其他 subview ....

如下面的代码示例所示,重写 CustomTitleLayout 中的 focusSearch 方法,并返回自定义标题布局中存在的可聚焦 View (与返回 super.focusSearch 相反)。

请注意,此代码取决于对 CustomTitleView 的 subview 设置 nextFocusLeft 和 nextFocusRight 属性。此外, subview 必须是可聚焦的 View (按钮等)。如果 subview 在默认情况下不可聚焦(类似于 TextView),您将必须手动启用 View 以通过 XML 使用 android:focusable="true" 或在代码中使用 View.setFocusable(true).

public class CustomTitleView extends RelativeLayout implements TitleViewAdapter.Provider {

.... all your custom view stuff....

@Override
public View focusSearch(View focused, int direction) {

View nextFoundFocusableViewInLayout = null;

// Only concerned about focusing left and right at the moment
if (direction == View.FOCUS_LEFT || direction == View.FOCUS_RIGHT) {

// Try to find the next focusable item in this layout for the supplied direction
int nextFoundFocusableViewInLayoutId = -1;
switch(direction) {
case View.FOCUS_LEFT :
nextFoundFocusableViewInLayoutId = focused.getNextFocusLeftId();
break;
case View.FOCUS_RIGHT :
nextFoundFocusableViewInLayoutId = focused.getNextFocusRightId();
break;
}

// View id for next focus direction found....get the View
if (nextFoundFocusableViewInLayoutId != -1) {
nextFoundFocusableViewInLayout = findViewById(nextFoundFocusableViewInLayoutId);
}
}

// Return the found View in the layout if it's focusable
if (nextFoundFocusableViewInLayout != null && nextFoundFocusableViewInLayout.isFocusable()) {
return nextFoundFocusableViewInLayout;
} else {
// No focusable view found in layout...propagate to super (should invoke the BrowseFrameLayout.OnFocusSearchListener
return super.focusSearch(focused, direction);
}
}

@Override
protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
// Gives focus to the SearchOrb first....if not...default to normal descendant focus search
return getSearchAffordanceView().requestFocus() || super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
}
}

关于android - 在 leanback 的标题 View 中使用多个 Orb 按钮或其他按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38169378/

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