在单击按钮时使用数据绑定到 运行 方法

Using data binding to run method on button click

尝试从我的视图模型在布局文件中使用 运行 方法,但是,我一直从我的布局文件中获取 error: cannot find symbol class ViewModels。布局文件是一个片段。我尝试使缓存无效并重新同步,但没有帮助。我需要向片段本身添加任何东西吗?我看到人们使用数据绑定来启动片段,但我已经阅读了它的可选内容。

更新:拿出OnClick方法测试,还是报错。我想问题出在我的减速上,但我不知道为什么。当我编辑布局时,当我在其中输入视图模型名称时会显示路径。

Update2:尝试将变量中的类型设置为等于 activity 的路径,该路径启动用于测试的片段并且构建得很好。必须有一种方法可以添加不是我的 activity.

的导入

布局文件

<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
    <variable
        name="viewTest"
        type="rangers.socmanv2.ViewModels.BattleRhythmViewModel" />
</data>
<android.support.constraint.ConstraintLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/battleRhythmMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.BattleRhythmFrag">

<Button
    android:id="@+id/newBattle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="76dp"
    android:layout_marginLeft="76dp"
    android:layout_marginTop="88dp"
    android:text="New Battle Rhythm"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
  />


<Button
    android:id="@+id/editBattle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="76dp"
    android:layout_marginLeft="76dp"
    android:layout_marginTop="50dp"
    android:text="Edit Battle Rhythm"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/newBattle" />

<Button
    android:id="@+id/deleteBattle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="76dp"
    android:layout_marginLeft="76dp"
    android:layout_marginTop="50dp"
    android:text="Delete Battle Rhythm"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editBattle" />

</android.support.constraint.ConstraintLayout>

</layout>

片段

public class BattleRhythmFrag extends Fragment {

private BattleRhythmViewModel mViewModel;
private View view;
private Button test;

public static BattleRhythmFrag newInstance() {
    return new BattleRhythmFrag();
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {


    view =  inflater.inflate(R.layout.battle_rhythm_fragment, container, false);

    mViewModel = new BattleRhythmViewModel();



    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mViewModel = ViewModelProviders.of(this).get(BattleRhythmViewModel.class);
    // TODO: Use the ViewModel
}


}

查看模型


public class BattleRhythmViewModel extends ViewModel {


    public void launchNewFragment(FragmentManager fragM)
    {
        Log.d("viewmodel","hitting launchNewFrag");
        HomeFrag test = new HomeFrag();
        fragM.beginTransaction().replace(R.id.homeContent,test,"launched"+test);

    }

    public void test()
    {Log.d("viewmodel","hitting test");
    }


    }

您的错误在 viewModel 构造函数中。 您必须添加一个默认构造函数,因为您没有添加任何工厂 ViewModelProvider.Factory 但您的 BattleRhythmViewModel 没有任何默认构造函数。

答案 1:


public class BattleRhythmViewModel extends ViewModel {

    public void launchNewFragment(){
    }

    public void test()
    {Log.d("viewmodel","hitting test");
    }
    }
 }

答案 2: 比答案 1 更好,因为您不需要添加默认值并且可以在模型中使用您的片段


public class Factory implements ViewModelProvider.Factory {
    private FragmentManager fragM;

    public Factory(FragmentManager fragM) {
        this.fragM= fragM;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(BattleRhythmViewModel.class)) {
            return (T) new BattleRhythmViewModel(fragM);
        }
        throw new IllegalArgumentException("Unknown ViewModel class");
    }
}

将您的视图模型创建者行更改为

    Factory factory = new Factory(this)
    mViewModel = ViewModelProviders.of(this,factory).get(BattleRhythmViewModel.class);