小编qba*_*ait的帖子

测试LiveData转换?

我使用Android架构组件和反应式方法构建了一个启动画面.我从Preferences LiveData对象返回fun isFirstLaunchLD(): SharedPreferencesLiveData<Boolean>.我有ViewModel将LiveData传递给视图并更新首选项

val isFirstLaunch = Transformations.map(preferences.isFirstLaunchLD()) { isFirstLaunch ->
    if (isFirstLaunch) {
        preferences.isFirstLaunch = false
    }
    isFirstLaunch
}
Run Code Online (Sandbox Code Playgroud)

在我的片段中,我从ViewModel观察LiveData

    viewModel.isFirstLaunch.observe(this, Observer { isFirstLaunch ->
        if (isFirstLaunch) {
            animationView.playAnimation()
        } else {
            navigateNext()
        }
    })
Run Code Online (Sandbox Code Playgroud)

我想现在测试我的ViewModel,看看是否正确更新了isFirstLaunch.我该怎么测试呢?我是否正确分离了所有图层?你会对这个示例代码进行什么样的测试?

android android-testing android-livedata android-architecture-components android-jetpack

13
推荐指数
2
解决办法
711
查看次数

不调用ReplacementSpan的draw()方法

我在字符串中设置背景:

spanString.setSpan(new BackgroundColorSpan(color), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Run Code Online (Sandbox Code Playgroud)

但是我想在这个背景中增加左右填充,所以我创建了自定义跨度

public class PaddingBackgroundSpan extends ReplacementSpan {
private int mBackgroundColor;
private int mForegroundColor;

public PaddingBackgroundSpan(int backgroundColor, int foregroundColor) {
    this.mBackgroundColor = backgroundColor;
    this.mForegroundColor = foregroundColor;
}

@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    return Math.round(measureText(paint, text, start, end));

}

@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
    RectF rect = new RectF(x, top, …
Run Code Online (Sandbox Code Playgroud)

android spannablestring

11
推荐指数
4
解决办法
3144
查看次数

如何使用 Jetpack Navigation 从嵌套 Fragment 导航到父 Fragment?

我有主导航:SplashFragment -> RegistrationFragment -> RootFragment

<fragment
    android:id="@+id/splashFragment"
    android:name="com.low6.low6.features.splash.SplashFragment"
    android:label="Splash"
    tools:layout="@layout/fragment_splash" >
    <action
        android:id="@+id/action_next"
        app:clearTask="true"
        app:destination="@id/registrationFragment" />
</fragment>

<fragment
    android:id="@+id/registrationFragment"
    android:name="com.low6.low6.features.registration.RegistrationFragment"
    android:label="Register">
    <action
        android:id="@+id/action_next"
        app:clearTask="true"
        app:destination="@id/rootFragment" />
</fragment>

<fragment
    android:id="@+id/rootFragment"
    android:name="com.low6.low6.core.RootFragment"
    android:label="@string/home"
    tools:layout="@layout/fragment_root" />
Run Code Online (Sandbox Code Playgroud)

我有嵌套的注册导航: RegistrationPersonalFragment -> RegistrationContactFragment -> RegistrationSecurityFragment

<fragment
    android:id="@+id/registrationPersonalFragment"
    android:name="com.low6.low6.features.registration.RegistrationPersonalFragment"
    android:label="Register">

    <action
        android:id="@+id/action_next"
        app:destination="@+id/registrationContactFragment" />
</fragment>

<fragment
    android:id="@+id/registrationContactFragment"
    android:name="com.low6.low6.features.registration.RegistrationContactFragment"
    android:label="Register">

    <action
        android:id="@+id/action_next"
        app:destination="@+id/registrationSecurityFragment" />
</fragment>

<fragment
    android:id="@+id/registrationSecurityFragment"
    android:name="com.low6.low6.features.registration.RegistrationSecurityFragment"
    android:label="Register">

    <action
        android:id="@+id/action_next"
        app:destination="@+id/rootFragment" />
</fragment>
Run Code Online (Sandbox Code Playgroud)

如何使用 Jetpack Navigation 组件从最后一个嵌套的 RegistrationSecurityFragment 重定向到 RootFragment?

目前

<action
    android:id="@+id/action_next"
    app:destination="@+id/rootFragment" />
Run Code Online (Sandbox Code Playgroud)

   navigateTo(R.id.action_next) …
Run Code Online (Sandbox Code Playgroud)

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

10
推荐指数
2
解决办法
9939
查看次数

Android ViewModel用于自定义视图

我想重构我的自定义视图以使用android体系结构组件。但是,我看到

ViewModelProviders.of(...)
Run Code Online (Sandbox Code Playgroud)

只接受活动或片段。任何想法如何使其工作?我应该使用片段而不是自定义视图吗?

android android-custom-view android-architecture-components

8
推荐指数
1
解决办法
1036
查看次数

迁移到AndroidX后的数据绑定错误

迁移到AndroidX后,我遇到了很多关于数据绑定错误的错误(Refactor> Migrate to AndroidX).

error: cannot find symbol
      @Nullable ViewGroup root, boolean attachToRoot, @Nullable DataBindingComponent component) {
                                                                ^
  symbol:   class DataBindingComponent
  location: class ActivityInfoBinding   
Run Code Online (Sandbox Code Playgroud)

我正在使用Android Studio 3.2 Beta 2

我的依赖:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.cardview:cardview:1.0.0-beta01'
    implementation 'com.google.android.material:material:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha1'
    implementation "com.squareup.retrofit2:retrofit:$versions.retrofit"
    implementation "com.squareup.retrofit2:converter-gson:$versions.retrofit"
    implementation 'androidx.room:room-runtime:2.0.0-beta01'
    kapt 'androidx.room:room-compiler:2.0.0-beta01'
    implementation "androidx.core:core-ktx:$versions.ktx"
    implementation 'androidx.lifecycle:lifecycle-runtime:2.0.0-beta01'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-beta01'
    kapt 'androidx.lifecycle:lifecycle-compiler:2.0.0-beta01'
    implementation "me.tatarka.bindingcollectionadapter2:bindingcollectionadapter:$versions.binding_collection_adapter"
    implementation "me.tatarka.bindingcollectionadapter2:bindingcollectionadapter-recyclerview:$versions.binding_collection_adapter"
    implementation "com.jakewharton.threetenabp:threetenabp:$versions.threetenabp"
    implementation "com.google.dagger:dagger:$versions.dagger"
    kapt "com.google.dagger:dagger-compiler:$versions.dagger"
    kapt "com.google.dagger:dagger-android-processor:$versions.dagger"
    implementation "com.google.dagger:dagger-android-support:$versions.dagger"
    implementation "com.airbnb.android:lottie:$versions.lottie"
    implementation …
Run Code Online (Sandbox Code Playgroud)

android android-databinding androidx

7
推荐指数
1
解决办法
1799
查看次数

在Kotlin中测试私有方法

如何在Kotlin中测试私有方法?我尝试从中添加@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)androidx.annotation.VisibleForTesting但没有将我的函数设为私有

这就是我的使用方式

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
fun doSomething() {}
Run Code Online (Sandbox Code Playgroud)

[编辑]

我知道我不应该测试private方法,但是现在它总是微不足道的。那下面的情况呢?

我有一个CsvReader类

class CsvReader(private val inputStream: InputStream, private val separator: String = "\t") {
    fun read(): List<String> {
        return read(inputStream.bufferedReader())
    }
    private fun read(bufferedReader: BufferedReader): List<String> {
        val line = bufferedReader.use { it.readLine() } // `use` is like try-with-resources in Java
        return parse(line)
    }
    private fun parse(line: String): List<String> {
        return line.split(separator)
    }
}
Run Code Online (Sandbox Code Playgroud)

我为此写了测试

class CsvReaderTest {
    private val stream = mock<InputStream>() …
Run Code Online (Sandbox Code Playgroud)

tdd android private kotlin

7
推荐指数
3
解决办法
4993
查看次数

应用内结算,inventory.getPurchase在不同的设备上为空,而不是付款

在我的应用程序用户可以购买广告删除,我保留此项目(不消费).所以我在主要活动中有片段检查用户是否购买了物品.

public class BillingInventoryFragment extends Fragment {
// Helper billing object
private IabHelper mHelper;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRetainInstance(true);

    initialiseBilling();
}


private void initialiseBilling() {
    if (mHelper != null) {
        return;
    }

    // Create the helper, passing it our context and the public key to verify signatures with
    mHelper = new IabHelper(getActivity(), BillingUtils.getApplicationKey());

    // Enable debug logging (for a production application, you should set this to false).
    mHelper.enableDebugLogging(true);

    // Start setup. This is asynchronous and the specified …
Run Code Online (Sandbox Code Playgroud)

android in-app-billing

6
推荐指数
1
解决办法
1741
查看次数

矢量Drawable在圈子里

是否有一种简单的方法来生成Vector Drawable,它是一个带有现有矢量可绘制图标的圆圈?

例: 现有的矢量绘图

生成的矢量drawable,里面有空图标的圆圈

svg android android-vectordrawable

6
推荐指数
3
解决办法
5847
查看次数

Android Studio 经常死机

我的 Android Studio 有时每天至少冻结 2 次,甚至十几次。

我使用 Android Studio 3.2 Beta 5,但它曾经发生在稳定的 3.1 上。

About my mac 
 Model Name:    MacBook Pro
 Model Identifier:  MacBookPro13,2
 Processor Name:    Intel Core i5
 Processor Speed:   2.9 GHz
 Number of Processors:  1
Run Code Online (Sandbox Code Playgroud)

我至少打开了 2 个项目,slack 和带有 ~6 个选项卡的浏览器。通常,它发生在构建之后。我在 3 个显示器上工作(2 个通过 USB-C 外部连接)它也发生在我的私人 MacBook 2013 上。

[编辑] 不幸的是增加值studio.vmotiponsfrom

-Xms256m
-Xmx1280m
Run Code Online (Sandbox Code Playgroud)

-Xms512m
-Xmx2048m
Run Code Online (Sandbox Code Playgroud)

不能解决问题。

发生冻结时我当前的内存使用情况 在此处输入图片说明

macos android-studio macbookpro-touch-bar

6
推荐指数
1
解决办法
3796
查看次数

所有操作的Android Jetpack导航默认过渡

我正在开发一个拥有50多个屏幕的应用。此刻我的动作看起来像那样

    <action
        android:id="@+id/action_next"
        app:destination="@+id/introFragment"
        app:enterAnim="@anim/nav_default_enter_anim"
        app:exitAnim="@anim/nav_default_exit_anim"
        app:popEnterAnim="@anim/nav_default_pop_enter_anim"
        app:popExitAnim="@anim/nav_default_pop_exit_anim" />
Run Code Online (Sandbox Code Playgroud)

我可以在一个地方为我的应用程序中的所有操作定义默认过渡,并删除200行以上的代码:)

        app:enterAnim="@anim/nav_default_enter_anim"
        app:exitAnim="@anim/nav_default_exit_anim"
        app:popEnterAnim="@anim/nav_default_pop_enter_anim"
        app:popExitAnim="@anim/nav_default_pop_exit_anim"
Run Code Online (Sandbox Code Playgroud)

android android-navigation android-jetpack

6
推荐指数
0
解决办法
220
查看次数