admin管理员组文章数量:1396858
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();
}
}
本文标签: androidHow to make CarUiContentListItem (in CarUiRecyclerView) selectableStack Overflow
版权声明:本文标题:android - How to make CarUiContentListItem (in CarUiRecyclerView) selectable? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744144757a2592783.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论