首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用扩展childActivity的数据库显示BaseActivity?

如何使用扩展childActivity的数据库显示BaseActivity?
EN

Stack Overflow用户
提问于 2019-08-04 19:18:28
回答 1查看 1.9K关注 0票数 5

我使用一个基活动作为另一个活动"RecicpeActivity“的父活动,因为我在基活动中重写了setContentView方法,以便子活动可以使用它并传递它的布局,以便在框架布局中充气。子活动"RecicpeActivity“使用DataBinding设置其视图。我正在做的是尝试将子活动的布局膨胀为基本活动中的框架布局“容器”,但是数据绑定根本没有被考虑,因为我有一个白屏幕,即使我在调试时看到子活动的布局是框架布局的子活动。

  • 我尝试过两种方法:

首先,我尝试通过调用setContentView传递子活动的布局,并将传递的布局膨胀到基本活动中的框架布局。

第二个问题,我已经尝试在基本活动中使用数据绑定,但我认为这并不重要。

_ChildActivity

代码语言:javascript
复制
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

代码语言:javascript
复制
 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“

代码语言:javascript
复制
   <?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“

代码语言:javascript
复制
<?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“第二条途径”

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-04 19:42:09

充气后调用layout.requestLayout(),使其能够适应充气后所做的更改。

代码语言:javascript
复制
 getLayoutInflater().inflate(layoutResID, baseBinding.activityContent, true);
 mRelativeLayout.requestLayout();
 super.setContentView(baseBinding.getRoot());

公开无效requestLayout () 当某项更改使此视图的布局无效时,调用此视图。这将安排视图树的布局传递。

如果在调试过程中可以看到子程序处于继承状态,则应修复此问题。

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

第2部分:绑定不起作用

您正在膨胀视图两次,一次在您的子活动中,一次在您的BaseActivity中:

代码语言:javascript
复制
  mBinding = DataBindingUtil.inflate(
                getLayoutInflater(), 
                R.layout.activity_recipe, null, false);
    setContentView(R.layout.activity_recipe);

mBinding您为绑定而膨胀了一次,然后将布局id传递给setContentView,在那里它再次膨胀:

代码语言:javascript
复制
 getLayoutInflater().inflate(layoutResID, frameLayout, true);

因此,Databinding中的视图与添加到基本布局中的视图不同。

创建一个重载版本的setContentView,它接受视图而不是id.

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57349559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档