【问题标题】:Focus is Getting Lost on adapter.notifyArrayItemRangeChanged焦点在 adapter.notifyArrayItemRangeChanged 上迷失了
【发布时间】:2015-03-13 13:32:17
【问题描述】:

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

adapter.notifyArrayItemRangeChanged(0, 1);

打电话。我正在使用:

View focusedView = getActivity().getCurrentFocus();

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

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

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:我附加了布局视图(层次结构视图)。

【问题讨论】:

  • 你正在使用什么视图...

标签: android android-activity android-view android-tv


【解决方案1】:

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

listviewName.setSelection(positionOfItem);

如果你的 listview 扩展了 ListActivity 然后试试:

getListView().setSelection(positionOfItem);

即使在 listView 更新后,它也会专注于特定位置。

【讨论】:

  • 谢谢,但没有列表视图。
【解决方案2】:

这应该只为需要聚焦的项目调用

vh.mButton.requestFocus();

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

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

【讨论】:

  • 如何只为第一项通知适配器?我是否必须重写 ActionsRowPresenter 中的方法?
  • 您要关注第一项还是第二项?适配器在 adapter.notifyArrayItemRangeChanged(0, 1) 中包含哪些项目?
  • 适配器包含 NowPlayingChannelRow,它由 NowPlayingChannelDetailsRow 和 ActionsRow 组成(见图)。我只希望 NowPlayingChannelDetailsRow 被刷新,因此调用 adapter.notifyArrayItemRangeChanged(0, 1);调用时再次渲染ActionsRow,失去焦点
  • 当你在ActionPresenter的onBindViewHolder中请求焦点vh.mButton.requestFocus()时会发生什么,只有action.getLabel1()等于要聚焦的视图的标签?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多