小编azi*_*ian的帖子

recyclerView 的 setOnClickListener 不起作用

因为RecyclerView如果它没有任何项目,则单击RecyclerView有效,但如果它有项目,则单击RecyclerView不起作用。小心我的意思是点击 just RecyclerViewnotRecyclerView的项目

recyclerView.setOnClickListener(view -> {Timber.d("recyclerView clicked");});
Run Code Online (Sandbox Code Playgroud)

RecyclerView即使上面有项目,我如何设置可点击。

android view android-layout android-view android-recyclerview

4
推荐指数
3
解决办法
4232
查看次数

不推荐使用LifecycleRegistryOwner类

我正在使用Android架构组件中的Room数据库.因此,LiveData我正在LifecycleRegistryOwner我的活动中实现界面.由于有替代解决方案可供使用,LifeCyceActivity但我的活动已经延长,BaseActivity所以我不能这样做.

什么是替代品LifecycleRegistryOwner

android android-room android-livedata android-architecture-components

4
推荐指数
2
解决办法
1162
查看次数

RxJava:知道何时完成 observable 并向调用者返回 Boolean Observable

我是 RxJava 的新手。我的经验主要来自这些教程: herehere

现在,我发现自己在使用 Retrofit2 进行 API 调用时会返回一个Obesrvable<AccessToken>. 客户端将调用此服务如下:

public Observable<TokenResult> authenticateWithClientCredentials() {
        return authService.authenticate("client_credentials").take(1);
}
Run Code Online (Sandbox Code Playgroud)

Authenticator调用的类#authenticateWithClientCredentials()将从客户端调用。

我想要实现的是Observable<Boolean>Authenticator类中返回一个,一旦 API 调用完成,即 insideonComplete()表示访问令牌已成功获取并保存在Authenticator类内的缓存中。

我试过defer接线员,但我还是迷路了。

编辑

我知道我可以在参数中传递一个回调,但是 Rx 的想法不是取代旧的经典回调方法吗?

android reactive-programming rx-java rx-android rx-java2

4
推荐指数
1
解决办法
4953
查看次数

Glide 4:获取 GifDrawable 的 GifDecoder

我希望将我的应用程序从 Glide v3 升级到 Glide v4。我需要知道gif通过 Glide 加载的循环有多长。

v3 代码:

int duration = 0;
GifDecoder decoder = gifDrawable.getDecoder();
for (int i = 0; i < gifDrawable.getFrameCount(); i++) {
    duration += decoder.getDelay(i);
}
Run Code Online (Sandbox Code Playgroud)

看起来GifDecoderGlide v4 不再公开。没有它我如何计算这个,或者我现在如何获得解码器?

java android image-loading android-image android-glide

4
推荐指数
1
解决办法
1268
查看次数

ConstraintLayout:基于百分比的尺寸

我可以使用约束布局中的水平和垂直偏差基于屏幕尺寸设置UI元素的位置。

但是,UI元素的宽度和高度不会根据屏幕大小而改变,因此看起来仍然不完美。

那我怎么能做到这一点呢?

下面是我的源代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.satizh.android.constraintlayouttest.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="125dp"
        android:layout_height="47dp"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="0dp"
        android:layout_weight="68"
        android:text="5%V30%H"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.050000012" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toBottomOf="@+id/button"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.915"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="0dp"
        app:layout_constraintVertical_bias="0.138" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.14" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.05" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline3"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.2" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline4"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.53" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

我已经创建了一些指导原则(以百分比为单位),以便我们可以猜测旋转设备时基于屏幕的UI按钮的实际外观。这是更好理解的gif。

在此处输入图片说明

android android-layout android-view android-constraintlayout

4
推荐指数
1
解决办法
3540
查看次数

在 RecyclerView 中未正确解析暗模式颜色

我正在尝试实现夜间模式,但我遇到了问题:CardView不会将颜色更改为night值,但活动正确地将其背景更改为夜间颜色值。

请帮助理解这个问题。

MainActivity

public class NewFinishActivity extends AppCompatActivity {

    @BindView(R.id.finishRecycler)
    RecyclerView finishRecycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

        setContentView(R.layout.activity_new_finish);
        ButterKnife.bind(this);


        LevelRecyclerAdapter mAdapter = new LevelRecyclerAdapter(getApplicationContext(), new ArrayList<>());
        finishRecycler.setAdapter(mAdapter);
    }
}
Run Code Online (Sandbox Code Playgroud)

