我在官方API文档中看到了DataBindingComponent.
https://developer.android.com/reference/android/databinding/DataBindingUtil.html
在编译期间生成此接口以包含所有使用的实例BindingAdapter的getter.当BindingAdapter是实例方法时,必须实例化实现该方法的类的实例.这个接口将使用getter为每个类生成,其名称为get*,其中*是声明BindingAdapter类/接口的简单类名.将通过向getter添加数字后缀来解析名称冲突.
也可以将此类的实例作为第一个参数传递给static或实例BindingAdapters.
如果使用Dagger 2,开发人员应扩展此接口并将扩展接口注释为Component.
但是,我在Web中找不到此类的任何示例用法.任何人都可以知道它是什么以及如何使用它.
我试着制作一些简单的代码并调试它以查看它是什么但它在变量上显示了null a变量.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding= DataBindingUtil.setContentView(this,R.layout.activity_main);
android.databinding.DataBindingComponent a = DataBindingUtil.getDefaultComponent();
setContentView(binding.getRoot());
}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的DAO,包括CRUD功能
FeedEntryDAO.java
@Dao
public interface FeedEntryDAO {
@Query("SELECT * FROM feedEntrys")
LiveData<List<FeedEntry>> getAll();
@Query("SELECT * FROM feedEntrys WHERE uid = :uid LIMIT 1")
LiveData<FeedEntry> findByUid(int uid);
@Insert
void insertAll(FeedEntry... feedEntries);
@Delete
void delete(FeedEntry feedEntry);
@Update
int update(FeedEntry feedEntry);
}
Run Code Online (Sandbox Code Playgroud)
对于select,可以返回LiveData类型.
在Activity中,代码非常适合选择
viewModel.getFeedEntrys().observe(this,entries -> {...});
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试插入,更新,删除数据时.代码看起来有点难看,每次都会创建一个asynctask.
new AsyncTask<FeedEntry, Void, Void>() {
@Override
protected Void doInBackground(FeedEntry... feedEntries) {
viewModel.update(feedEntries[0]);
return null;
}
}.execute(feedEntry);
Run Code Online (Sandbox Code Playgroud)
我有2个问题:
感谢任何建议和意见.谢谢.
android android-room android-livedata android-architecture-components
我有兴趣尝试Android Studio中显示的导航图.但是在导入谷歌样本后,我的预览无法使用
我使用的是Android Studio 3.2 Preview Canary 16
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:startDestination="@+id/launcher_home">
<fragment
android:id="@+id/launcher_home"
android:name="com.android.samples.arch.componentsbasicsample.StartFragment"
android:label="Home">
<action
android:id="@+id/end_action"
app:destination="@id/end_dest" />
</fragment>
<fragment
android:id="@+id/end_dest"
android:name="com.android.samples.arch.componentsbasicsample.EndFragment"
android:label="End"
>
</fragment>
</navigation>
Run Code Online (Sandbox Code Playgroud)
18/6/2018更新:
即使我重建项目它也不起作用.但是如果添加新屏幕,则会在预览模式下显示新屏幕
android android-architecture-components android-studio-3.2 android-architecture-navigation
我试图在android studio中使用beta功能(数据绑定).按照android studio的指南,我可以在android studio中找到相关的类DataBindingInfo.但是在创建项目后,数据绑定类不会生成.有人可以帮忙吗?
app模块的build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.pigfamily.myapplication"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
}
Run Code Online (Sandbox Code Playgroud)
该项目的build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
dependencies {
classpath "com.android.tools.build:gradle:1.3.0"
classpath …Run Code Online (Sandbox Code Playgroud) 我已经使用了数据绑定一段时间了,即使现在它也不适用于JDK 8和API 24.我仍然找到一种以更简单的方式使用数据绑定的方法.但是当我使用以下方式进行精确的双向数据绑定时(在我看来,双向数据绑定就像这里(什么是双向绑定?),有些奇怪的事情发生了.
1.双向数据绑定(以xml为单位)
android:text="@={testStr}"
Run Code Online (Sandbox Code Playgroud)
这在官方文档中没有提到(https://developer.android.com/topic/libraries/data-binding/index.html,此页面通常会更新,现在可能会更改).但它可以将变量绑定到xml.
2. ObservableField属性
这里的示例(https://developer.android.com/topic/libraries/data-binding/index.html#observablefields)
private static class User {
public final ObservableField<String> firstName =
new ObservableField<>();
public final ObservableField<String> lastName =
new ObservableField<>();
public final ObservableInt age = new ObservableInt();
}
Run Code Online (Sandbox Code Playgroud)
3.将模型类扩展为BaseObservable
private static class User extends BaseObservable {
private String firstName;
private String lastName;
@Bindable
public String getFirstName() {
return this.firstName;
}
@Bindable
public String getLastName() {
return this.lastName;
}
public void setFirstName(String firstName) { …Run Code Online (Sandbox Code Playgroud) 当我尝试为Room Db和Retrofit 实现NetworkBoundResource和Resource帮助类时,它完美无缺.但是,我需要在没有Room的情况下使用Retrofit实现RESTful的搜索结果.这Resources堂课很好,我不需要改变它.我想要做的是尝试删除此类中的数据库源.
public abstract class NetworkBoundResource<ResultType, RequestType> {
private final AppExecutors appExecutors;
private final MediatorLiveData<Resource<ResultType>> result = new MediatorLiveData<>();
@MainThread
public NetworkBoundResource(AppExecutors appExecutors) {
this.appExecutors = appExecutors;
result.setValue(Resource.loading(null));
LiveData<ResultType> dbSource = loadFromDb();
result.addSource(dbSource, data -> {
result.removeSource(dbSource);
if (shouldFetch(data)) {
fetchFromNetwork(dbSource);
} else {
result.addSource(dbSource, newData -> setValue(Resource.success(newData)));
}
});
}
@MainThread
private void setValue(Resource<ResultType> newValue) {
if (!Objects.equals(result.getValue(), newValue)) {
result.setValue(newValue);
}
}
private void fetchFromNetwork(final LiveData<ResultType> dbSource) …Run Code Online (Sandbox Code Playgroud) 在我们的用户需求中,我们有这样的流程。
我搜索了以下内容:
https://developer.android.com/guide/navigation/navigation-principles
但没有什么可以帮助...
我做了什么?
目前我拆分了 2 个图并在 Graph2 中添加了重复的片段 C。此外,我以编程方式将图开始目标更改为图 1 中的片段 C,只有我从图 2 中单击。这是一个丑陋的解决方案但我不知道如何解决这个问题。
我的问题是:有什么办法可以做到而不是制作重复的目的地并更改起始目的地?
图一:
<include app:graph="@navigation/graph2" />
<dialog
android:id="@+id/fragmentA"
android:label="Fragment A"
tools:layout="@layout/fragmentA"/>
<dialog
android:id="@+id/fragmentB"
android:label="Fragment B"
tools:layout="@layout/fragmentB">
<action
android:id="@+id/NavigateToGraph2"
app:destination="@id/graph2">
</dialog>
<dialog
android:id="@+id/fragmentC"
android:label="Fragment C"
tools:layout="@layout/fragmentC"/>
<dialog
android:id="@+id/fragmentD"
android:label="Fragment D"
tools:layout="@layout/fragmentD"/>
<dialog
android:id="@+id/fragmentE"
android:label="Fragment E"
tools:layout="@layout/fragmentE"/>
Run Code Online (Sandbox Code Playgroud)
图2
<dialog
android:id="@+id/fragmentF"
android:label="Fragment F"
tools:layout="@layout/fragmentF"/>
<dialog
android:id="@+id/fragmentG"
android:label="Fragment G"
tools:layout="@layout/fragmentG"/>
<dialog
android:id="@+id/fragmentC"
android:label="Fragment C"
tools:layout="@layout/fragmentC"/>
Run Code Online (Sandbox Code Playgroud) android android-navigation android-architecture-components android-architecture-navigation
我在android官方文档中阅读了有关生命周期和实时数据的文档.我知道该类实现了LifeCycleObserver并使位置监听器自动关闭或打开.我也知道实时数据可以自动激活或激活.我试图使用这两种方式实现Location Observer.它工作正常,当位置更新时它显示Toast 2次.
我的问题是,如果我真的想实现像DB Connection,GPS Location,Download Image,运行后台服务这样的方式,这两种方式有什么区别.我可以使用LiveData类吗?因为我只需要实现主动和非主动功能.
LocationLiveData.java
public class LocationLiveData extends LiveData<Location> {
private LocationManager locationManager;
private Context context;
public LocationLiveData(Context context) {
this.context = context;
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
private LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
setValue(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
@Override
protected void …Run Code Online (Sandbox Code Playgroud) android android-architecture-lifecycle android-architecture-components
我编写了一些片段场景测试,这些测试将截取屏幕截图和一些点击事件。由于未显示图像视图,很少有测试失败。
测试库( https://developer.android.com/training/basics/fragments/testing )中的测试活动似乎使用 FragmentActivity,它不是 AppCompatActivity 的子类。结果,这些图像视图不可见。我可以添加属性android:src以使其在测试中可见,但这将是一个丑陋的修复。还有其他解决办法吗?
感谢对此的任何评论或建议。
view_item.xml
<ImageView
android:id="@+id/ivRemove"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginStart="@dimen/spacing_16dp"
android:layout_marginTop="@dimen/spacing_16dp"
android:layout_marginBottom="@dimen/spacing_16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_remove" />
Run Code Online (Sandbox Code Playgroud)
构建.gradle.kts
defaultConfig {
this.testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
this.vectorDrawables.useSupportLibrary = true
}
Run Code Online (Sandbox Code Playgroud) android android-testing android-vectordrawable android-fragmentscenario
我刚刚将 Android Studio 升级到最新版本。目前,我猜测我的一些测试类会产生不稳定的测试结果,并且我想在 Android Studio 中运行重复模式,就像在 Android Studio 4.2 中所做的那样。但我发现出现了以下异常
即使我尝试使用一个简单的测试类,我仍然收到此错误。我使用 Gradle Test runner 运行,它按预期工作,但我无法使用它重复运行测试类以查看我的测试类中是否有任何片状结果
感谢任何评论或建议:)
java.lang.NoClassDefFoundError: io/mockk/proxy/MockKAgentFactory
at io.mockk.impl.JvmMockKGateway.<init>(JvmMockKGateway.kt:198)
at io.mockk.impl.JvmMockKGateway.<clinit>(JvmMockKGateway.kt:186)
at com.example.testCorrect(Student.kt:24)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) …Run Code Online (Sandbox Code Playgroud) android ×10
android-architecture-components ×5
android-architecture-navigation ×2
android-architecture-lifecycle ×1
android-room ×1
data-binding ×1
gradle ×1
java ×1
junit5 ×1
mockk ×1