我试图观察 div 的“offsetParent”属性的变化,但我似乎无法让它工作。
基本上我的目标是在 offsetParent 不为空之后执行一个函数。
this.element.callFunctionWhenOffsetParentChanges( _ => {
if(this.element.offsetParent){
this.doSomething();
}
});
Run Code Online (Sandbox Code Playgroud)
我尝试使用这样的 MutationObserver:
const observer = MutationObserver( _ => {
if(this.element.offsetParent){
this.doSomething();
}
});
observer.observe(this.element, {attributeFilter: ['offsetParent']})
Run Code Online (Sandbox Code Playgroud)
但它不会在 offsetParent 设置时触发(例如,当元素真正可见时)。
如果有必要,我可以发布更多代码,但也许我只是在 MutationObserver 中遗漏了一些东西,或者有另一种方法可以对设置的 offsetParent 做出反应。
在此先感谢您的帮助!
我目前正在重构遗留代码以使用Android架构组件,并在一种存储库模式中设置房间数据库和排球请求.因此,表示/域层要求存储库获取LiveData-Objects以观察或告诉他与服务器同步,之后删除旧的db条目并从服务器重新获取所有当前的条目.
我已经为同步部分编写了测试,所以我确信,对象会被正确获取并插入到数据库中.但是当编写一个测试来观察那个db表的条目时(并测试对象是否正确保存了所有需要在将它们放入db之前完成的)LiveData>我正在观察,不会被触发.
在下面的代码片段中,您可以假设,synchronizeFormsWithServer(...)方法可以正常工作,并且异步执行数据库操作.它包含从db中删除所有Form-Objects的操作,这些操作从服务器获取的Forms列表中不存在并插入所有新的Form-Objects.因为在测试开始时数据库是空的,所以这应该不重要
观察者未被触发的测试:
@Test
public void shouldSaveFormsFromServerIntoDb() throws Exception
{
Lifecycle lifecycle = Mockito.mock(Lifecycle.class);
when(lifecycle.getCurrentState()).thenReturn(Lifecycle.State.RESUMED);
LifecycleOwner owner = Mockito.mock(LifecycleOwner.class);
when(owner.getLifecycle()).thenReturn(lifecycle);
final CountDownLatch l = new CountDownLatch(19);
formRepository.allForms().observe(owner, formList ->
{
if (formList != null && formList.isEmpty())
{
for (Form form : formList)
{
testForm(form);
l.countDown();
}
}
});
formRepository.synchronizeFormsWithServer(owner);
l.await(2, TimeUnit.MINUTES);
assertEquals(0, l.getCount());
}
Run Code Online (Sandbox Code Playgroud)
FormRepository代码:
@Override
public LiveData<List<Form>> allForms()
{
return formDatastore.getAllForms();
}
Run Code Online (Sandbox Code Playgroud)
数据存储区:
@Override
public LiveData<List<Form>> getAllForms()
{
return database.formDao().getAllForms();
}
Run Code Online (Sandbox Code Playgroud)
formDao代码(数据库实现了你从房间里期待它的方式):
@Query("SELECT * FROM form")
LiveData<List<Form>> getAllForms(); …Run Code Online (Sandbox Code Playgroud) android observable android-room android-livedata android-architecture-components
在花了几个小时研究这个问题后,我决定提出问题,而不是在这样一个(看似)简单的任务上浪费更多时间。
我正在尝试实现一个带有透明背景且没有 Broder 的 FloatingActionButton,以仅显示一个自定义图标。我知道材料设计不鼓励它,但我需要这样做。
我面临的问题是有阴影显示,这是我不想要的。由于我将高程设置为 0dp,我不知道它是如何到达那里的,因此我不知道如何将其删除。
这是包含 FAB 的片段的代码:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_sample"
android:elevation="0dp"
android:fitsSystemWindows="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/fragment_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:gravity="center"
android:padding="10dp"
android:stretchMode="columnWidth"
android:verticalSpacing="25dp"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
style="@style/AppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:adjustViewBounds="true"
android:elevation="0dp"
android:fitsSystemWindows="true"
android:scaleType="fitCenter"
app:borderWidth="0dp"
app:fabSize="normal"
app:layout_anchorGravity="bottom|center"
app:layout_behavior="ScrollAwareFABBehavior"
/>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
这是我的 AppTheme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:fitsSystemWindows">true</item>
<item name="android:spotShadowAlpha">0</item>
<item name="android:shadowRadius">0</item>
<item name="android:ambientShadowAlpha">0</item>
<item name="selectableItemBackgroundBorderless">@null</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我在其中设置透明性的 Fragment 的 onCreateView:
@Override
public final View onCreateView(final LayoutInflater inflater, final ViewGroup …Run Code Online (Sandbox Code Playgroud) android shadow material-design floating-action-button android-elevation
android ×2
android-architecture-components ×1
android-room ×1
callback ×1
html ×1
javascript ×1
observable ×1
shadow ×1