android-fragments 相关问题

片段表示Android应用中的可重用行为或用户界面的一部分。

带有 TabLayout 的 ViewPager2 无法正确导航大量选项卡

我有 tablayout 并查看 pager2,如下所示: 我有 tablayout 并查看 pager2,如下所示: <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.google.android.material.tabs.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="34dp" android:background="@color/bgd_color_light" android:clipChildren="false" android:clipToPadding="false" app:tabBackground="@color/bgd_color_light" app:tabGravity="fill" app:tabIndicator="@drawable/tab_indicator_round" app:tabIndicatorAnimationMode="elastic" app:tabIndicatorColor="@color/app_color" app:tabIndicatorGravity="stretch" app:tabMode="auto" app:tabRippleColor="@color/transparent" app:tabSelectedTextColor="@color/white" app:tabTextAppearance="@style/CustomTabText" /> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/vp2" android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false" android:clipToPadding="false" android:orientation="horizontal" /> </LinearLayout> 我正在使用选项卡布局中介器在选项卡之间移动。我有 17 个选项卡,当我单击第 5 个或更多数字选项卡时,视图寻呼机滚动到最后位置。选项卡映射不正确。这是片段适配器代码: package com.ai.wallpaper.adapters import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.lifecycle.Lifecycle import androidx.viewpager2.adapter.FragmentStateAdapter import com.ai.wallpaper.fragments.WallpaperFragment import com.ai.wallpaper.response.models.Category class WallpaperPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle, private val isPremium: Boolean, private val categoryList: MutableList<Category>) : FragmentStateAdapter(fragmentManager, lifecycle) { override fun createFragment(position: Int): Fragment { return WallpaperFragment.newInstance(categoryList[position].id, isPremium) } override fun getItemCount(): Int { return categoryList.size } } // adapter attach vp2.adapter = wallpaperPagerAdapter TabLayoutMediator(tabLayout, vp2) { tab, position -> //tab.text = catList[position].name tab.text = "Tab $position" //WallpaperHttpRequest.printMessage("VP2", position) }.attach() 我认为这是因为您在不使用片段管理器的情况下实例化这些片段。默认情况下,适配器使用片段管理器创建前几页,但随后您返回匿名创建的片段。并且由于片段管理器只能使用默认工厂,因此您需要将这些参数作为参数传递。 override fun createFragment(position: Int): Fragment { return fragmentManager.fragmentFactory.instantiate( classLoader, WallpaperFragment::class.java.name ).also { (it as WallpaperFragment).let { fr -> fr.arguments = Bundle().also { bundle -> bundle.putInt("ID_ARG", categoryList[position].id) bundle.putInt("PPREMIUM_ARG", isPremium) } } } }

回答 1 投票 0

Fragment 未附加到 Activity,如何解决?

我正在使用带有底部导航视图的导航组件。 问题是当我打开应用程序时,所选片段是主片段,然后当我导航到另一个片段(收藏夹)时,您...

回答 1 投票 0

纵向上的 Viewpager 和横向上的两个窗格

我正在尝试实现一种布局,当设备纵向显示时显示视图寻呼机,而当设备横向显示时显示两个窗格。 所以我制作了两个不同的布局文件,一个只有一个

回答 4 投票 0

使用 ViewPager2 将方向更改为横向时显示两块片段

我正在使用带有 WebView 的 ViewPager2,在纵向模式下它显示单页,但是当我切换到横向模式时它显示当前页面的一半和上一页的一半。 我有

回答 2 投票 0

错误膨胀布局 - 错误膨胀类片段

我正在尝试在主活动中创建一个片段,以便稍后我可以动态添加元素,当我运行应用程序时,我收到此错误: 04-29 19:32:47.318 E/ AndroidRuntime(27813): java.lang.

回答 1 投票 0

Android Google Maps API v2:FragmentManager.findFragmentById() 始终返回 NULL

在开发涉及Android Google Maps API v2.0的应用程序时,我发现FragmentManager.findFragmentById()总是返回NULL。我膨胀了片段

回答 1 投票 0

Google Maps API V2 片段无法膨胀

这是我第一次开发Android应用程序,我的第一个目标是让一个简单的地图控件出现在屏幕上。代码和一切看起来都不错,除了应用程序......

回答 1 投票 0

这是 Android API 中的错误还是我只是误解了定义?

在Fragment API中有一个名为onHiddenChanged(boolean)的方法,我想知道参数的定义方式是否有错误。它说: 隐藏 如果片段我...

回答 2 投票 0

如何在Android应用程序中显示Google地图

我想在我的应用程序中实现 Google 地图...我获得了地图 API 密钥并在我的 XML 文件中使用它,但 Google 地图未显示。当我将片段添加到 XML 文件时,它会导致强制关闭错误,当我纠正时...

回答 1 投票 0

API 级别的片段 API < 11

我在我的应用程序中遇到了动态表单的问题,该应用程序适用于Android 2.1及更高版本。我知道自 API 级别 11(Android 3.0 Honeycomb)以来有新的 Fragment API,但我也有...

回答 3 投票 0

片段重新加载问题

