我试过这段代码但它没有用。在我的课堂上,我已实施onNavigationItemSelected
。
。我试过这个https://medium.com/@oldergod/bottomnavigationview-callback-and-and-databinding-9775b0c31d38。但这不起作用
仪表板布局......
<data>
<import type="android.view.View" />
<variable
name="navigationItemSelectedListener"
type="al_logistics.app.com.al_logistics.Dashboard"/>
</data>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header_manu"
app:navigationItemSelectedListener="@{navigationItemSelectedListener::onNavigationItemSelected}"
app:menu="@menu/list_menu"/>
这是我的onNavigationItemSelected
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
return true;
}
答案 0 :(得分:1)
请检查您是否已经完成此操作。您需要实现监听器,否则它将无法正常工作。如果解决方案有效,请告诉我
binding = DataBindingUtil.setContentView(this, R.layout.activity_dashboard);
binding.navView.setNavigationItemSelectedListener(this);
并从XML中删除此行:
app:navigationItemSelectedListener="@{navigationItemSelectedListener::onNavigationItemSelected}"
答案 1 :(得分:0)
问题是navigationItemSelectedListener要求提供监听器,而不是实际功能。 正确的使用方式是:
app:navigationItemSelectedListener="@{ menuItem -> navigationItemSelectedListener.onNavigationItemSelected(menuItem) }"
通过这种方式在数据绑定中创建侦听器。
其他解决方案(正如名称navigationItemSelectedListener
所暗示的那样)将用于实现BottomNavigationView.OnNavigationItemSelectedListener
然后在布局中
app:navigationItemSelectedListener="@{navigationItemSelectedListener}"