I need to obtain a reference to the great great grandparent's view. Currently I am doing it successfully using the following code:
FabToolbar fabToolbar = (FabToolbar) view.getParent().getParent().getParent().getParent();
This seems somewhat ridiculous, or, is it just the way it's done?
The reason I did it this way was due to my preference to want to use data binding in my layout, and my inability to obtain a reference to any view but the view being clicked. This is given the following code you will see below, ex:
@{(view) -> viewModel.onClickPart(view)}
The view being passed into the method is the MaterialIconView, this appears to be the only view I can reference, or is there a way to pass additional parameters to this method? My full layout is as below, please keep in mind that FabToolbar contains a LinearLayout child and a RelativeLayout child, hence why you see four calls to getParent() and not two (looking at the layout below).
<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>
Thank you for the advice.
You can pass other Views
from your layout with DataBinding
:
<com.bowyer.app.fabtoolbar.FabToolbar
android:id="@+id/fab_toolbar">
<LinearLayout>
<net.steamcrafted.materialiconlib.MaterialIconView
android:id="@+id/ic_part"
android:onClick="@{(view) -> viewModel.onClickPart(view, fabToolbar)}"/>
</LinearLayout>
</com.bowyer.app.fabtoolbar.FabToolbar>
In your ViewModel
:
public void onClickPart(View view, FabToolbar fabToolbar){ ... }
You need to use the camel case writing for any referenced view in data binding.
You can also pass properties of a view like that. (for example Visibility
, isChecked
, ... )
Here is an article from George Mount where he uses this (under "View attributes"). (Only for other properties, but I think you'll get the idea)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.