有一个活动,其底部工具栏有四个选项卡。 每次点击以下选项卡时,都会将片段替换为容器框架布局。 在所有片段中都有一个 API ...

回答 2 投票 0

在Fragment中初始化YouTube播放器

请耐心等待,因为我对 Android 还很陌生。我正在尝试在片段类中初始化 YouTube 播放器。 Google 已经提供了在 Activity 中初始化的方法,如下所示:

回答 1 投票 0

仅当存在匹配时返回索引-1

我正在尝试获取可在列表视图中选择的数组中的项目的索引。 问题是,当我点击该项目时,它只返回索引-1,这意味着它不匹配...

回答 2 投票 0

Android ViewModel MutableLiveData 多次更新

场景 你好, 我有一个带有 ViewPager 的活动。在 ViewPagerAdapter 中,我使用不同的数据创建同一片段的实例。 在每个实例中我初始化一个 ViewModel 瓦尔

回答 3 投票 0

Fragment 中的 GlSurfaceView 在恢复时冻结 UI

我有一个带有 GlSurfaceView 的片段,其中有一个按钮可以将您导航到第二个片段。第二个片段还有一个 GlSurfaceView 和一个按钮,可让您导航回第一个片段

回答 1 投票 0

如何在打开电视时自动启动 Android 应用程序?

我将这些权限添加到清单中 我将 BootReceiver 添加到清单中: 我将这些权限添加到清单中 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 我将 BootReceiver 添加到清单中: <receiver android:name="com.portlmedia.streets.BootReceiver" android:enabled="true" android:exported="true"> <intent-filter android:directBootAware="true"> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" /> <action android:name="android.intent.action.QUICKBOOT_POWERON" /> <action android:name="android.intent.action.REBOOT"/> </intent-filter> </receiver> 我在我的项目中创建了BootReceiver: public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { Intent activityIntent = new Intent(context, MainActivity.class); context.startActivity(activityIntent); } } } 我的问题,如果你可以帮助我,我如何使用 android studio 中的模拟器来测试它? 我在 onReceive 方法中放置了一个断点,但是当我启动应用程序时它没有命中它 我也尝试过使用冷重启,但没有任何结果,我想测试是否确实有效,或者可能是我的代码有问题? 第1步:清单代码 添加权限 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> 应用程序类内部 <receiver android:name=".BootReceiver" android:enabled="true" android:exported="true"> <intent-filter> <category android:name="android.intent.category.DEFAULT" /> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.QUICKBOOT_POWERON" /> <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" /> </intent-filter> </receiver> <service android:name=".AppStartService" android:enabled="true" android:exported="true" android:stopWithTask="false" /> 第2步:获取用户授予的Overlay权限或 adb shell pm grant com.example.appstart android.permission.SYSTEM_ALERT_WINDOW 第3步:启动接收器 class BootReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent?) { Log.i(TAG, "onReceive: boot received ${intent?.action}") val serviceIntent = Intent(context, AppStartService::class.java) ContextCompat.startForegroundService(context, serviceIntent) } companion object { private const val TAG = "BootReceiver" } } 第4步:服务代码 class AppStartService: Service() { override fun onBind(intent: Intent?): IBinder? { return null } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { return START_STICKY } override fun onCreate() { super.onCreate() startForeground(1, createNotification()) GlobalScope.launch { withContext(Dispatchers.Main) { try { val intent = Intent(this@AppStartService, MainActivity::class.java) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK startActivity(intent) } catch (ex: Exception) { Log.e(TAG, "onCreate: ", ex) } } } } private fun createNotification(): Notification { val serviceChannel = NotificationChannel( CHANNEL_ID, "${getString(R.string.app_name)} Service", NotificationManager.IMPORTANCE_DEFAULT ) val manager = getSystemService( NotificationManager::class.java ) manager.createNotificationChannel(serviceChannel) return NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("${getString(R.string.app_name)} Service") .setSilent(true) .setContentText("Please restart this device if this service is not running") .setSmallIcon(R.mipmap.ic_launcher) .build() } companion object { private const val TAG = "AppStartService" private const val CHANNEL_ID = "app-start-service" } } 对我有用!

回答 1 投票 0

Android 在 Fragment 内显示 Snackbar

我在片段内显示小吃栏时遇到了一些问题。我可以为一项活动设置一个很好的设置,但是当我在片段中尝试时,它不会出现,也不会给出错误。 小吃吧

回答 5 投票 0

在导航片段中使用片段

代码 主要活动 // MainActivity.kt 类 MainActivity : AppCompatActivity() { 覆盖 fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)

回答 1 投票 0

fragment之间有多种通信方式?我们什么时候应该使用它们?

fragment之间有多种通信方式 (1)共享ViewModel (2) 分片结果API (3)导航组件 (a) 安全参数 (b) 参数(捆绑) (4)自定义范围的ViewModel...

回答 1 投票 0

防止android重新创建特定Fragment的新实例

我有这样的结构: MainActivity -> ProfileFragment -> ListFragment 其中“->”是 FragmentTransaction.add(....) 我的问题是,我需要 android 来重新加载旧实例吗...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.