我需要获得伟大的祖父母的观点的参考。目前我正在使用下面的代码成功地做到这一点:最好的方式来获得视图的伟大的大父母视图?
FabToolbar fabToolbar = (FabToolbar) view.getParent().getParent().getParent().getParent();
这似乎有些荒谬,或者,它只是它的方式?
我之所以这样说是因为我的偏好要使用的数据在我的布局结合了,我无法获得引用任何看法,但认为被点击。这给出了下面的代码,你将在下面看到,例如:
@{(view) -> viewModel.onClickPart(view)}
的观点被传递到方法是MaterialIconView,这似乎是我可以引用的唯一看法,还是有办法通过附加这个方法的参数?我的完整布局如下,请记住,FabToolbar包含一个LinearLayout子项和一个RelativeLayout子项,因此为什么看到四个调用getParent()而不是两个(查看下面的布局)。
<com.bowyer.app.fabtoolbar.FabToolbar
android:id="@+id/fab_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:tb_anim_duration="400"
app:tb_color="@color/primary"
app:tb_container_gravity="center"
app:tb_fab_type="normal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/accent"
android:gravity="center"
android:orientation="horizontal">
<net.steamcrafted.materialiconlib.MaterialIconView
android:id="@+id/ic_part"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="@{(view) -> viewModel.onClickPart(view)}"
android:layout_marginLeft="@dimen/content_space"
android:layout_marginRight="@dimen/content_space"
android:background="@null"
android:scaleType="center"
app:materialIcon="engine"
app:materialIconColor="#fff"
app:materialIconSize="24dp"
/>
<net.steamcrafted.materialiconlib.MaterialIconView
android:id="@+id/ic_dyno"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="@{(view) -> viewModel.onClickDyno(view)}"
android:layout_marginLeft="@dimen/content_space"
android:layout_marginRight="@dimen/content_space"
android:background="@null"
android:scaleType="center"
app:materialIcon="chart_line"
app:materialIconColor="#fff"
app:materialIconSize="24dp"
/>
<net.steamcrafted.materialiconlib.MaterialIconView
android:id="@+id/ic_timeslip"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="@{(view) -> viewModel.onClickTimeslip(view)}"
android:layout_marginLeft="@dimen/content_space"
android:layout_marginRight="@dimen/content_space"
android:background="@null"
android:scaleType="center"
app:materialIcon="timer_10"
app:materialIconColor="#fff"
app:materialIconSize="24dp"
/>
<net.steamcrafted.materialiconlib.MaterialIconView
android:id="@+id/ic_laptime"
android:layout_width="48dp"
android:onClick="@{(view) -> viewModel.onClickLaptime(view)}"
android:layout_height="48dp"
android:layout_marginLeft="@dimen/content_space"
android:layout_marginRight="@dimen/content_space"
android:background="@null"
android:scaleType="center"
app:materialIcon="timer"
app:materialIconColor="#fff"
app:materialIconSize="24dp"
/>
<net.steamcrafted.materialiconlib.MaterialIconView
android:id="@+id/ic_fuel"
android:layout_width="48dp"
android:onClick="@{(view) -> viewModel.onClickFuel(view)}"
android:layout_height="48dp"
android:layout_marginLeft="@dimen/content_space"
android:layout_marginRight="@dimen/content_space"
android:background="@null"
android:scaleType="center"
app:materialIcon="gas_station"
app:materialIconColor="#fff"
app:materialIconSize="24dp"
/>
<net.steamcrafted.materialiconlib.MaterialIconView
android:id="@+id/ic_repair"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="@{(view) -> viewModel.onClickRepair(view)}"
android:layout_marginLeft="@dimen/content_space"
android:layout_marginRight="@dimen/content_space"
android:background="@null"
android:scaleType="center"
app:materialIcon="wrench"
app:materialIconColor="#fff"
app:materialIconSize="24dp"
/>
</LinearLayout>
</com.bowyer.app.fabtoolbar.FabToolbar>
谢谢你的建议。
嘿 - 我有第一个不久后添加一个答案。你已经阅读过吗?可以在onclick监听器中传递视图。 :) – yennsarah