Fragment加载其内容但代码不会更新它

时间:2017-02-02 18:20:05

标签: android android-fragments

我目前正在将常规Activity中的应用程序修改为带有Fragments的TabbedActivity,到目前为止,我已经设法在创建活动后将片段的适当布局加载到应用程序,但是我在onCreateView方法上的代码执行,因为我能够调试其中的每一行,但它对片段显示的布局没有影响,此代码负责诸如填充Spinners之类的内容。

我知道我在某处做错了什么,我只是无法弄清楚它是什么以及它发生在哪里。

public class myClass extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private TabLayout mTabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_myclass);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mTabLayout = (TabLayout) findViewById(R.id.tabs);
    mTabLayout.setupWithViewPager(mViewPager);
    myClass_Fragment ids = new myClass_Fragment ();
    FragmentManager fm = getFragmentManager();
    fm.beginTransaction().replace(R.id.main_content, ids, "fragment one").commit();
}

public static class PlaceholderFragment extends Fragment {
    public PlaceholderFragment() {
    }

public static PlaceholderFragment newInstance() {
        PlaceholderFragment fragment = new PlaceholderFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.ids, container, false);
        return rootView;
    }
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "IDS";
            case 1:
                return "Other";
        }
        return null;
    }
}
}

片段:

public class myClass_Fragment extends Fragment {
public myClass_Fragment () {

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.ids, container, false);
// code that updates views such as spinners
// everything in here works, I can see the content getting retrieved by debugging individual lines
    return  rootView;
}
}

所有视图都在片段的布局中声明,一旦应用加载了所有内容,但没有在片段的onCreateView方法上检索到的值。

我认为这几乎是关于这个问题的重要事项,我想知道我做错了什么,我该怎么做才能解决它。

根据要求,activity_myclass.xml:

<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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".myClass">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="fixed"
    app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

作为旁注,在开始更改此类布局之前,应用程序上的所有内容都运行正常,因此问题必须出在这个新实现上。

1 个答案:

答案 0 :(得分:0)

我不知道这是否符合您的要求,但您可以尝试这种方式:

<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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".myClass">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    ...

尝试替换上面FrameLayout中的片段,你能看到片段里面的视图吗?