我有一个活动和一个片段。我正在使用MVP模式。我试图通过数据绑定该片段中abutton的onClick方法进行绑定,但无济于事。
我尝试过: A)将演示者的名称设置为演示者,这是片段中演示者的名称。 B)fragmentPresenter,它是活动中主持人的名字。
各个代码段:
活动
private HomeFragmentPresenter homeFragmentPresenter;
private HomeFragment homeFragment;
片段演示者
@Override
public void handleBookTitleEditButtonPress(View view) {
System.out.println("Pressed edit button");
DataRepository.ChangeCurrentBookName(newTitle);
}
片段xml
<data>
<variable
name="homeFragmentPresenter"
type="se.hkr.fictioner.data_fragments.home_fragment.HomeFragmentPresenter" />
</data>
<ImageButton
android:id="@+id/current_book_title_edit"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_edit_black_24dp"
android:onClick="@{homeFragmentPresenter::handleBookTitleEditButtonPress}"
android:contentDescription="TODO" />
有没有办法以这种方式做到这一点?我有2个按钮,我想将演示者的字段绑定到editText.text
答案 0 :(得分:0)
好吧,错误是我忘记将实际绑定添加到片段的onCreateView()中。这是来自使用相同方案的不同片段的示例。
旧
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(inflater,
R.layout.character_fragment,container,false);
recyclerView = view.findViewById(R.id.character_recycler_view);
recyclerView.setAdapter(adapter);
linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
return view;
}
新
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState){
CharacterFragmentBinding binding = DataBindingUtil.inflate(inflater,
R.layout.character_fragment,container,false);
View view = binding.getRoot();
binding.setPresenter(presenter);
recyclerView = view.findViewById(R.id.character_recycler_view);
recyclerView.setAdapter(adapter);
linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
return view;
}
出于不幸,让它在这里。