0
我有一個用於膨脹各種片段的父佈局。在一個這樣的片段佈局中,我需要訪問片段類中的視圖元素。 我使用下面的代碼來獲取片段佈局中文本視圖的引用。如何在片段在另一個佈局內膨脹時獲取片段的視圖引用?
CreateTaskFragment.java
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = getView();
endDateView = (TextView)view.findViewById(R.id.estimated_end_input);
endDateView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Date","I'm clicked");
}
});
}
的endDateView將返回空。
fragment_create_task.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.tetrissoft.dailyplanner.CreateTaskFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/estimated_end_date"
android:textSize="15sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:id="@+id/estimated_end_label"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/task_end_date_input"
android:textSize="18sp"
android:inputType="date"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_below="@+id/estimated_end_input"/>
</LinearLayout>
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/drawer_layout">
<!-- The Main Content Layout -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="25dp"
app:elevation="4dp"
android:clickable="true"
android:src="@drawable/ic_create_black_24dp"
android:id="@+id/fab_button"/>
</FrameLayout>
<!-- The Navigation Drawer-->
<ListView
...
</ListView>
</android.support.v4.widget.DrawerLayout>
我怎樣才能得到參考CreateTaskFragment的視圖元素?以下方法
以下方法拷貝到您的片段和片段 – Drv