我有以下@Dao,提供Flowable<User>流:
@Dao
interface UsersDao {
@Query("SELECT * FROM users")
fun loadUsers(): Flowable<List<User>>
}
Run Code Online (Sandbox Code Playgroud)
我希望流的订户在那里发生一些变化时立即接收数据库的更新.订阅Room Flowable我将获得开箱即用的功能.
我想要的是:如果数据库为空我想执行Web请求并将用户保存到数据库中.订阅者将自动接收刚刚发生的新更新.
现在我希望存储库的客户端不要知道所有的初始化逻辑:他所做的一切 - 他执行usersRepository.loadUsers().所有这些魔法应该发生在存储库类中:
class UsersRepository @Inject constructor(
private val api: Api,
private val db: UsersDao
) {
fun loadUsers(): Flowable<List<User>> {
...
}
}
Run Code Online (Sandbox Code Playgroud)
当然我可以使用以下方法:
fun loadUsers(): Flowable<List<User>> {
return db.loadTables()
.doOnSubscribe {
if (db.getCount() == 0) {
val list = api.getTables().blockingGet()
db.insert(list)
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我想在不使用副作用(doOn...运算符)的情况下构造流.我尝试过,composing()但没有多大帮助.被困在这一段时间了.
我正在尝试使用片段创建应用程序.我创建了一个测试片段(HomeFragment),它只有一个简单的TextView.我创建了所有必需的类(模块,模型和提供者).但我得到一个奇怪的并发症错误
Error:(25, 10) error: [dagger.android.AndroidInjector.inject(T)] android.arch.lifecycle.ViewModelProvider.Factory is bound multiple times:
@Provides android.arch.lifecycle.ViewModelProvider.Factory app.series.com.series3go.ui.main.MainActivityModule.mainViewModelProvider(app.series.com.series3go.ui.main.MainViewModel)
@Provides android.arch.lifecycle.ViewModelProvider.Factory app.series.com.series3go.ui.home.HomeFragmentModule.provideHomeFragmentViewModel(app.series.com.series3go.ui.home.HomeFragmentViewModel)
Run Code Online (Sandbox Code Playgroud)
HomeFragment
public class HomeFragment extends BaseFragment<HomeFragmentBinding, HomeFragmentViewModel> {
public static final String TAG = HomeFragment.class.getSimpleName();
@Inject
ViewModelProvider.Factory mViewModelFactory;
HomeFragmentBinding mHomeFragmentBinding;
private HomeFragmentViewModel mHomeFragmentViewModel;
public static HomeFragment newInstance() {
Bundle args = new Bundle();
HomeFragment fragment = new HomeFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mHomeFragmentBinding = …Run Code Online (Sandbox Code Playgroud) 我有一个带有匕首设置的项目,具有以下提供者方法:
@Module(...)
abstract class AppModule {
@Module
companion object {
...
@Provides
@Singleton
@JvmStatic
fun provideSharedPreferences(@AppContext context: Context): SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
}
@Binds
@AppContext
@Singleton
abstract fun provideAppContext(application: Application): Context
}
Run Code Online (Sandbox Code Playgroud)
这是来自应用程序的代码onCreate():
override fun onCreate() {
if (BuildConfig.DEBUG) {
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDialog()
.build())
StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.build())
Timber.plant(Timber.DebugTree())
}
...
super.onCreate()
}
Run Code Online (Sandbox Code Playgroud)
在API 27模拟器上运行项目会导致以下行为:

使用以下日志:
D/StrictMode:StrictMode策略违规; 〜duration = 275 ms:android.os.StrictMode $ StrictModeDiskReadViolation:policy = 196671 violation = 2 at android.os.StrictMode $ AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1440)at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java: 251)在java.io.File.exists(File.java:807)的android.app.ContextImpl.getDataDir(ContextImpl.java:2197)在Android上的android.app.ContextImpl.getPreferencesDir(ContextImpl.java:517).在Android.pretent.PreferenceManager.getDefaultSharedPreferences的android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:167)的android.app.ContextImpl.getSharedPreferences(ContextImpl.java:368)上的app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:714) (PreferenceManager.java:526)com.some.package.di.module.AppModule $ Companion.provideSharedPreferences(AppModule.kt:112)...
这意味着,是 …
android disk-io kotlin android-sharedpreferences android-strictmode
我正在使用 Play Services Auth api Phone,到目前为止我有以下内容
fun startSmsListener() {
val client = SmsRetriever.getClient(applicationContext /* context */);
val task = client.startSmsRetriever();
task.addOnSuccessListener(object : OnSuccessListener<Void> {
override fun onSuccess(p0: Void?) {
//do somethin
}
})
task.addOnFailureListener(object : OnFailureListener {
override fun onFailure(p0: Exception) {
//Handle error
}
})
}
Run Code Online (Sandbox Code Playgroud)
现在我想把它放在一个 SmsManager 类中,并将它转换成一个 Single/Observable,这样我就可以在我的视图模型中以一种反应方式处理它。我怎样才能做到这一点?
到目前为止,我有这个:
var single = Single.create(SingleOnSubscribe<Void> { e ->
val task = client.startSmsRetriever()
task.addOnSuccessListener {
e.onSuccess(it)
}
task.addOnFailureListener {
e.onError(it)
}
})
Run Code Online (Sandbox Code Playgroud)
但我不确定这段代码是否正确,是否有遗漏的东西,比如在处理后删除监听器。
有什么帮助吗?
我的应用程序中有一些屏幕,TalkBack 无法推断出正确的阅读顺序。根据文档,我可以使用android:accessibilityTraversalAfter和朋友来改变阅读顺序。但它对我来说不适用于可聚焦的元素,ViewGroup这些元素应该一起阅读。
整个布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:accessibilityTraversalBefore="@id/before"
android:focusable="true"
tools:context=".MainActivity">
<TextView
android:id="@+id/before"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:accessibilityTraversalAfter="@id/before"
android:text="Before"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/after"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:accessibilityTraversalBefore="@id/after"
android:text="After"
app:layout_constraintBottom_toTopOf="@+id/before"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
它呈现After在屏幕中间的Before底部。我希望 TalkBack 将整个屏幕视为单个连续元素,因此我设置android:focusable为true. 默认情况下,TalkBack 读取为:“之后,之前”。但我希望它读作“之前,之后”。虽然我添加了android:accessibilityTraversalBeforeand android:accessibilityTraversalAfter,但它仍然显示为“之后,之前”。这是节点树调试的输出:
TreeDebug: (-2147455381)429.FrameLayout:(0, 0 - 1080, 1920):A
TreeDebug: (30189)429.TextView:(42, 101 - 397, 172):TEXT{My Application}:A:supportsTextLocation
TreeDebug: (31150)429.ViewGroup:(0, 210 …Run Code Online (Sandbox Code Playgroud) 当我在活动中使用它时,Butterknife运行良好,但当我尝试在片段中使用它时,它不起作用:
public class SettingsFragment extends Fragment {
private static final String TAG = "SettingsFragment";
@BindView(R.id.settings_email) TextView _settingsMail;
@BindView(R.id.settings_password) TextView _passwordMail;
@BindView(R.id.settings_token) TextView _tokenMail;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//returning our layout file
final View view = inflater.inflate(R.layout.fragment_3_settings, container, false);
ButterKnife.bind(this, view);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());
String email = prefs.getString("email", null);
String passwd = prefs.getString("password", null);
String token = prefs.getString("token", null);
_settingsMail.setText(email);
_passwordMail.setText(passwd);
_tokenMail.setText(token);
return inflater.inflate(R.layout.fragment_3_settings, container, false);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用翻译动画将图像视图从屏幕顶部移动到屏幕中央或屏幕中间,即当活动开始时,图像视图开始从屏幕顶部移动到屏幕中间或屏幕中央,其中图像视图空间的顶部和底部图像视图空间在屏幕上显示的完全相同,就像我们在相对布局中所做的一样。在xml文件的布局中,使用父标签中心为true。通常我们会在facebook和whatsapp应用程序中找到这类动画,它们已将它们用于图像的翻译或移动图像视图动画。到目前为止我做了什么。请帮助我解决这些问题。谢谢。
public class MainActivity extends AppCompatActivity {
ImageView imageview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview = (ImageView) findViewById(R.id.imagview);
RelativeLayout root = (RelativeLayout) findViewById(R.id.rel);
TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 50);
anim.setInterpolator((new AccelerateDecelerateInterpolator()));
anim.setAnimationListener(new MyAnimationListener());
anim.setDuration(500);
anim.setFillAfter(true);
imageview.startAnimation(anim);
}
});
}
Run Code Online (Sandbox Code Playgroud) I am trying to implement paging in one activity but I keep running out of memory. The app runs till the 4th page but when it gets to the 5th it crashes. I've tried increasing the JVM heap size but the app still crashes at this one point where it runs out of memory to allocate.
This is my main file:
public class S1Intro extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_s1);
MyPagerAdapter adapter = new MyPagerAdapter(); …Run Code Online (Sandbox Code Playgroud) android ×8
java ×4
kotlin ×3
android-view ×2
rx-java ×2
rx-java2 ×2
android-room ×1
animation ×1
butterknife ×1
dagger ×1
dagger-2 ×1
disk-io ×1
fragment ×1
talkback ×1