焦点是在adapter.notifyArrayItemRangeChanged上迷失

时间:2015-03-13 13:32:18

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

在我的Android TV应用中,ActionsRow中的操作按钮忽略了

adapter.notifyArrayItemRangeChanged(0, 1);

呼叫。我正在使用:

View focusedView = getActivity().getCurrentFocus();

获取焦点视图,但无法在 notifyArrayItemRangeChanged 之后重新设置焦点。在 notifyArrayItemRangeChanged 之前具有焦点的按钮失去焦点,我无法在 onBindViewHolder 中(或之后)创建该按钮 requestFocus ActionPresenter ActionsRowPresenter onBindRowViewHolder ...每次在 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:我附加了布局视图(Hiearchy View)。

enter image description here

2 个答案:

答案 0 :(得分:1)

如果listview延长Activity,请尝试:

listviewName.setSelection(positionOfItem);

如果您的listview延伸ListActivity,请尝试:

getListView().setSelection(positionOfItem);

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

答案 1 :(得分:0)

只应为需要关注的项目调用

  

vh.mButton.requestFocus();

如果只是为ActionPresenter中的第一个项目通知适配器,则仅为第一个项目调用onBindViewHolder,它将获得焦点。

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