Mus*_*oud 5 data-binding android layout-inflater
我使用基础活动作为另一个活动“RecicpeActivity”的父活动,因为我已经覆盖了基础活动中的 setContentView 方法,以便子活动可以使用它并传递其布局以在框架布局中膨胀。子活动“RecicpeActivity”使用数据绑定来设置其视图。我正在做的是我试图将子活动的布局膨胀到基础活动中的框架布局“容器”中,但根本没有考虑数据绑定,因为我有一个白屏,即使我已经看到了调试时将子活动的布局作为框架布局的子项。
1- 第一个我尝试通过调用 setContentView 来传递子活动的布局,并将传递的布局膨胀到基础活动中的框架布局。
2- 第二个我尝试在基本活动中使用数据绑定,但我认为这无关紧要。
_ChildActivity
public class RecipeActivity extends BaseActivity {
private ActivityRecipeBinding mBinding;
private static final String RECIPE_INTENT_KEY = "recipe key";
private ScrollView mScrollView;
private RecipeViewModel recipeViewModel;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recipeViewModel =
ViewModelProviders.of(this).get(RecipeViewModel.class);
mBinding = DataBindingUtil.inflate(
getLayoutInflater(),
R.layout.activity_recipe, null, false);
setContentView(R.layout.activity_recipe);
// This method is implemented in the BaseActivity.
showProgressBar(true);
recipeViewModel.getRecipe().observe(this, new Observer<Recipe>() {
@Override
public void onChanged(Recipe recipe) {
if (recipe != null){
if (recipe.getRecipe_id().equals(
recipeViewModel.getRecipeId())){
mBinding.setRecipe(recipe);
mScrollView.setVisibility(View.VISIBLE);
showProgressBar(false);
}
}
}
});
recipeViewModel.getRecipeById(getIncomingIntentRecipeId());
}
private String getIncomingIntentRecipeId(){
if (getIntent().hasExtra(RECIPE_INTENT_KEY)){
String recipe_id = getIntent().getStringExtra(RECIPE_INTENT_KEY);
return recipe_id;
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
_基础活动
public abstract class BaseActivity extends AppCompatActivity {
public ProgressBar mProgressBar;
@Override
public void setContentView(int layoutResID) {
RelativeLayout mRelativeLayout =
(RelativeLayout) getLayoutInflater().inflate(
R.layout.activity_base, null);
FrameLayout frameLayout = mRelativeLayout.findViewById(
R.id.activity_content);
mProgressBar = mRelativeLayout.findViewById(R.id.progress_bar)
/**
* True means layoutResID should be inflated and made a part of
parent frameLayout
*/
getLayoutInflater().inflate(layoutResID, frameLayout, true);
super.setContentView(mRelativeLayout);
}
public void showProgressBar(boolean visibility){
mProgressBar.setVisibility(visibility ?
View.VISIBLE : View.INVISIBLE);
}
Run Code Online (Sandbox Code Playgroud)
_ChildActivity 布局“activity_recipe”
<?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="recipe"
type="com.mustafa.foodapp.models.Recipe" />
<import type="com.mustafa.foodapp.util.StringUtils" />
</data>
<ScrollView
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/recipe_image"
android:layout_width="match_parent"
android:layout_height="@dimen/recipe_image_height"
android:scaleType="center"
app:imageUrl="@{recipe.image_url}" />
<TextView
android:id="@+id/recipe_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/recipe_image"
android:padding="7dp"
android:text="@{recipe.title}"
android:textColor="#000"
android:textSize="@dimen/recipe_title_text_size" />
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/recipe_title"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="100">
<TextView
android:id="@+id/recipe_social_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:gravity="center"
android:text="@{String.valueOf(
Math.round(recipe.social_rank))}"
android:textColor="@color/red"
android:textSize="@dimen/
recipe_publisher_text_size"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ingredients_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/container"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{StringUtils.getStringIngredients(
recipe.ingredients)}" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
</layout>
Run Code Online (Sandbox Code Playgroud)
_BaseActivity 布局“activity_base”
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/base_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/activity_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ProgressBar
android:id="@+id/progress_bar"
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="125dp"
android:layout_height="125dp"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
_BaseActivity 与数据绑定“第二种方式”
public abstract class BaseActivity extends AppCompatActivity {
public ProgressBar mProgressBar;
public ActivityBaseBinding baseBinding;
@Override
public void setContentView(int layoutResID) {
baseBinding = DataBindingUtil.inflate(
getLayoutInflater(), R.layout.activity_base, null, false);
mProgressBar = baseBinding.progressBar;
/**
* True means layoutResID should be inflated and made a part of the
parent frameLayout
*/
getLayoutInflater().inflate(layoutResID, baseBinding.activityContent,
true);
super.setContentView(baseBinding.getRoot());
}
public void showProgressBar(boolean visibility){
mProgressBar.setVisibility(visibility ?
View.VISIBLE : View.INVISIBLE);
}
Run Code Online (Sandbox Code Playgroud)
充气后调用layout.requestLayout(),以便它可以适应充气后所做的更改。
getLayoutInflater().inflate(layoutResID, baseBinding.activityContent, true);
mRelativeLayout.requestLayout();
super.setContentView(baseBinding.getRoot());
Run Code Online (Sandbox Code Playgroud)
public void requestLayout ()
当某些内容发生更改导致该视图的布局无效时调用此方法。这将安排视图树的布局过程。
如果您在调试时可以在视图层次结构中看到子级,那么这应该可以解决问题。
https://developer.android.com/reference/android/view/View.html#requestLayout()
第 2 部分:绑定不起作用
您将视图膨胀两次,一次在您的子 Activity 中,一次在您的 BaseActivity 中:
mBinding = DataBindingUtil.inflate(
getLayoutInflater(),
R.layout.activity_recipe, null, false);
setContentView(R.layout.activity_recipe);
Run Code Online (Sandbox Code Playgroud)
mBinding 您为数据绑定膨胀了一次,然后将布局 id 传递给 setContentView ,它再次膨胀:
getLayoutInflater().inflate(layoutResID, frameLayout, true);
Run Code Online (Sandbox Code Playgroud)
因此,数据绑定中的视图与添加到基本布局中的视图不同。
创建 setContentView 的重载版本,它接受视图而不是 id。