Android 焦点在适配器上丢失。notifyArrayItemRangeChanged

Android 焦点在适配器上丢失。notifyArrayItemRangeChanged,android,android-activity,android-view,android-tv,Android,Android Activity,Android View,Android Tv,在我的安卓电视应用程序中,ActionsRow中的action按钮会失去焦点 adapter.notifyArrayItemRangeChanged(0, 1); 打电话。我正在使用: View focusedView = getActivity().getCurrentFocus(); 获取聚焦视图,但无法在更改NotifyArrayItemRange后重新设置聚焦。在NotifyArrayItemRange更改之前具有焦点的按钮失去焦点,并且我无法在ActionPresenter的onB

在我的安卓电视应用程序中,ActionsRow中的action按钮会失去焦点

adapter.notifyArrayItemRangeChanged(0, 1);
打电话。我正在使用:

View focusedView = getActivity().getCurrentFocus();
获取聚焦视图,但无法在更改NotifyArrayItemRange后重新设置聚焦。在NotifyArrayItemRange更改之前具有焦点的按钮失去焦点,并且我无法在ActionPresenter的onBindViewHolder或ActionsRowPresenter的onBindRowViewHolder中(或之后)使该按钮成为requestFocus。。。每次更改NotifyArrayItemRange后,焦点都设置为第一个按钮

如何保持焦点或如何将其恢复到原始视图

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();
}

操作RowPresenter

    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);
}
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);

}

正在播放频道RowPresenter

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


如果您的
列表视图
扩展了
活动
,请尝试:

listviewName.setSelection(positionOfItem);
getListView().setSelection(positionOfItem);
如果您的
listview
扩展了
ListActivity
,请尝试:

listviewName.setSelection(positionOfItem);
getListView().setSelection(positionOfItem);

即使在listView更新之后,它也会聚焦于特定的位置。

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

vh.mButton.requestFocus()

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


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

谢谢,但是没有listview。您使用的是什么视图…如何不为第一个项目设置适配器?我必须重写ActionsRowPresenter中的方法吗?要聚焦第一项还是第二项?适配器中包含哪些项。notifyArrayItemRangeChanged(0,1)?适配器包含NowPlayingChannelRow,它由NowPlayingChannelDetailsRow和ActionsRow组成(请参见图)。我只想刷新NowPlayingChannelDetailsRow,从而调用adapter.notifyArrayItemRangeChanged(0,1);调用它时,ActionsRow将再次呈现,焦点将丢失仅当action.getLabel1()等于要聚焦的视图的标签时,在ActionPresenter的onBindViewHolder中请求focus vh.mButton.requestFocus()时会发生什么情况?