Новый фрагмент полностью пуст при попытке раздувать макет - PullRequest
0 голосов
/ 08 июня 2019

У меня есть функция в моем классе RecyclerViewAdapter, которая заменяет мое текущее представление новым фрагментом элемента, по которому щелкают (из моего RecyclerView). Мой загруженный класс BlogFragment отображается как пустой, хотя у меня есть textView и imageView внутри макета, который надувается в OnCreateView. Я не понимаю, почему он выглядит пустым и не отображает в файле XML ничего, что он должен раздуть.

// this function is called when a user clicks on an item in my RecyclerView
private void launchBlogFragment(final View itemView) {

AppCompatActivity activity = (AppCompatActivity) itemView.getContext();
Fragment myfrag = new BlogFragment();

Log.i(TAG, "right before Before new blog fragment is launched");
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.pager, myfrag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}

Мой класс BlogFragment

public class BlogFragment extends Fragment {

public BlogFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.d(TAG, "In Blog Fragment Tab                ");
    // hides tab bar so BlogFragment can appear to be full screen
    ((MainActivity)getActivity()).hideTabBar();
    final View view = inflater.inflate(R.layout.fragment_blog, container, false);
    return view;
  }
}

мой файл фрагмента_blog.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/myLayout">

<ImageView
    android:id="@+id/cancelButton"
    android:paddingRight="5dp"
    android:paddingLeft="5dp"
    android:src="@drawable/ic_baseline_clear_24px"
    android:layout_width="190dp"
    android:layout_height="190dp"
    android:contentDescription="@string/image"
    />


<TextView
    android:id="@+id/blogTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:text=“EXAMPLE“
    android:paddingTop="5dp"
    android:textSize="72sp"
    android:paddingStart="5dp"
    android:paddingEnd="5dp"
    android:fontFamily="@font/caviar_dreams_bold"
    />

</FrameLayout>

MainActivity

public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /* Creating ViewPagerAdapter */
    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    /* Adding new fragments to tab layout */
    adapter.addFragment(new SocialMediaTab(), "Social Media Tab");
    adapter.addFragment(new dummyTab1(), "Dummy Tab 1");
    adapter.addFragment(new dummyTab2(), "Dummy Tab 2");
    adapter.addFragment(new dummyTab3(), "Dummy Tab 3");
    viewPager.setAdapter(adapter);

    /* Utilizing tab layout in activity_main to display data */
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

public void hideTabBar() {
    tabLayout.setVisibility(View.GONE);
}

activity_main.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">


<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="fixed"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:tabGravity="fill"
    app:tabTextColor="@color/white"
    app:tabSelectedTextColor="@color/highlightTab"
    />


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

</LinearLayout>

Мой LogCat один раз входит в мою функцию launchBlogFragment. (ни мой imageView, ни textView в фрагменте_blog.xml не отображаются)

2019-06-08 00:11:14.418 10179-10179/com.yourgesture.testloading I/ADAPTER: right before Before new blog fragment is launched
2019-06-08 00:11:14.433 10179-10179/com.yourgesture.testloading D/BLOG FRAGMENT: In Blog Fragment Tab                
2019-06-08 00:11:14.449 10179-10184/com.yourgesture.testloading I/zygote: Do full code cache collection, code=102KB, data=102KB
2019-06-08 00:11:14.452 10179-10184/com.yourgesture.testloading I/zygote: After code cache collection, code=94KB, data=76KB
2019-06-08 00:11:14.503 10179-10214/com.yourgesture.testloading D/EGL_emulation: eglMakeCurrent: 0xe95843c0: ver 3 0 (tinfo 0xe9583450)
2019-06-08 00:11:16.425 10179-10214/com.yourgesture.testloading D/EGL_emulation: eglMakeCurrent: 0xe95843c0: ver 3 0 (tinfo 0xe9583450)
...