标签: android-jetpack

如何使用ViewModel和LiveData进行改进API调用

这是我第一次尝试实现MVVM架构,而且我对进行API调用的正确方法感到有些困惑.

目前,我只是尝试从IGDB API进行简单查询,并输出日志中第一项的名称.

我的活动设置如下:

public class PopularGamesActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popular_games);


        PopularGamesViewModel popViewModel = ViewModelProviders.of(this).get(PopularGamesViewModel.class);
        popViewModel.getGameList().observe(this, new Observer<List<Game>>() {
            @Override
            public void onChanged(@Nullable List<Game> gameList) {
                String firstName = gameList.get(0).getName();
                Timber.d(firstName);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我的视图模型设置如下:

public class PopularGamesViewModel extends AndroidViewModel {

    private static final String igdbBaseUrl = "https://api-endpoint.igdb.com/";
    private static final String FIELDS = "id,name,genres,cover,popularity";
    private static final String ORDER = "popularity:desc";
    private static final int LIMIT = 30;

    private LiveData<List<Game>> mGameList; …
Run Code Online (Sandbox Code Playgroud)

android mvvm retrofit android-livedata android-jetpack

16
推荐指数
1
解决办法
9378
查看次数

如何将List <Object>转换为PagedList <Object>,反之亦然?

PagedList<Object>用于Android的酷分页库.要使问题尽可能小:如果我有一个字符串列表,如

  List<String> stringList; // it consists of 200 strings
Run Code Online (Sandbox Code Playgroud)

我想转换 stringListPagedList<String>类似的类型

  PagedList<String> pagedStringList;
Run Code Online (Sandbox Code Playgroud)

而且,如果我有一个PagedList<Object>如何将其转换为List<Object>?我经历了这个以供参考

如果我尝试相反的方式....

我怎么能转换List<Object>DataSource.Factory<Integer, Object>..所以间接我可以把它转换成PagedList<>

DataSource.Factory<Integer, Object>我可以转换为PagedList<>但我怎么能转换listPagedList<>

android rx-java2 android-livedata android-architecture-components android-jetpack

15
推荐指数
1
解决办法
4203
查看次数

如何使用Navigation type safeargs插件将Parcelable类型的对象传递给Fragment

我正在重写我的简单UI应用程序以使用导航架构组件,我需要传递一个实现Parcelable的Pojo,没有看到任何关于如何做到这一点的文档.

任何帮助,将不胜感激.

android android-fragments android-architecture-components android-jetpack

15
推荐指数
4
解决办法
6314
查看次数

导航架构组件 - 如何刷新片段?

我有一个 ListFragment 有一个 recyclerview 和 onClick 任何项目打开 DetailsFragment。详细信息片段在底部包含另一个显示“更多项目”的回收站视图。现在 onClick 任何这些项目应该打开该特定项目的 DetailsFragment。基本上片段需要刷新。

过去,我只会使用 fragmentManager 替换 fragment。我如何去刷新片段?如何创建指向同一个片段的动作?

android android-fragments android-architecture-components android-jetpack android-architecture-navigation

15
推荐指数
1
解决办法
6238
查看次数

是否可以在嵌套导航图 Android 中拥有动态目的地?

我在导航图中实现了一个嵌套图,它有 2 个图。在第一个图中,有 3 个片段,在第二个图中,有 2 个片段。图 2 包含在图 1 中。我想导航到(图 1 第 1 步)到(图 2 第 2 步)。我们不能在两个嵌套片段之间定义动作。那么有什么方法可以将动态目的地分配给导航吗?

在此处输入图片说明

图一

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@id/dashboardFragment">
    <fragment
        android:id="@+id/dashboardFragment"
        android:name="com.navigationgraphexample.fragments.DashboardFragment"
        android:label="fragment_dashboard"
        tools:layout="@layout/fragment_dashboard" >
        <action
            android:id="@+id/action_dashboardFragment_to_stepOneFragment2"
            app:destination="@id/stepOneFragment" />
        <action
            android:id="@+id/action_dashboardFragment_to_sub_nav"
            app:destination="@id/sub_nav" />

    </fragment>
    <fragment
        android:id="@+id/stepOneFragment"
        android:name="com.navigationgraphexample.fragments.StepOneFragment"
        android:label="fragment_step_one"
        tools:layout="@layout/fragment_step_one">
        <action
            android:id="@+id/action_stepOneFragment_to_stepTwoFragment"
            app:destination="@id/stepTwoFragment" />
    </fragment>
    <fragment
        android:id="@+id/stepTwoFragment"
        android:name="com.navigationgraphexample.fragments.StepTwoFragment"
        android:label="fragment_step_two"
        tools:layout="@layout/fragment_step_two" />
    <fragment
        android:id="@+id/notificationFragment"
        android:name="com.navigationgraphexample.fragments.NotificationFragment"
        android:label="fragment_notification"
        tools:layout="@layout/fragment_notification" >
        <deepLink
            android:id="@+id/deepLink"
            app:uri="myapp.com/{myargs}"
            />
        <argument android:name="myargs"
            app:argType="integer"
            android:defaultValue="1" />
    </fragment>
    <include app:graph="@navigation/sub_nav" />
</navigation> …
Run Code Online (Sandbox Code Playgroud)

android android-navigation android-jetpack

15
推荐指数
1
解决办法
2887
查看次数

导航架构片段重载问题

我在图片库中使用导航架构,当我从片段 A 转到 B 然后返回到 A 时,再次调用这 3 个方法这将导致我的图库重新加载,我应该在片段中加载我的数据,所以当我从 B 回到 A 我的方法没有被调用?:

  1. 创建视图
  2. 视图创建
  3. 在恢复

步骤 A 到 B

navigation android android-fragments android-jetpack android-architecture-navigation

15
推荐指数
2
解决办法
6094
查看次数

如何在 Jetpack Compose 中处理导航?

在 Jetpack Compose 中,导航应该如何完成?所有(而且不是很多)示例(包括来自 Google 的官方示例)都使用密封类并加载新屏幕以观察当前屏幕的变化。这确实(有点)工作,但不提供导航后台堆栈,并且手机的后退按钮完全不知道,只是关闭应用程序而不是返回上一个屏幕。这是否应该以某种方式与来自 AndroidX 的导航组件融合——但它是基于 XML 的,而 Compose 就是要远离 XML?或者是否有一个全新的导航概念即将到来,可能类似于 SwiftUI(navigationlink 等)?这似乎是最大的障碍之一——因为没有导航,你只能拥有一个玩具应用程序。有人知道这里的路线图吗?

android kotlin android-jetpack android-jetpack-compose

15
推荐指数
3
解决办法
7969
查看次数

从另一个图表导航到一个片段而不是它的开始目的地

在我的第一张图中,我有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/firstGraph"
    app:startDestination="@id/listFragment">

    <fragment
        android:id="@+id/listFragment"
        android:name="com.example.ListFragment">

        <action
            android:id="@+id/action_list_to_details"
            app:destination="@id/detailsFragment" />

    </fragment>

    <fragment
        android:id="@+id/detailsFragment"
        android:name="com.example.DetailsFragment">

    </fragment>
</navigation>
Run Code Online (Sandbox Code Playgroud)

在我的第二张图中,我有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/secondGraph"
    app:startDestination="@id/dashboardFragment">

    <include app:graph="@navigation/firstGraph" />

    <fragment
        android:id="@+id/dashboardFragment"
        android:name="com.example.DashboardFragment">
        <action
            android:id="@+id/action_dashboard_to_notification"
            app:destination="@id/notificationFragment"/>
    </fragment>

    <fragment
        android:id="@+id/notificationFragment"
        android:name="com.example.NotificationsFragment">

        <action
            android:id="@+id/action_notification_to_details"
            app:destination="@id/firstGraph"/>

    </fragment>
</navigation>
Run Code Online (Sandbox Code Playgroud)

我想直接从“notificationFragment”导航到“detailsFragment”而不是起始目的地,包括第二个图形堆栈

navigation android kotlin android-jetpack

15
推荐指数
1
解决办法
3469
查看次数

Jetpack Compose 截取可组合功能的屏幕截图?

我想在 Jetpack Compose 上截取特定可组合函数的截图。我该怎么做?请任何人帮助我。我将截取可组合函数的截图并与其他应用程序共享

我的功能示例

@Composable
fun PhotoCard(){
    Stack(){
        Image(imageResource(id = R.drawable.background))
        Text(text = "Example")
    }
}
Run Code Online (Sandbox Code Playgroud)

这个功能怎么截屏?

android android-jetpack android-jetpack-compose

15
推荐指数
2
解决办法
443
查看次数

Android Jetpack Compose Icons 不包含部分材质图标

androidx.compose.material.icons.Icons 中有许多常用的材质图标,但有些缺失。举个例子,没有打印图标

...

import androidx.compose.material.Icon
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu  // ok
import androidx.compose.material.icons.filled.Print // error, unresolved reference

@Composable
fun IconsExample() {
    Icon(Icons.Filled.Menu, "menu")   // ok
    Icon(Icons.Filled.Print, "print") // error, unresolved reference
}
Run Code Online (Sandbox Code Playgroud)

在应用程序中使用这些缺失图标的最简单方法是什么?

android android-jetpack android-jetpack-compose

15
推荐指数
1
解决办法
3767
查看次数