我不明白如何使用LayoutBuilder来获取Widget的高度.
我需要显示小部件列表并获得它们的高度,以便我可以计算一些特殊的滚动效果.我正在开发一个包,其他开发人员提供小部件(我不控制它们).我读到LayoutBuilder可以用来获得高度.
在非常简单的情况下,我试图在LayoutBuilder.builder中包装Widget并将其放入Stack中,但我总是得到minHeight 0.0,并且maxHeight INFINITY.我是否滥用LayoutBuilder?
编辑:看起来LayoutBuilder似乎不行.我发现CustomSingleChildLayout几乎是一个解决方案.
我扩展了该委托,我能够在getPositionForChild(Size size, Size childSize)方法中获得小部件的高度.但是,第一个调用的方法是Size getSize(BoxConstraints constraints)作为约束,我得到0到INFINITY,因为我在ListView中放置这些CustomSingleChildLayouts.
我的问题是SingleChildLayoutDelegate getSize像需要返回视图的高度一样运行.那时我不知道孩子的身高.我只能返回constraints.smallest(为0,高度为0)或constraints.biggest,这是无穷大并崩溃应用程序.
在文档中它甚至说:
...但是父母的大小不能取决于孩子的大小.
这是一个奇怪的限制.
我看到了从底部到顶部RecyclerView显示的某种方法ViewHolders.现在,我找不到它的任何地方(后经历时间的一半RecyclerView,RecyclerAdapter,LayoutManager...).
类似的问题已经被问到,但我不能让他们中的任何一个工作.
我想要的是在RecyclerView中选择项目,更改该项目视图的背景,并存储所选项目的位置.
主要问题是你有onCreateViewHolder(在适配器中),onBindViewHolder(在适配器中)和ViewHolder构造函数,并且每个人都在使用不同的方法.现在,我甚至不知道将onClickListener放在哪里(在以前的项目中我把它放在ViewHolder中),因为人们也在建议另外两种方法.
我的想法是将每个ViewHolder(或View)存储在列表中,这样我就可以引用每一行,并从那里更改背景.但这对我不起作用,因为当我尝试从三个地方(onCreateVH,onBindVH,VH类)中的任何一个添加到View(或ViewHolders)列表时,我的应用程序因某种原因崩溃(空指针ex).
有什么建议?在哪里以及如何实施它?
做一些足球的应用程序,我有几个Activities,Fragments而且RecyclerViews,只是在我的文字的这一个外壳颜色改变:

该片段正在显示徽标和名称.
但在中RecyclerView,你几乎看不到球员的名字,因为它太白了,其他3个小文也难以阅读.看起来文字改变了它的颜色以适应某些黑色主题,而通过我的所有应用程序它是默认的Theme.AppCompat.Light.DarkActionBar
编辑一切看起来很好:

并且一个视图持有者的代码看起来也很好:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:text="12: Tyler Blackett"
android:id="@+id/txt_players_name"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/txt_players_position"
android:layout_below="@+id/txt_players_name"
android:layout_alignLeft="@+id/txt_players_name"
android:layout_alignStart="@+id/txt_players_name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/txt_players_nationality"
android:layout_below="@+id/txt_players_position"
android:layout_alignLeft="@+id/txt_players_position"
android:layout_alignStart="@+id/txt_players_position" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/txt_players_value"
android:layout_below="@+id/txt_players_nationality"
android:layout_alignLeft="@+id/txt_players_nationality"
android:layout_alignStart="@+id/txt_players_nationality" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
编辑:适配器的一些代码.我多次使用这个适配器(情况0,1,2),只有在这种情况下,它正在改变文本的颜色:
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
RecyclerView.ViewHolder viewHolder …Run Code Online (Sandbox Code Playgroud) 我需要构建一个小部件来获取它的位图。我不在乎屏幕上的小部件。
所以我的问题是:我可以在不使用屏幕视图层次结构的情况下以某种方式“在侧面”构建小部件吗?
我没有找到办法做到这一点。所以,如果这不可能,我可以在屏幕上构建小部件,但实际上不显示它。我试过了,Visibility但这会使RenderObjectnull 。有了Offstage它打电话时会失败toImage()的断言:Failed assertion: line 2752 pos 12: ‘!debugNeedsPaint’: is not true.