新导航组件的拟议实践在I/O中提供,具有以下模板和建议的理念:
典型的应用程序通常具有包含CollapsingToolbar的详细视图.如何在该架构下构建它?
android android-architecture-components android-jetpack android-architecture-navigation
是否可以使用新的Android架构组件和房间持久性库将Enum类型用作Entity类中的嵌入字段?
我的实体(带有嵌入式枚举):
@Entity(tableName = "tasks")
public class Task extends SyncEntity {
@PrimaryKey(autoGenerate = true)
String taskId;
String title;
/** Status of the given task.
* Enumerated Values: 0 (Active), 1 (Inactive), 2 (Completed)
*/
@Embedded
Status status;
@TypeConverters(DateConverter.class)
Date startDate;
@TypeConverters(StatusConverter.class)
public enum Status {
ACTIVE(0),
INACTIVE(1),
COMPLETED(2);
private int code;
Status(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的TypeConverter:
public class StatusConverter {
@TypeConverter
public static Task.Status toStatus(int status) {
if …Run Code Online (Sandbox Code Playgroud) 我一直在努力思考将Android服务放在新的Android推荐架构中的位置.我提出了许多可能的解决方案,但我无法决定哪一个是最好的方法.
我做了很多研究,我找不到任何有用的指导方针或教程.我发现关于将服务放置在我的应用程序架构中的唯一提示是@JoseAlcerreca Medium post
理想情况下,ViewModels不应该对Android有任何了解.这提高了可测试性,泄漏安全性和模块化.一般的经验法则是确保ViewModel中没有android.*导入(例如android.arch.*).这同样适用于演示者.
根据这一点,我应该将我的Android服务置于我的架构组件层次结构的顶层,与我的活动和碎片处于同一级别.这是因为Android服务是Android框架的一部分,所以ViewModels不应该知道它们.
现在,我将简要介绍一下我的场景,但只是为了让全景更清晰,而不是因为我想要这个特定场景的答案.
以下是我能想到的3种不同架构:
片段和AndroidService之间共享ViewModel

我很确定我应该将它们放在架构之上并将它们视为一个Activity/Fragment,因为BoundServices是Android Framework的一部分,它们由Android操作系统管理,并且它们与其他活动和片段绑定.在这种情况下,我不知道与LiveData,ViewModels和Activities/Fragments交互的最佳方式是什么.
有些人可能认为它们应该被视为一个数据源(因为在我的情况下,它使用蓝牙从一个规模获取数据),但我不认为这是一个好主意,因为我在上一段中所说的都是特别是因为它在这里所说的:
避免将应用的入口点(如活动, 服务和广播接收器)指定为数据源.相反,它们应该只与其他组件协调以检索与该入口点相关的数据子集.每个应用程序组件都是相当短暂的,具体取决于用户与其设备的交互以及系统的整体当前运行状况.
所以,最后,我的问题是:
我们应该在哪里放置我们的Android(绑定)服务以及它们与其他架构组件的关系?这些替代品中的任何一种都是好方法吗?
android android-service android-architecture android-architecture-components android-jetpack
找不到新的Android架构组件的源代码.它发表了吗?如果是的话,在哪里?
如果源是在AOSP中,请指定我应该使用哪个特定的repo项目repo sync [PROJ_NAME]才能获得它.
我正在尝试使用Architecture Components,我想为RecyclerView的每个项目构建一个ViewModel.我不确定这是否正式正确或我应该坚持"旧方式".
我有这个适配器:
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {
private List<Post> list;
public static class PostViewHolder extends RecyclerView.ViewHolder{
final ItemPostBinding binding;
public PostViewHolder(ItemPostBinding binding){
super(binding.getRoot());
this.binding = binding;
}
}
@Override
public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ItemPostBinding binding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_post,
parent, false);
return new PostViewHolder(binding, parent.getContext());
}
@Override
public void onBindViewHolder(PostViewHolder holder, int position) {
holder.binding.setPost(list.get(position));
holder.binding.executePendingBindings();
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
public void setList(List<Post> list){ …Run Code Online (Sandbox Code Playgroud) android mvvm android-recyclerview android-architecture-components
我有一个案例,希望通过arch导航组件实现它.例如,我有2个导航图(主要和嵌套).我可以从嵌套中调用主图吗?如何? 例
android android-navigation android-architecture-components android-jetpack
假设我们有两个片段:MainFragment和SelectionFragment.第二个是用于选择某个对象的构建,例如整数.从第二个片段接收结果有不同的方法,如回调,总线等.
现在,如果我们决定使用导航架构组件导航到第二个片段,我们可以使用以下代码:
NavHostFragment.findNavController(this).navigate(R.id.action_selection, bundle)
Run Code Online (Sandbox Code Playgroud)
其中bundle是Bundle(当然)的实例.正如您所看到的那样,无法访问SelectionFragment我们可以进行回调的位置.问题是,如何使用Navigation Architecture Component接收结果?
android android-architecture-components android-jetpack android-architecture-navigation
Java POJO对象
public class Section {
@ColumnInfo(name="section_id")
public int mSectionId;
@ColumnInfo(name="section_name")
public String mSectionName;
public int getSectionId() {
return mSectionId;
}
public void setSectionId(int mSectionId) {
this.mSectionId = mSectionId;
}
public String getSectionName() {
return mSectionName;
}
public void setSectionName(String mSectionName) {
this.mSectionName = mSectionName;
}
}
Run Code Online (Sandbox Code Playgroud)
我的查询方法
@Query("SELECT * FROM section")
LiveData<List<Section>> getAllSections();
Run Code Online (Sandbox Code Playgroud)
访问数据库
final LiveData<List<Section>> sections = mDb.sectionDAO().getAllSections();
Run Code Online (Sandbox Code Playgroud)
在下一行,我正在检查sections.getValue()哪个总是给我null,虽然我在DataBase中有数据,后来我得到了onChanged()方法中的值.
sections.observe(this, new Observer<List<Section>>() {
@Override
public void onChanged(@Nullable List<Section> sections){
}
});
Run Code Online (Sandbox Code Playgroud)
但是当我从查询中省略LiveData时,我正在按预期获取数据.查询方法:
@Query("SELECT * …Run Code Online (Sandbox Code Playgroud) android android-room android-livedata android-architecture-components
我正在使用新的Android Paging库来获得具有无限滚动功能的RecyclerView.我无法理解为什么当我像这样设置我的PagedList时,库不会触发loadAfter()方法:
val config: PagedList.Config = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPageSize(10)
.setPrefetchDistance(1)
.setInitialLoadSizeHint(10)
.build()
val pageList = PagedList.Builder(LookDataSource(lookList), config)
.setInitialKey(0)
.setMainThreadExecutor(MainThreadExecutor())
.setBackgroundThreadExecutor(PaginationThreadPoolExecutor())
.setBoundaryCallback(LookBoundaryCallback())
.build()
Run Code Online (Sandbox Code Playgroud)
注意:我的prefetchDistance是1,因为我想在看到最后一个项目时加载另一批数据.文档说0将永远不会加载更多数据
我的DataSource是一个PageKeyedDataSource,键是页面的索引.
我已经调查的源代码ContiguousPagedList这是当你使用一个PageKeyedDataSource,我无法理解这些所产生的特定PagedList:
@MainThread
@Override
protected void loadAroundInternal(int index) {
int prependItems = mConfig.prefetchDistance - (index - mStorage.getLeadingNullCount());
int appendItems = index + mConfig.prefetchDistance
- (mStorage.getLeadingNullCount() + mStorage.getStorageCount());
mPrependItemsRequested = Math.max(prependItems, mPrependItemsRequested);
if (mPrependItemsRequested > 0) {
schedulePrepend();
}
mAppendItemsRequested = Math.max(appendItems, mAppendItemsRequested);
if (mAppendItemsRequested > 0) {
scheduleAppend();
}
}
Run Code Online (Sandbox Code Playgroud)
根据我的配置,当看到我的最后一项时,appendItems …
pagination android android-recyclerview android-architecture-components
我有一个BottomNavigationView和一个应用程序ViewPager.如何使用新的"导航架构组件"实现它?
什么是最佳做法?
非常感谢
android ×10
android-architecture-components ×10
android-architecture-navigation ×2
android-room ×2
mvvm ×1
pagination ×1
repo ×1