我正在尝试将自定义View设置为VerticalGridFragment的标题

这是我创建的类:

public class MainTitleView extends RelativeLayout implements TitleViewAdapter.Provider {

    @BindView(R.id.top_logo)
    ImageView topLogo;

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

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

        @Override
        public void setBadgeDrawable(Drawable drawable) {
        }

        @Override
        public void updateComponentsVisibility(int flags) {
        }
    };

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

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

    public MainTitleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        View root  = LayoutInflater.from(context).inflate(R.layout.main_title, this);
        ButterKnife.bind(this, root);
        topLogo.setImageResource(R.drawable.ic_logo_actionbar_white);
    }

    public void setTitle(CharSequence title) {
        if (title != null) {
            topLogo.setVisibility(View.VISIBLE);
        }
    }

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


然后在我的VerticalGridFragment中:

setTitleView(new MainTitleView(getActivity()));


这行不通。我可以在调试器中看到调用了构造函数,但看不到任何附加到标头的东西。我找不到如何使用此TitleViewAdapter.Provider的任何示例。

请帮忙。谢谢。

最佳答案

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(android.support.v17.leanback.R.layout.lb_vertical_grid,
            container, false);
    ViewGroup gridFrame = (ViewGroup) root.findViewById(android.support.v17.leanback.R.id.grid_frame);
    installTitleView(inflater, gridFrame, savedInstanceState);
    getProgressBarManager().setRootView(root);
    return root;
}

@Override
public View onInflateTitleView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View title = inflater.inflate(R.layout.custom_title, parent, false);
    //initialize your custom title view here
    return title;
}


它为我工作。 R.layout.custom_title仅是ID为@+id/browse_title_group的具有单个视图(com.your.company.MainTitleView)的xml。

07-27 23:04