我的应用程序中有 2 个片段,Fragment1
并且Fragment2
. 两者都使用不同的布局,称为content_view1
and content_view2
。我将两个片段都放入了ViewPager
. 现在看代码:
MainActivity.java
public class MainActivity extends FragmentActivity {
public ViewPager vp;
FragmentAdapter fragmentAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
vp = new ViewPager(this);
vp.setId("VP".hashCode());
fragmentAdapter = new FragmentAdapter(
getSupportFragmentManager());
vp.setAdapter(fragmentAdapter);
setContentView(vp);
vp.setCurrentItem(0);
}
}
Fragment1.java
public class Fragment1 extends Fragment {
MainActivity mainActivity;
public Fragment1() {
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainActivity=(MainActivity)getActivity();
return inflater.inflate(R.layout.content_view1, null);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((TextView)mainActivity.findViewById(R.id.haiTextView)).setText("Hello");
}
}
Fragment2.java
public class Fragment2 extends Fragment {
MainActivity mainActivity;
public Fragment2() {
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainActivity=(MainActivity)getActivity();
return inflater.inflate(R.layout.content_view2, null);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((TextView)mainActivity.findViewById(R.id.haiTextView)).setText("Hai");
}
}
FragmentAdapter.java
public class FragmentAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> mFragments;
public FragmentAdapter(FragmentManager fm) {
super(fm);
mFragments = new ArrayList<Fragment>();
mFragments.add(new Fragment1());
mFragments.add(new Fragment2());
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
}
content_view1.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|center_horizontal" >
<TextView
android:id="@+id/haiTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
</FrameLayout>
content_view2.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|center_horizontal" >
<TextView
android:id="@+id/haiTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
</FrameLayout>
现在您看到两种布局都有一个 textView 具有相同id
的 .
当我运行这个应用程序时,fragment1TextView
有文本“hai”,fragment2TextView
有文本“”。但我期待的是,fragment1TextView
应该有文本“Hello”,fragment2TextView
应该有文本“hai”。
好的,现在我将TextView
id更改content_view2.xml
为haiTextView2
。也做了同样的 Fragment2.java
事情。现在该应用程序按我的预期完美运行。
现在我的问题是:
1)为什么当我保持两个TextView
id 相同时我没有得到预期的输出?
2) 为什么我不能在这里使用相同的 ID?