我正在尝试将DataBinding添加到我的应用中。在我的应用程序中,我有一个BaseActivity,它有一个工具栏和一个FrameLayout。 FrameLayout是活动的容器'它扩展了BaseActivity。如何在我的BaseActivity和扩展活动中添加数据绑定?
我将在没有DataBinding的情况下共享我的代码:
这是我的 BaseActivity.java :
public class BaseActivity extends AppCompatActivity {
@Override
public void setContentView(@LayoutRes int layoutResID) {
LinearLayout container = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
FrameLayout activityContent = (FrameLayout) container.findViewById(R.id.activityContent);
getLayoutInflater().inflate(layoutResID, activityContent, true);
super.setContentView(container);
Toolbar toolbar = (Toolbar) container.findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(menuItem);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
}
这是 activity_base.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<include
android:id="@+id/toolbarLayout"
layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/activityContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</layout>
以下是扩展BaseActivity的活动: CardRecyclerViewActivity.java :
public class CardRecyclerViewActivity extends BaseActivity {
RecyclerView recyclerView;
ReplikAdapter adapter;
ArrayList<Replik> replikListesi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_recyclerview);
replikListesiniDoldur();
adapter = new ReplikAdapter(replikListesi, this, R.layout.item_card_replik);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setAdapter(adapter);
setTitle(R.string.cardRecylerView);
}
public void replikListesiniDoldur() {
replikListesi = new ArrayList<Replik>();
replikListesi.add(new Replik(R.drawable.harvey1, "Ben ihtimallere oynamam. Adama oynarım.", "Harvey Specter"));
replikListesi.add(new Replik(R.drawable.harvey2, "Avukatlık doktorluğa çok benzer, acıtana kadar bastırırsın ve böylece nereye bakman gerektiğini anlarsın.", "Harvey Specter"));
replikListesi.add(new Replik(R.drawable.harvey3, "İşte aramızdaki fark bu; Sen küçük kaybetmek istiyorsun ben ise büyük kazanmak.", "Harvey Specter"));
replikListesi.add(new Replik(R.drawable.harvey4, "Benim hayallerim yok, hedeflerim var.", "Harvey Specter"));
}
}
Okey,我将DataBinding添加到我的 BaseActivity.java 中:
public class BaseActivity extends AppCompatActivity {
@Override
public void setContentView(@LayoutRes int layoutResID) {
ActivityBaseBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_base, null, false);
getLayoutInflater().inflate(layoutResID, binding.activityContent, true);
super.setContentView(binding.getRoot());
setSupportActionBar(binding.toolbarLayout.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(menuItem);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
}
使用DataBinding activity_base.xml :
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/toolbarLayout"
layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/activityContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</layout>
但我不确定如何将DataBinding添加到此场景中的扩展活动中。你能帮帮我吗?
答案 0 :(得分:4)
这个问题可能会迟到。
您的 BaseActivity 应如下所示。
public abstract class BaseActivity<B extends ViewDataBinding, T extends BaseViewModel> extends AppCompatActivity {
protected B dataBinding;
protected T baseViewModel;
protected void bindView(int layoutId) {
dataBinding = DataBindingUtil.setContentView(this, layoutId);
}
@Override
protected void onDestroy() {
baseViewModel.detachView();
super.onDestroy();
}
}
您的 MainActivity 应如下所示。
public class MainActivity extends BaseActivity<ActivityMainBinding, MainActivityViewModel> implements MainActivityView, View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bindView(R.layout.activity_main);
baseViewModel = new MainActivityViewModel();
baseViewModel.attachView(this);
dataBinding.btnPerformOperation.setOnClickListener(this);
}
@Override
public void OnDataLoad(String item) {
dataBinding.setIsLoading(false);
startActivity(new Intent(MainActivity.this, SecondActivity.class).putExtra("result", item));
}
@Override
public void OnError(Throwable throwable) {
}
@Override
public void onClick(View v) {
dataBinding.setIsLoading(true);
baseViewModel.loadNextData();
}
}
参考源代码可以从here下载。
希望这会对你有所帮助。
答案 1 :(得分:3)
怎么样?
<强> BaseActivityViewModel.java 强>
href="mypages/stu/sturegform.php"
<强> activity_base.xml 强>
public class BaseActivityViewModel {
public void onClick() {
Log.i("BaseActivityViewModel | onClick", "111111111111");
}
}
BaseActivity.java
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="model"
type="kr.changhoonjin.textmvvmwithextend.BaseActivityViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{()->model.onClick()}"
android:text="base"/>
<FrameLayout
android:id="@+id/layout_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</layout>
<强> MainActivityViewModel.java 强>
public class BaseActivity extends AppCompatActivity {
private ActivityBaseBinding binding;
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_base);
binding.setModel(new BaseActivityViewModel());
}
protected <T extends ViewDataBinding> T putContentView(@LayoutRes int resId) {
return DataBindingUtil.inflate(getLayoutInflater(), resId, binding.layoutContainer, true);
}
}
<强> activity_main.xml中强>
public class MainActivityViewModel {
public void onClick2() {
Log.i("MainActivityViewModel | onClick2", "2222222222222");
}
}
<强> MainActivity.java 强>
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="model"
type="kr.changhoonjin.textmvvmwithextend.MainActivityViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{()->model.onClick2()}"
android:text="main"/>
</LinearLayout>
</layout>
答案 2 :(得分:-1)
它与您在BaseActivity中所做的相同,而不是inflate
只使用setContentView
private ActivityCardRecyclerviewBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this,R.layout.activity_card_recyclerview);
}