Android 片段加载其内容,但代码不加载';我不能更新它
我目前正在将一个应用程序从常规活动修改为带有片段的TabbedActivity,到目前为止,我已经成功地在创建活动后将片段的适当布局加载到应用程序中,但是我在onCreateView方法上的代码会执行,因为我能够调试其中的每一行,但是它对片段所显示的布局没有影响,这段代码负责诸如填充微调器之类的事情 我知道我在某个地方做错了什么,我只是不太清楚它是什么,发生在哪里Android 片段加载其内容,但代码不加载';我不能更新它,android,android-fragments,Android,Android Fragments,我目前正在将一个应用程序从常规活动修改为带有片段的TabbedActivity,到目前为止,我已经成功地在创建活动后将片段的适当布局加载到应用程序中,但是我在onCreateView方法上的代码会执行,因为我能够调试其中的每一行,但是它对片段所显示的布局没有影响,这段代码负责诸如填充微调器之类的事情 我知道我在某个地方做错了什么,我只是不太清楚它是什么,发生在哪里 public class myClass extends AppCompatActivity { private SectionsP
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方法上检索到的值
我想这几乎就是这个问题的全部,我想知道我做错了什么,我能做些什么来解决它
根据要求,活动_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>
作为补充说明,在开始更改这种布局之前,应用程序上的一切都很正常,因此问题在于这种新的实现。我不知道这是否符合您的要求,但您可以通过以下方式尝试:
<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中的片段,您能看到片段中的视图吗?占位符片段中的newInstance()方法在哪里?抱歉,我已经更新了这个问题。你可以发布activity_myclass.xml布局文件吗?现在应该已经存在了。我添加了你建议的框架布局,并将其设置为fm.beginTransaction()。替换(R.id.fragment_container,id,“fragment one”).commit();但是,在对onCreateView方法进行所有更改之前,我仍然得到相同的基本布局。在上面发布的代码中,您将相同的布局放大了两倍:R.layout.ids,您试图更改的片段的布局。