尝试膨胀布局时,新片段完全为空

时间:2019-06-07 22:19:51

标签: java android android-fragments fragment layout-inflater

我在RecyclerViewAdapter类中有一个函数,该函数用(从RecyclerView中)单击的项的新片段替换当前视图。即使我在OnCreateView中被放大的布局中有textView和imageView,加载的我的BlogFragment类也显示为空。我不明白为什么它似乎为空,并且在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;
  }
}

我的fragment_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>

一旦进入我的launchBlogFragment函数,我的LogCat。 (fragment_blog.xml中的imageView或textView均未显示)

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)

0 个答案:

没有答案