导航抽屉和Android数据绑定

时间:2017-03-08 13:00:58

标签: java android data-binding navigation-drawer android-databinding

抱歉我的英文。我需要使用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上的点击。如何正确实现?

1 个答案:

答案 0 :(得分:0)

在“活动”中添加此代码,以便像这样进行数据绑定

@Override
protected 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>

这应该按照您的要求进行,您可以将文本和图像绑定到导航抽屉