MainActivity 布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_color"
    tools:context=".activities.NewFinishActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/finishRecycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/background_color"
        android:orientation="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

适配器

public class LevelRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<UserLevel> mItems;
    private Context mContext;

    public LevelRecyclerAdapter(Context context, List<UserLevel> items) {
        mContext = context; …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-view android-recyclerview android-darkmode

4
推荐指数
1
解决办法
1141
查看次数

Dagger2 模块和依赖项之间的区别

我无法理解告诉组件他的模块是什么和告诉组件它的组件依赖项是什么之间的区别。

例如:

@Module public class ModuleA {
    @Provides DependencyA providesDependencyA() {
        return new DependencyA();
    }
}

@Module public class ModuleB {
    @Provides DependencyB providesDependencyB() {
        return new DependencyB();
    }
}

@Component (modules = {ModuleA.class}) 
public interface ComponentA {
    DependencyA getDependencyA();
}
Run Code Online (Sandbox Code Playgroud)

这有什么区别:

@Component (modules = {ModuleA.class, ModuleB.class}) 
public interface ComponentB {
    DependencyB getDependencyB();
}
Run Code Online (Sandbox Code Playgroud)

然后:

@Component (dependencies = {ComponentA.class}, modules = {ModuleB.class})
public interface ComponentB {
    DependencyB getDependencyB();
}
Run Code Online (Sandbox Code Playgroud)

android dagger-2

3
推荐指数
1
解决办法
1174
查看次数

TextDrawable - 根据键显示颜色(电子邮件,ID)

我在使用TextDrawable遇到了一个问题- 我希望为同一个用户显示相同的颜色 - int color2 = generator.getColor("user@gmail.com");在我的情况下使用userId作为键,但我得到的是所有人userId的相同颜色.我在ListView和现在的RecyclerView中尝试了这个,但总是相同的结果 - 我的所有联系人共享相同的颜色.

这是我的ContactsAdapter的代码:

@Override
public void onBindViewHolder(ContactsAdapter.ContactsViewHolder holder, int position) {
    Contact contact = contactList.get(position);

    holder.userName.setText(contact.getUserName());

    TextDrawable.IBuilder builder = TextDrawable.builder()
            .beginConfig()
            .withBorder(0)
            .toUpperCase()
            .endConfig()
            .round();
    ColorGenerator generator = ColorGenerator.MATERIAL;
    // generate random color
    int color1 = generator.getRandomColor();                                    
    // generate color based on a key (same key returns the same color), useful for list/grid views
    int color2 = generator.getColor(holder.getItemId());
    //int color2 = generator.getColor("user@gmail.com");
    TextDrawable textDrawable …
Run Code Online (Sandbox Code Playgroud)

java android android-adapter android-view android-drawable

3
推荐指数
1
解决办法
371
查看次数

gradle插件升级后无法解析DefaultManifestParser

我正在尝试在Android Studio中编译AndroidCaldavSyncAdapter项目.它甚至工作过,但升级后的Gradle插件版本,从com.android.tools.build:gradle:1.5.0com.android.tools.build:gradle:2.3.0其停止工作.现在我收到这个错误:

  Error:(34, 0) A problem occurred evaluating project ':ACalDAV'.
Run Code Online (Sandbox Code Playgroud)

cannot resolve symbol 'builder'

import com.android.builder.core.DefaultManifestParser
Run Code Online (Sandbox Code Playgroud)

这是build.grade文件:

import com.android.builder.core.DefaultManifestParser

apply plugin: 'com.android.application'
apply from: '../config/quality/quality.gradle'

android {
    compileSdkVersion 22
    buildToolsVersion '25.0.3'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

    }

    defaultConfig {
        applicationId "de.we.acaldav"
        minSdkVersion 14 …
Run Code Online (Sandbox Code Playgroud)

android build gradle android-build android-gradle-plugin

3
推荐指数
1
解决办法
902
查看次数

图标和NavigationView之间的左边距

我必须在图标和NavigationView图像中的箭头之间添加左边距:

在此输入图像描述

我知道根据谷歌的规格,这个保证金必须有,16dp但我需要改变它.我试过了:

 <dimen tools:override="true" name="design_navigation_icon_padding">64dp</dimen>
 <dimen tools:override="true" name="design_navigation_separator_vertical_padding">20dp</dimen>
Run Code Online (Sandbox Code Playgroud)

但仍然没有工作.有任何想法吗?

java android android-layout android-view navigationview

3
推荐指数
1
解决办法
1675
查看次数