我正在开发一种安卓应用程序,其中我需要在 Fradition Affority
内有基于同一等级的多个碎片。 目前,我的代码主要基于官方文档中的http:// developmenter.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/ap/fragmentTabsPager.html' rel=“nofollown noreferrrr” > 。 这个示例是 < a >, 除了我对每个碎片都立即将相同的类别划为:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tabs_pager);
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
mViewPager = (ViewPager)findViewById(R.id.pager);
mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
mTabsAdapter.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1"), MyFragment.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2"), MyFragment.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab 3"), MyFragment.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("tab4").setIndicator("Tab 4"), MyFragment.class, null);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
}
类 MyFrashment
很简单 :
public class MyFragment extends Fragment {
View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
v = inflater.inflate(R.layout.test, container, false);
return v;
}
}
我简化了分层类所膨胀的布局, 改成简单的文字视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/test_textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/some_default_value"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
So, the thing is, I would like to ba able to modify the value of test_textView1
separately in each fragment, after they have been instantiated. My problem is that I don t manage to access to the elements of the layout of a fragment. I cannot directly use the findViewById()
method since it would affect all the fragments.
Inside the TabsAdapter
class, I tried to access a single fragment, and then access its View by doing the following:
getItem(number).getView();
但它返回了 null
( getThomatter ()
返回了正确的碎片, 但 getView ()
返回了空号 ) 。
我还读了Frashment
中宣布文本View,则添加一种方法 setTextView(Strings)
,并从 Fradition Affactivity
中称之为它,它产生一个NullpokerException。
那么,有没有具体的方法可以 单独访问我碎片中的布局项目?谢谢!