- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我使用了 android tv leanback 库的 Google 示例项目作为引用。
所以,我的问题是如何在 Android TV 的 BrowseFragment Header 中添加项目(即 Button、ImageView、TextView)以及搜索按钮。
我可以使用下面的代码注释来隐藏搜索按钮,但我无法在添加搜索按钮的同时添加项目。
setOnSearchClickedListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), SearchActivity.class);
startActivity(intent);
}
});
最佳答案
我不确定,但我认为,您需要自定义浏览 fragment 。 您可以在 java 类中扩展 Browse Fragment 然后尝试自定义 它或尝试使其不可见。您可以自定义Row Fragment 和Header Fragment 并创建自定义Frame 布局。
activity_main.xml
<android.support.v17.leanback.widget.SearchOrbView
android:id="@+id/custom_search_orb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="27dp"
android:layout_marginLeft="56dp"
android:layout_gravity="top|left"
android:visibility="gone"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"/>
<FrameLayout
android:id="@+id/header_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="top|left" />
<FrameLayout
android:id="@+id/rows_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|left"
android:layout_marginLeft="300dp" />
</com.ttnd.androidtv.views.CustomFrameLayout>`
CustomRowFragment.java
import android.app.LoaderManager;
import android.content.Intent;
import android.content.Loader;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v17.leanback.app.RowsFragment;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.HeaderItem;
import android.support.v17.leanback.widget.ImageCardView;
import android.support.v17.leanback.widget.ListRow;
import android.support.v17.leanback.widget.ListRowPresenter;
import android.support.v17.leanback.widget.OnItemViewClickedListener;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.Row;
import android.support.v17.leanback.widget.RowPresenter;
import android.support.v4.app.ActivityOptionsCompat;
import android.telecom.Connection;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.Utils;
import com.example.ttnd.demoapptv.R;
import com.ttnd.androidtv.models.Movie;
import com.ttnd.androidtv.presenter.CardPresenter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class CustomRowsFragment extends RowsFragment {
private ArrayObjectAdapter rowsAdapter;
private static String mVideosUrl;
// CustomHeadersFragment, scaled by 0.9 on a 1080p screen, is 600px wide.
// This is the corresponding dip size.
private static final int HEADERS_FRAGMENT_SCALE_SIZE = 300;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = super.onCreateView(inflater, container, savedInstanceState);
int marginOffset = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, HEADERS_FRAGMENT_SCALE_SIZE, getResources().getDisplayMetrics());
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
params.rightMargin -= marginOffset;
v.setLayoutParams(params);
rowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
CardPresenter cardPresenter = new CardPresenter();
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
listRowAdapter.add(new Movie());
HeaderItem header = new HeaderItem(0, "01234");
rowsAdapter.add(new ListRow(header, listRowAdapter));
setAdapter(rowsAdapter);
//v.setBackgroundColor(getRandomColor());
return v;
}
private void loadVideoData() {
}
private int getRandomColor() {
Random rnd = new Random();
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
}
public void refresh() {
getView().setPadding(Utils.convertDpToPixel(getActivity(), -24), Utils.convertDpToPixel(getActivity(), 128), Utils.convertDpToPixel(getActivity(), 300), 0);
}
}
CustomHeaderFragment.java
package android.support.v17.leanback.app;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v17.leanback.R;
import android.support.v17.leanback.widget.FocusHighlightHelper;
import android.support.v17.leanback.widget.ItemBridgeAdapter;
import android.support.v17.leanback.widget.PresenterSelector;
import android.support.v17.leanback.widget.OnItemViewSelectedListener;
import android.support.v17.leanback.widget.Row;
import android.support.v17.leanback.widget.RowHeaderPresenter;
import android.support.v17.leanback.widget.SinglePresenterSelector;
import android.support.v17.leanback.widget.VerticalGridView;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLayoutChangeListener;
import android.widget.FrameLayout;
public class HeadersFragment extends BaseRowFragment {
interface OnHeaderClickedListener {
void onHeaderClicked();
}
interface OnHeaderViewSelectedListener {
void onHeaderSelected(RowHeaderPresenter.ViewHolder viewHolder, Row row);
}
private OnHeaderViewSelectedListener mOnHeaderViewSelectedListener;
private OnHeaderClickedListener mOnHeaderClickedListener;
private boolean mHeadersEnabled = true;
private boolean mHeadersGone = false;
private int mBackgroundColor;
private boolean mBackgroundColorSet;
private static final PresenterSelector sHeaderPresenter = new SinglePresenterSelector(
new RowHeaderPresenter(R.layout.lb_header));
public HeadersFragment() {
setPresenterSelector(sHeaderPresenter);
}
public void setOnHeaderClickedListener(OnHeaderClickedListener listener) {
mOnHeaderClickedListener = listener;
}
public void setOnHeaderViewSelectedListener(OnHeaderViewSelectedListener listener) {
mOnHeaderViewSelectedListener = listener;
}
@Override
VerticalGridView findGridViewFromRoot(View view) {
return (VerticalGridView) view.findViewById(R.id.browse_headers);
}
@Override
void onRowSelected(RecyclerView parent, RecyclerView.ViewHolder viewHolder,
int position, int subposition) {
if (mOnHeaderViewSelectedListener != null) {
if (viewHolder != null && position >= 0) {
Row row = (Row) getAdapter().get(position);
ItemBridgeAdapter.ViewHolder vh = (ItemBridgeAdapter.ViewHolder) viewHolder;
mOnHeaderViewSelectedListener.onHeaderSelected(
(RowHeaderPresenter.ViewHolder) vh.getViewHolder(), row);
} else {
mOnHeaderViewSelectedListener.onHeaderSelected(null, null);
}
}
}
private final ItemBridgeAdapter.AdapterListener mAdapterListener =
new ItemBridgeAdapter.AdapterListener() {
@Override
public void onCreate(ItemBridgeAdapter.ViewHolder viewHolder) {
View headerView = viewHolder.getViewHolder().view;
headerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnHeaderClickedListener != null) {
mOnHeaderClickedListener.onHeaderClicked();
}
}
});
headerView.setFocusable(true);
headerView.setFocusableInTouchMode(true);
if (mWrapper != null) {
viewHolder.itemView.addOnLayoutChangeListener(sLayoutChangeListener);
} else {
headerView.addOnLayoutChangeListener(sLayoutChangeListener);
}
}
};
private static OnLayoutChangeListener sLayoutChangeListener = new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
v.setPivotX(v.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL ? v.getWidth() : 0);
v.setPivotY(v.getMeasuredHeight() / 2);
}
};
@Override
int getLayoutResourceId() {
return R.layout.lb_headers_fragment;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final VerticalGridView listView = getVerticalGridView();
if (listView == null) {
return;
}
if (getBridgeAdapter() != null) {
FocusHighlightHelper.setupHeaderItemFocusHighlight(listView);
}
view.setBackgroundColor(getBackgroundColor());
updateFadingEdgeToBrandColor(getBackgroundColor());
updateListViewVisibility();
}
private void updateListViewVisibility() {
final VerticalGridView listView = getVerticalGridView();
if (listView != null) {
getView().setVisibility(mHeadersGone ? View.GONE : View.VISIBLE);
if (!mHeadersGone) {
if (mHeadersEnabled) {
listView.setChildrenVisibility(View.VISIBLE);
} else {
listView.setChildrenVisibility(View.INVISIBLE);
}
}
}
}
void setHeadersEnabled(boolean enabled) {
mHeadersEnabled = enabled;
updateListViewVisibility();
}
void setHeadersGone(boolean gone) {
mHeadersGone = gone;
updateListViewVisibility();
}
static class NoOverlappingFrameLayout extends FrameLayout {
public NoOverlappingFrameLayout(Context context) {
super(context);
}
@Override
public boolean hasOverlappingRendering() {
return false;
}
}
private final ItemBridgeAdapter.Wrapper mWrapper = new ItemBridgeAdapter.Wrapper() {
@Override
public void wrap(View wrapper, View wrapped) {
((FrameLayout) wrapper).addView(wrapped);
}
@Override
public View createWrapper(View root) {
return new NoOverlappingFrameLayout(root.getContext());
}
};
@Override
void updateAdapter() {
super.updateAdapter();
ItemBridgeAdapter adapter = getBridgeAdapter();
if (adapter != null) {
adapter.setAdapterListener(mAdapterListener);
adapter.setWrapper(mWrapper);
}
if (adapter != null && getVerticalGridView() != null) {
FocusHighlightHelper.setupHeaderItemFocusHighlight(getVerticalGridView());
}
}
void setBackgroundColor(int color) {
mBackgroundColor = color;
mBackgroundColorSet = true;
if (getView() != null) {
getView().setBackgroundColor(mBackgroundColor);
updateFadingEdgeToBrandColor(mBackgroundColor);
}
}
private void updateFadingEdgeToBrandColor(int backgroundColor) {
View fadingView = getView().findViewById(R.id.fade_out_edge);
Drawable background = fadingView.getBackground();
if (background instanceof GradientDrawable) {
background.mutate();
((GradientDrawable) background).setColors(
new int[] {Color.TRANSPARENT, backgroundColor});
}
}
int getBackgroundColor() {
if (getActivity() == null) {
throw new IllegalStateException("Activity must be attached");
}
if (mBackgroundColorSet) {
return mBackgroundColor;
}
TypedValue outValue = new TypedValue();
if (getActivity().getTheme().resolveAttribute(R.attr.defaultBrandColor, outValue, true)) {
return getResources().getColor(outValue.resourceId);
}
return getResources().getColor(R.color.lb_default_brand_color);
}
@Override
void onTransitionStart() {
super.onTransitionStart();
if (!mHeadersEnabled) {
final VerticalGridView listView = getVerticalGridView();
if (listView != null) {
listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
if (listView.hasFocus()) {
listView.requestFocus();
}
}
}
}
@Override
void onTransitionEnd() {
if (mHeadersEnabled) {
final VerticalGridView listView = getVerticalGridView();
if (listView != null) {
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
if (listView.hasFocus()) {
listView.requestFocus();
}
}
}
super.onTransitionEnd();
}
}
CustomFrameLayout.java
package com.ttnd.androidtv.views;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
public class CustomFrameLayout extends FrameLayout {
public interface OnFocusSearchListener {
View onFocusSearch(View focused, int direction);
}
public interface OnChildFocusListener {
boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect);
void onRequestChildFocus(View child, View focused);
}
public CustomFrameLayout(Context context) {
this(context, null, 0);
}
public CustomFrameLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private OnFocusSearchListener mListener;
private OnChildFocusListener mOnChildFocusListener;
public void setOnFocusSearchListener(OnFocusSearchListener listener) {
mListener = listener;
}
public void setOnChildFocusListener(OnChildFocusListener listener) {
mOnChildFocusListener = listener;
}
@Override
protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
if (mOnChildFocusListener != null) {
return mOnChildFocusListener.onRequestFocusInDescendants(direction, previouslyFocusedRect);
}
return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
}
@Override
public View focusSearch(View focused, int direction) {
if (mListener != null) {
View view = mListener.onFocusSearch(focused, direction);
if (view != null) {
return view;
}
}
return super.focusSearch(focused, direction);
}
@Override
public void requestChildFocus(View child, View focused) {
super.requestChildFocus(child, focused);
if (mOnChildFocusListener != null) {
mOnChildFocusListener.onRequestChildFocus(child, focused);
}
}
}
CardPresenter.java
package com.ttnd.androidtv.presenter;
import android.graphics.drawable.Drawable;
import android.support.v17.leanback.widget.ImageCardView;
import android.support.v17.leanback.widget.Presenter;
import android.util.Log;
import android.view.ViewGroup;
import com.bumptech.glide.Glide;
import com.example.ttnd.demoapptv.R;
import com.ttnd.androidtv.models.Movie;
public class CardPresenter extends Presenter {
private static final String TAG = "CardPresenter";
private static int CARD_WIDTH = 313;
private static int CARD_HEIGHT = 176;
private static int sSelectedBackgroundColor;
private static int sDefaultBackgroundColor;
private Drawable mDefaultCardImage;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
Log.d(TAG, "onCreateViewHolder");
sDefaultBackgroundColor = parent.getResources().getColor(R.color.default_background);
sSelectedBackgroundColor = parent.getResources().getColor(R.color.selected_background);
mDefaultCardImage = parent.getResources().getDrawable(R.drawable.ic_launcher);
ImageCardView cardView = new ImageCardView(parent.getContext()) {
@Override
public void setSelected(boolean selected) {
updateCardBackgroundColor(this, selected);
super.setSelected(selected);
}
};
cardView.setFocusable(true);
cardView.setFocusableInTouchMode(true);
updateCardBackgroundColor(cardView, false);
return new ViewHolder(cardView);
}
private static void updateCardBackgroundColor(ImageCardView view, boolean selected) {
int color = selected ? sSelectedBackgroundColor : sDefaultBackgroundColor;
view.setBackgroundColor(color);
view.findViewById(R.id.info_field).setBackgroundColor(color);
}
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
Movie movie = (Movie) item;
ImageCardView cardView = (ImageCardView) viewHolder.view;
Log.d(TAG, "onBindViewHolder");
if (movie.getCardImageUrl() != null) {
cardView.setTitleText(movie.getTitle());
cardView.setContentText(movie.getStudio());
cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
Glide.with(viewHolder.view.getContext())
.load(movie.getCardImageUrl())
.centerCrop()
.error(mDefaultCardImage)
.into(cardView.getMainImageView());
}
}
@Override
public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
Log.d(TAG, "onUnbindViewHolder");
ImageCardView cardView = (ImageCardView) viewHolder.view;
cardView.setBadgeImage(null);
cardView.setMainImage(null);
}
}
电影.java
package com.ttnd.androidtv.models;
import android.os.Parcel;
import android.os.Parcelable;
import java.net.URI;
import java.net.URISyntaxException;
public class Movie implements Parcelable {
private static final String TAG = "Movie";
static final long serialVersionUID = 727566175075960653L;
private static int sCount = 0;
private String mId = "0";
private String mTitle = "Title Here";
private String mDescription = "Description Here";
private String mBgImageUrl = "http://commondatastorage.googleapis.com/android-tv/Sample%20videos/Demo%20Slam/Google%20Demo%20Slam_%2020ft%20Search/bg.jpg";
private String mCardImageUrl = "http://commondatastorage.googleapis.com/android-tv/Sample%20videos/Zeitgeist/Zeitgeist%202010_%20Year%20in%20Review/card.jpg";
private String mVideoUrl = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
private String mStudio = "Studio Here";
private String mCategory = "Category Here";
public Movie() {
}
public Movie(Parcel in){
String[] data = new String[8];
in.readStringArray(data);
mId = data[0];
mTitle = data[1];
mDescription = data[2];
mBgImageUrl = data[3];
mCardImageUrl = data[4];
mVideoUrl = data[5];
mStudio = data[6];
mCategory = data[7];
}
public static String getCount() {
return Integer.toString(sCount);
}
public static void incrementCount() {
sCount++;
}
public String getId() {
return mId;
}
public void setId(String id) {
mId = id;
}
public String getTitle() {
return mTitle;
}
public void setTitle(String title) {
mTitle = title;
}
public String getDescription() {
return mDescription;
}
public void setDescription(String description) {
mDescription = description;
}
public String getStudio() {
return mStudio;
}
public void setStudio(String studio) {
mStudio = studio;
}
public String getVideoUrl() {
return mVideoUrl;
}
public void setVideoUrl(String videoUrl) {
mVideoUrl = videoUrl;
}
public String getBackgroundImageUrl() {
return mBgImageUrl;
}
public void setBackgroundImageUrl(String bgImageUrl) {
mBgImageUrl = bgImageUrl;
}
public String getCardImageUrl() {
return mCardImageUrl;
}
public void setCardImageUrl(String cardImageUrl) {
mCardImageUrl = cardImageUrl;
}
public String getCategory() {
return mCategory;
}
public void setCategory(String category) {
mCategory = category;
}
public URI getBackgroundImageURI() {
try {
return new URI(getBackgroundImageUrl());
} catch (URISyntaxException e) {
return null;
}
}
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] {mId,
mTitle,
mDescription,
mBgImageUrl,
mCardImageUrl,
mVideoUrl,
mStudio,
mCategory});
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(200);
sb.append("Movie{");
sb.append("mId=" + mId);
sb.append(", mTitle='" + mTitle + '\'');
sb.append(", mVideoUrl='" + mVideoUrl + '\'');
sb.append(", backgroundImageUrl='" + mBgImageUrl + '\'');
sb.append(", backgroundImageURI='" + getBackgroundImageURI().toString() + '\'');
sb.append(", mCardImageUrl='" + mCardImageUrl + '\'');
sb.append('}');
return sb.toString();
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Movie createFromParcel(Parcel in) {
return new Movie(in);
}
public Movie[] newArray(int size) {
return new Movie[size];
}
};
}
我在谷歌搜索时发现了一个与此类定制相关的博客。你可以看看下面的网址: https://medium.com/building-for-android-tv
关于android - 在 Android TV 中的 BrowseFragment header 中添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34901464/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!