gpt4 book ai didi

android - 如何显示带有扩展 BaseActivity 的数据绑定(bind)的 childActivity?

转载 作者:行者123 更新时间:2023-11-29 00:53:23 25 4
gpt4 key购买 nike

我正在使用一个基本 Activity 作为另一个 Activity “RecicpeActivity”的父 Activity ,因为我已经覆盖了基本 Activity 中的 setContentView 方法,以便子 Activity 可以使用它并传递其布局以在框架布局中膨胀。子 Activity “RecicpeActivity”使用 DataBinding 来设置其 View 。我正在做的是我试图将子 Activity 的布局膨胀到基本 Activity 中的框架布局“容器”但是数据绑定(bind)根本没有被考虑因为我有一个白屏即使我已经看到了调试时将子 Activity 布局作为框架布局的子布局。

  • 我试过两种方法:

1- 第一个我尝试通过调用 setContentView 简单地传递子 Activity 的布局,并将传递的布局扩展到基础 Activity 中的框架布局。

2- 第二个是我尝试在基本 Activity 中使用数据绑定(bind),但我认为这无关紧要。

_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;
}

_BaseActivity

 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);
}

_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>

_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>

_BaseActivity 与数据绑定(bind)“第二种方式”

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);
}

最佳答案

在膨胀后调用 layout.requestLayout() 以适应膨胀后所做的更改。

 getLayoutInflater().inflate(layoutResID, baseBinding.activityContent, true);
mRelativeLayout.requestLayout();
super.setContentView(baseBinding.getRoot());

public void requestLayout ()
Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.

如果您在调试时可以在 View 层次结构中看到子项,那么这应该可以解决问题。

https://developer.android.com/reference/android/view/View.html#requestLayout()

第 2 部分:绑定(bind)不起作用

您将 View 膨胀两次,一次是在您的子 Activity 中,一次是在您的 BaseActivity 中:

  mBinding = DataBindingUtil.inflate(
getLayoutInflater(),
R.layout.activity_recipe, null, false);
setContentView(R.layout.activity_recipe);

mBinding 您为数据绑定(bind)膨胀了一次,然后您将布局 ID 传递给 setContentView,它再次膨胀:

 getLayoutInflater().inflate(layoutResID, frameLayout, true);

因此数据绑定(bind)中的 View 与您添加到基本布局中的 View 不同。

创建一个接受 View 而不是 id 的 setContentView 的重载版本。

关于android - 如何显示带有扩展 BaseActivity 的数据绑定(bind)的 childActivity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57349559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com