我想使用数据绑定库来更改工具栏标题。除底部导航视图的监听器外,一切正常,即我遇到了错误:
我认为这是由于使用了错误的setter名称来关联xml属性所致。在xml中,BottomNavigationView有
换句话说,我应该怎么做才能在ViewModel中编写代码,以便我可以控制对BottomNavigationView的单击?
以下是文件:
ActivityMain.java
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Could not resolve viewmodel::onNavigationClick as a listener.
file:D:\Replacements\app\src\main\res\layout\activity_main.xml
loc:85:40 - 85:67
****\ data binding error ****
我认为这是由于使用了错误的setter名称来关联xml属性所致。在xml中,BottomNavigationView有
app:onNavigationItemSelected
,但BottomNavigationView的setter是setOnNavigationItemSelectedListener
,而不是setOnNavigationItemSelected
。如果我是正确的,解决方案在Android文档中。但是我应该把这些注释放在哪里?在哪个类中?换句话说,我应该怎么做才能在ViewModel中编写代码,以便我可以控制对BottomNavigationView的单击?
以下是文件:
ActivityMain.java
public class ActivityMain extends AppCompatActivity {
private ActivityMainViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initiating ViewModel to Activity
viewModel = ViewModelProviders.of(this).get(ActivityMainViewModel.class);
// Initiating ContentView in Activity
setContentView(R.layout.activity_main);
// Initiating DataBinding for ContentView in Activity
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setViewmodel(viewModel);
}
}
ActivityMainViewModel.java
public class ActivityMainViewModel extends ViewModel {
private MutableLiveData<String> toolbarTitle = new MutableLiveData<>();
public String getToolbarTitle() {
return toolbarTitle.getValue();
}
public void setToolbarTitle(String toolbarTitle) {
this.toolbarTitle.setValue(toolbarTitle);
}
public boolean onNavigationClick(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_1:
setToolbarTitle("title 1");
return true;
case R.id.navigation_2:
setToolbarTitle("title 2");
return true;
case R.id.navigation_3:
setToolbarTitle("title 3");
return true;
}
return false;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="viewmodel" type="com.example.example.viewmodel.ActivityMainViewModel"/>
</data>
<android.support.constraint.ConstraintLayout
...>
<android.support.design.widget.AppBarLayout
...>
<android.support.v7.widget.Toolbar
android:id="@+id/main_toolbar"
android:title="@{viewmodel.toolbarTitle}"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/new_blue_light"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
...>
...
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:itemIconTint="@drawable/navigation_color_state"
app:itemTextColor="@drawable/navigation_color_state"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation"
app:onNavigationItemSelected="@{viewmodel::onNavigationClick}"
tools:layout_constraintBottom_creator="0"
tools:layout_constraintLeft_creator="0"
tools:layout_constraintRight_creator="0" />
</android.support.constraint.ConstraintLayout>
</layout>
我尝试做的唯一一件事就是创建自定义视图(应用程序可以运行,但它不会更改工具栏标题,并显示警告:warning: Application namespace for attribute app:onNavigationItemSelected will be ignored.
):
@BindingMethods({
@BindingMethod(
type = BottomNavigationViewBinding.class,
attribute = "app:onNavigationItemSelected",
method = "setOnNavigationItemSelectedListener"
),
})
public class BottomNavigationViewBinding extends BottomNavigationView{
private OnNavigationItemSelectedListener mSelectedListener;
public BottomNavigationViewBinding(Context context) {
this(context, null);
}
public BottomNavigationViewBinding(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BottomNavigationViewBinding(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public interface OnNavigationItemSelectedListener {
boolean onNavigationItemSelected(MenuItem item);
}
public void setOnNavigationItemSelectedListener(@Nullable OnNavigationItemSelectedListener listener) {
mSelectedListener = listener;
}
}