最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - How to make CarUiContentListItem (in CarUiRecyclerView) selectable? - Stack Overflow

programmeradmin5浏览0评论

I am implementing a CarUiRecyclerView with CarUiContentListItem in a com.android.car.settingsmon.BaseFragment. Here is my layout xml of the fragment.

<LinearLayout
xmlns:android=";
xmlns:app=";
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.android.car.ui.recyclerview.CarUiRecyclerView
    android:id="@+id/my_recyclerview"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:carUiSize="large"
    app:enableDivider="true" />
</LinearLayout>

Here is what I do for the CarUiRecyclerView.

private ArrayList<CarUiContentListItem> mList;
private CarUiListItemAdapter mAdapter;
private CarUiRecyclerView mRecyclerView;

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
    mRootView = super.onCreateView(inflater, container, savedInstanceState);
    mList = new ArrayList<CarUiContentListItem>();
    mRecyclerView = mRootView.findViewById(R.id.my_recyclerview);
    mAdapter = new CarUiListItemAdapter(mList);
    mRecyclerView.setAdapter(mAdapter);
}

Later, I will create some CarUiContentListItem and add them into mList.

CarUiContentListItem item = new CarUiContentListItem(CarUiContentListItem.Action.ICON);
item.setTitle(...);
item.setIcon(...);
mList.add(0, item);
mAdapter.notifyItemInserted(0);

So far so good, mRecyclerView can be scrolled by touch (and mouse if I connected scrcpy). But it doesn't respond to key events (KEYCODE_DPAD_UP/KEYCODE_DPAD_DOWN).

I want the first item to be selected (or should I say "focused"?) when I enter this fragment. When I press DPAD_DOWN, the 2nd item should be focused. What should I do?

I tried the following code to manually set focus on a item, but the focused item's text and icon just disappear. Only the focused stroke/background are visible.

public boolean dispatchKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();
    int action = event.getAction();
    boolean ret = false;

    if (action == KeyEvent.ACTION_UP &&
        (keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_DPAD_UP)) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            if (mCurrentFocusedItemIndex < mList.size() - 1) {
                mCurrentFocusedItemIndex++;
            }
        } else if(keyCode == KeyEvent.KEYCODE_DPAD_UP) {
            if (mCurrentFocusedItemIndex > 0) {
                mCurrentFocusedItemIndex--;
            }
        }
        setFocusOnItem(mCurrentFocusedItemIndex);
        ret = true;
    }

    ret |= super.dispatchKeyEvent(event);
    return ret;
}

private void setFocusOnItem(int position) {
    mRecyclerView.smoothScrollToPosition(position);
    RecyclerView.ViewHolder viewHolder = mRecyclerView.findViewHolderForAdapterPosition(position);
    if (viewHolder != null) {
        View itemView = viewHolder.itemView;
        itemView.requestFocus();
    }
}

发布评论

评论列表(0)

  1. 暂无评论