Android 从FragmentActivity访问片段控件
我有一个Android 从FragmentActivity访问片段控件,android,android-fragments,android-fragmentactivity,Android,Android Fragments,Android Fragmentactivity,我有一个FragmentActivity,它应该可以容纳2个片段。每个片段都有自己的布局,其中包含一个表。我需要从FragmentActivity中访问片段布局中的表,以放置从XML文件读取的一些数据。我的问题是当我使用 tableOrders=(TableLayout)findviewbyd(R.id.tabel1) 它返回null。现在,我知道版面中的项目只有在膨胀之前才能被访问,在尝试调用findViewById()之前,我已经在FragmentActivity中这样做了。以下是我尝试的代
FragmentActivity
,它应该可以容纳2个片段。每个片段都有自己的布局,其中包含一个表。我需要从FragmentActivity
中访问片段布局中的表,以放置从XML文件读取的一些数据。我的问题是当我使用
tableOrders=(TableLayout)findviewbyd(R.id.tabel1)代码>
它返回null
。现在,我知道版面中的项目只有在膨胀之前才能被访问,在尝试调用findViewById()
之前,我已经在FragmentActivity中这样做了。以下是我尝试的代码:
从FragmentActivity
派生的FragmentHolder.java
的一次创建:
private TableLayout tableOrders = null;
private TableLayout tableExecutions = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_holder);
initialisePaging();
//View orderFragment = (View)findViewById(R.layout.fragment_orders);
tableOrders = (TableLayout)/*orderFragment.*/findViewById(R.id.tabel1);
//View execFragment = (View)findViewById(R.layout.executions_fragment);
tableExecutions = (TableLayout)/*execFragment.*/findViewById(R.id.tabel2);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if(bundle != null)
logedUser = (User) bundle.getSerializable("user");
AssetManager am = getApplicationContext().getAssets();
String xmlContentOrders = XMLfunctions.getXml(am, "ordersData.xml");
populateOrdersTable(xmlContentOrders);
String xmlContentExecutions = XMLfunctions.getXml(am, "executionsData.xml");
populateExecutionsTable(xmlContentExecutions);
}
片段类(不知道是否有帮助):
片段类(不知道是否有帮助):
以及片段的.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/tabel1header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/tablebgcolor" >
<TableRow >
<TextView
android:id="@+id/ordersHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:text="@string/orders"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="@+id/ordersTable"
style="@style/HeaderRow" >
<TextView
android:id="@+id/textSymbol"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/symbol"
style="@style/HeaderText" />
<TextView
android:id="@+id/textSide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/side"
style="@style/HeaderText" />
<TextView
android:id="@+id/textPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/price"
style="@style/HeaderText" />
<TextView
android:id="@+id/textQuantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/quantity"
style="@style/HeaderText"/>
<TextView
android:id="@+id/textRemaining"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/remanent"
style="@style/HeaderText" />
</TableRow>
</TableLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TableLayout
android:id="@+id/tabel1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/tablebgcolor">
</TableLayout>
</ScrollView>
</LinearLayout>
您如何将片段
放入您的活动
?您是在呼叫FragmentManager还是将其包含在XML中?您总是可以按代码检索片段,并以这种方式访问其内部成员。我正在使用FragmentPagerAdapter
派生类将片段
s放入活动
。我将用该函数的代码更新帖子。如何通过代码访问内部成员的片段?
public class OrdersFragment extends Fragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_orders, container, false);
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/tabel1header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/tablebgcolor" >
<TableRow >
<TextView
android:id="@+id/ordersHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:text="@string/orders"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="@+id/ordersTable"
style="@style/HeaderRow" >
<TextView
android:id="@+id/textSymbol"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/symbol"
style="@style/HeaderText" />
<TextView
android:id="@+id/textSide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/side"
style="@style/HeaderText" />
<TextView
android:id="@+id/textPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/price"
style="@style/HeaderText" />
<TextView
android:id="@+id/textQuantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/quantity"
style="@style/HeaderText"/>
<TextView
android:id="@+id/textRemaining"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/remanent"
style="@style/HeaderText" />
</TableRow>
</TableLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TableLayout
android:id="@+id/tabel1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/tablebgcolor">
</TableLayout>
</ScrollView>
</LinearLayout>
private void initialisePaging() {
OrdersFragment fragmentOne = new OrdersFragment();
ExecutionsFragment fragmentTwo= new ExecutionsFragment();
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(fragmentOne);
pagerAdapter.addFragment(fragmentTwo);
ViewPager viewPager = (ViewPager) super.findViewById(R.id.viewPager);
viewPager.setAdapter(pagerAdapter);
viewPager.setOffscreenPageLimit(2);
viewPager.setCurrentItem(0);
}