对不起我的英语不好.我需要使用Android数据绑定处理按钮单击.我在content_main.xml中包含了数据绑定.
<data> <variable name="temp" type="skubyev.anton.navigationdrawer.DataBindingClass.MainActivityData" /> <variable name="presenter" type="skubyev.anton.navigationdrawer.DataBindingClass.MainActivityPresenter"/> </data>.... <androID.support.design.Widget.floatingActionbutton androID:onClick="@{() -> presenter.onShowData(temp)}"
但是MainActivity.java
@OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ContentMainBinding binding = DataBindingUtil.setContentVIEw(this, R.layout.content_main); MainActivityPresenter mainActivityPresenter = new MainActivityPresenter(this); MainActivityData mainActivityData = new MainActivityData("Иди нахуй"); binding.setTemp(mainActivityData); binding.setPresenter(mainActivityPresenter); setContentVIEw(R.layout.activity_main);.... @OverrIDe public voID showData(MainActivityData mainActivityData) { String textForRecognition = mainActivityData.getTextForRecognition(); Toast.makeText(this, textForRecognition, Toast.LENGTH_SHORT).show(); }
我想要showData运行,然后点击content_main中的floatingActionbutton.但showData – 类MainActivity中的函数.因此,据我所知,当您单击content_main.xml上的按钮时,不会调用它.我该怎么办?创建一个单独的类ContentActivity.java whith函数showData.
我只想要MainActivity.java负责处理content_main上的点击.如何正确实现?
解决方法:
在Activity中添加此代码用于数据绑定
@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding activityMainBinding = DataBindingUtil.setContentVIEw(this, R.layout.activity_main); Mainviewmodel mainviewmodel = viewmodelProvIDers.of(this).get(Mainviewmodel.class); activityMainBinding.setHandlers(handlers); activityMainBinding.setviewmodel(mainviewmodel); Toolbar toolbar = findVIEwByID(R.ID.toolbar); toolbar.setTitle("HOME"); DrawerLayout drawer = activityMainBinding.drawerLayout; ActionbarDrawerToggle toggle = new ActionbarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); NavigationVIEw navigationVIEw = activityMainBinding.navVIEw; NavheaderMainBinding navheaderMainBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.nav_header_main, activityMainBinding.navVIEw, true); navheaderMainBinding.navEmail.setText(session.getFacilityEmail()); navheaderMainBinding.navname.setText(session.getFacilityname()); navigationVIEw.setNavigationItemSelectedListener(this);}
在nav_hader_main.xml中你必须是这样的
<?xml version="1.0" enCoding="utf-8"?><layout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:app="http://schemas.androID.com/apk/res-auto"><data> <variable name="navigationheader" type="com.nudge.gurukul.vIEw.activitIEs.MainActivity" /> <import type="androID.vIEw.VIEw" /></data><linearLayout androID:layout_wIDth="match_parent" androID:layout_height="@dimen/nav_header_height" androID:background="@drawable/sIDe_nav_bar" androID:gravity="bottom" androID:orIEntation="vertical" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingBottom="@dimen/activity_vertical_margin" androID:theme="@style/themeOverlay.AppCompat.Dark"> <ImageVIEw androID:ID="@+ID/imageVIEw" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:contentDescription="@string/nav_header_desc" androID:paddingtop="@dimen/nav_header_vertical_spacing" app:srcCompat="@mipmap/app_logo_round" /> <TextVIEw androID:ID="@+ID/nav_name" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:paddingtop="@dimen/nav_header_vertical_spacing" androID:textAppearance="@style/TextAppearance.AppCompat.Body1" /> <TextVIEw androID:ID="@+ID/nav_email" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" /></linearLayout>
总结 以上是内存溢出为你收集整理的java – 导航抽屉和android数据绑定全部内容,希望文章能够帮你解决java – 导航抽屉和android数据绑定所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)