【问题标题】:View Pager inside detail fragment of master detail flow is called but not shown查看 Pager inside 主详细信息流的详细信息片段被调用但未显示
【发布时间】:2019-01-06 17:12:01
【问题描述】:

我已经在主详细信息流活动的详细信息中设置了一个视图寻呼机,它似乎正在工作(打印日志),但视图没有出现,所以这里是保存视图寻呼机的配方详细信息布局细节片段

<?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,"new view called");每次都会打印,这意味着它正在工作但没有显示,而且任何布局中都没有滚动视图(看来这可能是一个问题)

编辑

添加了配方详细布局

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

【问题讨论】:

  • 将您的 viewpager 和 textview 放入框架、线性或相对等其他布局中
  • 只是更改为相对布局仍然没有任何日志仍在打印
  • 您应该在视图寻呼机中为您的 recipe_detail 视图添加不同的视图。
  • 我添加了其他布局,我也尝试过膨胀任何旧的随机布局,它也没有显示

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


【解决方案1】:

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

collection.addView(layout);

它工作正常

【讨论】:

    猜你喜欢
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多