You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PlaybackSupportFragment中Presenter的onClickEvent无法触发的解决方案咨询

Fixing onClickEvent Not Triggering in Leanback Presenter for PlaybackSupportFragment

Ah, I've run into this exact issue with Leanback before! The problem here is that you're trying to handle clicks the standard Android way with setOnClickListener, but Leanback has its own event handling system built around focus-based navigation (which makes total sense for TV interfaces). Directly attaching a click listener to your View will get overridden or ignored by the framework.

Here's how to fix it step by step:

1. Remove the direct OnClickListener from your Presenter

Leanback doesn't expect you to handle clicks at the ViewHolder level. Go ahead and delete that setOnClickListener block in your onBindViewHolder method:

@Override
public void onBindViewHolder(ViewHolder viewHolder, Object item) {
    TextView vHolder = (TextView) viewHolder.view;
    final Episode model = (Episode) item;
    vHolder.setText(model.name);
    // Delete the entire setOnClickListener section here
}

2. Use Leanback's built-in click listener in your Fragment

Instead, register a global click listener on your PlaybackSupportFragment (or the adapter's host) that will catch all item clicks. Add this code right after you call setAdapter(superAdapter):

setOnItemViewClickedListener(new OnItemViewClickedListener() {
    @Override
    public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 
                              RowPresenter.ViewHolder rowViewHolder, Row row) {
        // Check if the clicked item is an Episode
        if (item instanceof Episode) {
            Episode clickedEpisode = (Episode) item;
            Toast.makeText(getContext(), "Clicked episode: " + clickedEpisode.name, 
                           Toast.LENGTH_SHORT).show();
            // Add your actual click logic here (e.g., start playback)
        }
    }
});

Why this works

Leanback’s framework handles all navigation and interaction events (like D-pad clicks or remote button presses) centrally. By using OnItemViewClickedListener, you’re tapping into the framework’s intended way of handling item interactions, ensuring your clicks get detected regardless of how the user navigates to the item.

A quick sanity check: make sure your TextView in the Presenter still has setFocusable(true) and setFocusableInTouchMode(true) set (which you already do) — this is critical for Leanback to recognize the item as selectable and focusable.

内容的提问来源于stack exchange,提问作者Prophet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:09:24