android - 如何使用 Dpad/Game Controller 选择并单击 Android ViewPager2 的子视图
问题描述
我正在我当前的 Android 项目中调查蓝牙 Dpad 和游戏控制器。
我可以成功选择并单击我的应用程序中的所有 UI 宽度。
但是我有一个ViewPager2
包含多个片段的,我无法选择或单击任何片段小部件。
MyViewPager2
在 XML 中定义如下:-
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/dashboard_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/dashboard_dots"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="2dp"
app:tabBackground="@drawable/dot_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp" />
在我的主要活动中,我配置ViewPager2
如下: -
viewPager = findViewById(R.id.dashboard_pager)
val pagerAdapter = ScreenSlidePagerAdapter(this@DashboardActivity)
viewPager.adapter = pagerAdapter
val tabLayout = findViewById<TabLayout>(R.id.dashboard_dots)
TabLayoutMediator(tabLayout, viewPager) { _, _ -> }.attach()
我的适配器是:-
private inner class ScreenSlidePagerAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
override fun getItemCount(): Int = NUM_PAGES
override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> DashboardClockFragment()
1 -> DashboardSpeedometerFragment()
2 -> DashboardTimerFragment()
else -> DashboardTimerFragment()
}
}
}
DashboardClockFragment
布局 XML 类似于:-
<FrameLayout
android:id="@+id/dashboard_clock_wrapped"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground">
<TextClock
android:id="@+id/dashboard_clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:fontFamily="@font/lcd_digital"
android:format12Hour="kk:mm:ss"
android:format24Hour="kk:mm:ss"
android:gravity="center"
android:padding="10dp"
android:textColor="?android:attr/textColorPrimary"
android:textSize="@dimen/speed_limit_text_size" />
<TextView
android:id="@+id/dashboard_clock_paused"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:fontFamily="@font/lcd_digital"
android:gravity="center"
android:padding="10dp"
android:text="@string/start_time"
android:textColor="?android:attr/textColorPrimary"
android:textSize="@dimen/speed_limit_text_size"
android:textStyle="bold"
android:visibility="gone" />
</FrameLayout>
我无法使用 Dpad 控制器在布局中选择 TextClock 或 TextView。
在触摸模式下,我可以单击子视图,一切都按需要进行。
在配置布局以响应 Dpad 和/或游戏控制器时,我错过了什么?
解决方案
尝试添加到您希望在页面android:focusableInTouchMode="true"
上android:focusable="true"
可选择的视图。
要捕捉点击添加onClickListener
到所需的视图。
推荐阅读
- c# - 系统图标 - Windows 10 风格
- mysql - 在 MYSQL 中运行 IF ELSE STATEMENT 以运行不同的查询
- node.js - Node.js:在 consul kv get 中传递令牌
- python - python中的opencv未检测到第二个摄像头
- php - 不能使用“对象”作为类名,因为它是保留的 - 修复此代码
- windows - 是否可以像访问任何其他 Isilon 共享一样从远程服务器访问 Azure FileShare?
- reactjs - React 有天赋的聊天消息对齐
- javascript - JavaScript 算法:从字符串中查找连续重复字符的开始和结束索引
- angular - 角度可观察管道返回数组
- xcode - 如何选择嵌入式库而不是已安装的库?