主细节流程的View分页内部细节片段被调用但未显示

时间:2018-07-30 12:39:16

标签: android android-fragments android-viewpager master-detail

我已在似乎正在运行的主详细信息流程活动的细节中设置了一个视图分页器(打印日志),但该视图未出现,因此这里是保存该视图分页器的配方详细信息布局详细片段

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".recipeDetailActivity"
tools:ignore="MergeRootFrame">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="recipe detail"
    android:textSize="22sp"/>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

</android.support.design.widget.CoordinatorLayout>

当我运行应用程序时,上面的文本视图确实显示在布局中,视图分页器应为列表中的每个项目加载布局(列表中肯定有项目),此布局也具有较大的文本视图(仅用于测试目的),但此布局未显示,这是我的视图分页器的整个适配器,因为里面没有任何复杂的东西

public class CustomPagerAdapter extends PagerAdapter {

private Context mContext;
private ArrayList<Step> steps;
private String TAG = "CstmPgAdptr";

public CustomPagerAdapter(Context context, ArrayList<Step> adapterSteps) {
    mContext = context;
    this.steps = adapterSteps;
}

@Override
public Object instantiateItem(ViewGroup collection, int position) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    Step step = steps.get(position);
    ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.recipe_detail, collection,false);
    TextView descriptionText = layout.findViewById(R.id.recipe_detail);
    descriptionText.setText(step.getDescription());
    Log.d(TAG,"new view called");
    return layout;
}

@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
    collection.removeView((View) view);
}

@Override
public int getCount() {
    //return CustomPagerEnum.values().length;
    return steps.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public CharSequence getPageTitle(int position) {
    return steps.get(position).getShortDescription();
}

} 

当我在保存视图分页器的片段上滑动时,此日志为Log.d(TAG,“称为新视图”);每次都会打印,这意味着它正在工作但没有显示,而且在任何布局中都没有滚动视图(可能会出现问题)

编辑

添加了食谱详细信息布局

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".recipeDetailActivity"
tools:ignore="MergeRootFrame">


<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/detail_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.AppBarLayout>

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
    android:id="@+id/video_view"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:layout_margin="@dimen/default_padding"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/app_bar" />

<TextView
    android:id="@+id/recipe_detail_text"
    android:gravity="center"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/default_padding"
    android:textIsSelectable="true"
    app:layout_constraintTop_toBottomOf="@id/video_view"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

<TextView
    android:id="@+id/recipe_detail2"
    android:gravity="center"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/default_padding"
    android:text="@string/app_name"
    android:textIsSelectable="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />



</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

好的,那是我的错。我添加了这个

collection.addView(layout);

它工作正常