焦点迷失在 adapter.notifyArrayItemRangeChanged

Focus is Getting Lost on adapter.notifyArrayItemRangeChanged

在我的 Android TV 应用中,ActionsRow 中的操作按钮失去对

的关注
adapter.notifyArrayItemRangeChanged(0, 1);

打电话。我正在使用:

View focusedView = getActivity().getCurrentFocus();

获取焦点视图,但无法在 notifyArrayItemRangeChanged 后重新设置焦点。 notifyArrayItemRangeChanged 之前具有焦点的按钮失去焦点,我无法在 onBindViewHolder 中(或之后)使该按钮 requestFocus ActionPresenter或者ActionsRowPresenteronBindRowViewHolder...设置焦点每次在 notifyArrayItemRangeChanged.

之后转到第一个按钮

对如何保持焦点或如何将其设置回原始视图有任何想法吗?

ActionPresenter:

@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
    Log.i(TAG, "onBindViewHolder called, item:" + item.toString());
    Action action = (Action) item;
    ActionViewHolder vh = (ActionViewHolder) viewHolder;
    vh.mAction = action;
    vh.mButton.setText(action.getLabel1());
    vh.mButton.setTag(action.getId());

    vh.mButton.requestFocus();
}

ActionsRowPresenter:

protected void onBindRowViewHolder(ActionsRowPresenter.ViewHolder holder, Object item) {
    super.onBindRowViewHolder(holder, item);
    Log.i(TAG, "onBindRowViewHolder called");

    ViewHolder vh = holder;
    ActionsRow row = (ActionsRow) vh.getRow();

    ArrayObjectAdapter aoa = new ArrayObjectAdapter(mActionPresenter);
    aoa.addAll(0, (Collection)row.getActions());
    vh.bind(aoa);
}

NowPlayingChannelRowPresenter:

public NowPlayingChannelRowPresenter(Channel c) {
    setHeaderPresenter(null);
    setSelectEffectEnabled(false);

    mSelectedChannel = c;

    npcdrPresenter = new NowPlayingChannelDetailsRowPresenter(new DetailsDescriptionPresenter());
    actionsRowPresenter = new ActionsRowPresenter();

    ClassPresenterSelector ps = new ClassPresenterSelector();

    ps.addClassPresenter(NowPlayingChannelDetailsRow.class, npcdrPresenter);
    ps.addClassPresenter(ActionsRow.class, actionsRowPresenter);

    npcdRow = new NowPlayingChannelDetailsRow(mSelectedChannel);

    actionsRow = new ActionsRow(mSelectedChannel);

    actionsRow.addAction(new Action(ACTION_START_STOP, "Stop"));
    actionsRow.addAction(new Action(ACTION_ADD_REMOVE_FAVORITE, "Add to Favorites"));

}


@Override
protected RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.now_playing_channel_row, parent, false);
    ViewHolder vh = new ViewHolder(v);

    vh.mNpcdVh = (NowPlayingChannelDetailsRowPresenter.ViewHolder) npcdrPresenter.onCreateViewHolder(vh.llNpcd);
    vh.llNpcd.addView(vh.mNpcdVh.view);

    vh.mActionsVh = (ActionsRowPresenter.ViewHolder) actionsRowPresenter.onCreateViewHolder(vh.llActions);
    vh.llActions.addView(vh.mActionsVh.view);

    return vh;
}


@Override
protected void onBindRowViewHolder(RowPresenter.ViewHolder holder, Object item) {
    super.onBindRowViewHolder(holder, item);

    ViewHolder vh = (ViewHolder) holder;
    npcdrPresenter.onBindRowViewHolder(vh.mNpcdVh, npcdRow);
    actionsRowPresenter.onBindRowViewHolder(vh.mActionsVh, actionsRow);

}

编辑 1: 没有 ListView,我正在使用带有自定义呈现器的 ArrayObjectAdapter 实例

编辑 2: 我附加了布局视图(层次视图)。

如果您的 listview 扩展了 Activity 则尝试:

listviewName.setSelection(positionOfItem);

如果您的 listview 扩展了 ListActivity 则尝试:

getListView().setSelection(positionOfItem);

即使listView更新后也会聚焦到特定位置

这应该只针对需要关注的项目调用

vh.mButton.requestFocus();

如果您仅针对 ActionPresenter 中的第一项通知适配器,将仅针对第一项调用 onBindViewHolder 并获得焦点。

如果需要聚焦的是第二个项目,则应该为该项目调用 ActionPresenter 的 onBindViewHolder,您应该只为第二个项目而不是所有项目请求聚焦。