featureModule = 应用插件:'com.android.dynamic-feature'
libraryModule = 应用插件:'com.android.library'
问题- 在功能模块中使用任何库资源都需要完整的包名称限定符,这最终会给功能模块中的数据绑定生成类带来问题。
描述- 我有上面的应用程序结构。当我尝试在任何 featureModule 中使用任何库资源时,它需要指定具有完整包名称限定符的资源,因为默认情况下我将功能资源 R 作为主要导入。
因此,对于任何 kotlin 类,假设活动代码如下所示
功能 1/TestActivity.kt
internal class TestActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityTestBinding = DataBindingUtil.setContentView(this, R.layout.activity_test)
binding.testName.text = getText(R.string.abc_test) // this resource is present in core-lib module
}
}
Run Code Online (Sandbox Code Playgroud)
以上将导致未解决的引用错误,您可以通过使用全名而不是R.string.abc_test
使用来解决该错误com.blah.core-lib.R.string.abc_test
。
R.layout.activity_test
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/test_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/spacing_medium"
android:text="@{@string/abc_test}" />
</layout>
Run Code Online (Sandbox Code Playgroud)
但是对于 XML 数据绑定,它会产生更大的问题。由于R.layout.abc_test
在功能模块中,因此无法访问库资源。我可以通过使用实用程序类并让实用程序类返回带有完整包名称限定符的字符串来解决这个问题,但是这个问题限制了数据绑定的许多功能,我不能直接在 XML 本身中使用资源。
PS:如果我将我的功能模块转换为库模块,这一切都很好。即使在代码级别,如果它是一个库模块,我也不会使用完整的包名限定符。
有没有人有更好的建议来解决功能模块的这个问题?
我有一个小场景,我有以下结构,我试图在baseActivity Fragment中注入片段管理器,但由于某种原因,我运气不好:(
@Singleton
@Component(modules = { AppModule.class,
ActivityModule.class,
AndroidSupportInjectionModule.class })
public interface AppComponent extends AndroidInjector<App> {
@Override
void inject(App application);
@Component.Builder interface Builder {
@BindsInstance
AppComponent.Builder application(App application);
AppComponent build();
}
}
Run Code Online (Sandbox Code Playgroud)
ActivityModule.class
@PerActivity
@ContributesAndroidInjector(modules = BaseActivityModule.class)
abstract BaseActivity baseActivity();
Run Code Online (Sandbox Code Playgroud)
BaseActivityModule.class
static final String ACTIVITY_FRAGMENT_MANAGER = "ACTIVITY_FRAGMENT_MANAGER";
@PerActivity
@Named(ACTIVITY_FRAGMENT_MANAGER)
@Provides
static FragmentManager activityFragmentManager(BaseActivity activity) {
return activity.getSupportFragmentManager();
}
Run Code Online (Sandbox Code Playgroud)
BaseAcitivity.class
public abstract class BaseActivity extends DaggerAppCompatActivity {
@Named(ACTIVITY_FRAGMENT_MANAGER)
@Inject
FragmentManager fragmentManager;
}
Run Code Online (Sandbox Code Playgroud)
所以即使我在BaseActivityModule.class中提供我的片段管理器,dagger也会抛出以下错误.我甚至尝试使用Activity而不是BaseActivity作为BaseActivityModule中的输入参数.即使这样,我也会遇到同样的问题.不确定到底是什么搞砸了.所以任何帮助都表示赞赏.提前致谢 :)
Error:(17, 8) error: [dagger.android.AndroidInjector.inject(T)] @javax.inject.Named("ACTIVITY_FRAGMENT_MANAGER") android.support.v4.app.FragmentManager cannot …
Run Code Online (Sandbox Code Playgroud) 有没有可能使用livedata并行运行多个异步调用的方式?
假设我有4个异步调用。我想等到一切都完成后,再相应地使用所有4个调用的结果。
我能想到的一种方法是
public class MakeParallel {
private final CountDownLatch countDown = new CountDownLatch(4);
public void firstCall() {
Transformation.map(makeFirstCall(), input -> {
if(input.isSuccessful()) {
countDownLatch.countDown();
checkResult();
}
return input;
}
}
public void secondCall() {
Transformation.map(makeSecondCall(), input -> {
if(input.isSuccessful()) {
countDownLatch.countDown();
checkResult();
}
return input;
}
}
void checkResult() {
if(countDownLatch.getCount == 0) {
// Result is ready
} else {
// Something has error
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来解决这种情况?
java android android-livedata android-architecture-components
我想在具有角半径 (10dp) 的 Card 组件的上半部分添加边框。因此,只有底部缺失,卡片的其余部分的笔划为 1dp。(有点像U型和倒U型)
我想对具有底角半径且顶部缺失的卡片执行相同的操作。
我尝试设计自定义形状,但它不尊重卡片的形状。
// this is just a line but it doesn't respect the card corner radius?
private fun createShape(thickness: Float) : Shape {
return GenericShape { size, _ ->
moveTo(0f,0f)
lineTo(0f, size.height)
lineTo(thickness, size.height)
lineTo(thickness, 0f)
lineTo(0f, thickness)
}
}
val thickness = with(LocalDensity.current) {
1.dp.toPx()
}
Card(
shape = RoundedCornerShape(topEnd = 10.dp, topStart = 10.dp, bottomEnd = 0.dp, bottomStart = 0.dp),
modifier = Modifier
.border(BorderStroke(width = 1.dp, color = Color.Black), createShape(thickness))
) { …
Run Code Online (Sandbox Code Playgroud) 我试图为每个模块使用一个活动,其中每个活动都有自己的导航图。因此,导航从一个动态功能模块中的活动转到另一个动态功能模块中的另一活动。
功能 1/活动 A -> 功能 2/活动 B
ActivityA 只是调用基本startActivity
方法来启动 ActivityB。
因为从ActivityB
技术上讲,它的片段位于起始目的地。即使我的后退堆栈中有后退按钮,它也不会显示后退按钮ActivityA
。有什么办法可以解决这个问题吗?
PS:由于ActivityA
&ActivityB
位于两个独立的模块中,我避免将它们放在一张图表下。对于活动转换,我只是startActivity
通过intent
使用创建来调用className
。
更新- 即使我使用导航控制器从 ActivityA 的片段导航到 ActivityB,问题仍然存在。
</layout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/main_toolbar"
app:navigationIcon="@drawable/ic_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="@string/title" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/host_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="@navigation/landing_graph" />
</LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
internal class ActivityB : AppCompatActivity() {
private var navController : NavController? = null
override fun onCreate(savedInstanceState: Bundle?) …
Run Code Online (Sandbox Code Playgroud) 这是我的申请文件
private AppComponent component;
@Override
public void onCreate() {
super.onCreate();
setupGraph();
}
private void setupGraph() {
component = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
component.inject(this);
}
public AppComponent component() {
return component;
}
Run Code Online (Sandbox Code Playgroud)
应用程序组件:
@Singleton
@Component(
modules = {
AppModule.class
}
)
public interface AppComponent {
void inject(BaseActivity activity);
void inject(BaseFragment fragment);
}
Run Code Online (Sandbox Code Playgroud)
ActivityModule:
@Module public final class ActivityModule {
private final Activity activity;
public ActivityModule(Activity activity) {
this.activity = activity;
}
@Provides @ActivityContext
public Context provideActivityContext() {
return activity;
}
/** …
Run Code Online (Sandbox Code Playgroud) 问题:我想过滤列表中的列表。我所有的数据模型都是不可变的。
我的 JSON 结构如下所示
{
"root": [
{
"id": 2,
"val": 1231.12,
"fruit": [
{
"id": 2,
"name": "apple"
}
]
},
{
"id": 3,
"val": 1231.12,
"fruit": [
{
"id": 2,
"name": "apple"
},
{
"id": 3,
"name": "orange"
}
]
}
],
"fruits": [
{
"id": 1,
"name": "apple"
},
{
"id": 2,
"name": "guava"
},
{
"id": 3,
"name": "banana"
}
]
}
Run Code Online (Sandbox Code Playgroud)
问题陈述- 基本上,我想创建一个包含水果名称为苹果的所有根项目的列表。目前,我的天真解决方案看起来像这样。这涉及创建一个临时的可变列表,然后向其中添加特定项目。
下面的解决方案工作正常,但还有其他更好的方法来实现相同的目标。
val tempList = arrayListOf<RootItem>()
root?.forEach { item ->
item.fruit.filter …
Run Code Online (Sandbox Code Playgroud)