ListFragment onCreateView 未调用
ListFragment onCreateView not called
我正在尝试使用 FragmentManager 替换片段,但由于某些原因 OnCreateView()
未被调用。
这是我用来创建片段的代码:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FeedbackListFragment listFragment = new FeedbackListFragment();
fragmentTransaction.replace(R.id.feedback_placeholder, listFragment);
fragmentTransaction.commit();
这是 FeedbackListFragment 的代码:
public class FeedbackListFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG,"create feedbacklistview");
View view = inflater.inflate(R.layout.feedback_popup, container, false);
return view;
}
}
这是我的主要布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_normal"
android:id="@+id/main_layout">
<FrameLayout
android:id="@+id/feedback_placeholder"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="match_parent"></FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="90dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/overview_scan_text"
android:id="@+id/scantagLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="42sp"
android:textIsSelectable="false"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:id="@+id/cleanjack_image"
android:src="@drawable/logo_clean_jack"
android:layout_gravity="left|center_vertical"
android:contentDescription="@string/app_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/messageLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textIsSelectable="false"/>
</LinearLayout>
我已经删除了 android:visibility="gone"
以查看是否是问题所在,但它没有用。
来自 ListFragment 中 onCreateView 的文档:
Provide default implementation to return a simple list view. Subclasses
can override to replace with their own layout. If doing so, the
returned view hierarchy must have a ListView whose id
is {@link android.R.id#list android.R.id.list} and can optionally
have a sibling view id {@link android.R.id#empty android.R.id.empty}
that is to be shown when the list is empty.
If you are overriding this method with your own custom content,
consider including the standard layout {@link android.R.layout#list_content}
in your layout file, so that you continue to retain all of the standard
behavior of ListFragment. In particular, this is currently the only
way to have the built-in indeterminant progress state be shown.
我试过你的示例代码
Activity :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FeedbackListFragment listFragment = new FeedbackListFragment();
fragmentTransaction.replace(R.id.feedback_placeholder, listFragment);
fragmentTransaction.commit();
}
片段列表:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.feedback_popup, container, false);
return view;
}
XML Activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bcbcbc"
android:id="@+id/main_layout">
<FrameLayout
android:id="@+id/feedback_placeholder"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="match_parent"></FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="90dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="overview_scan_text"
android:id="@+id/scantagLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="42sp"
android:textIsSelectable="false"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:id="@+id/cleanjack_image"
android:src="@drawable/ic_launcher_background"
android:layout_gravity="left|center_vertical"
android:contentDescription="@string/app_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/messageLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textIsSelectable="false"/>
</LinearLayout>
Xml 片段
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</android.support.constraint.ConstraintLayout>
你是否设置了fragmentList的listView的id为
android:id="@android:id/list"
希望这对您有所帮助 :)
我正在尝试使用 FragmentManager 替换片段,但由于某些原因 OnCreateView()
未被调用。
这是我用来创建片段的代码:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FeedbackListFragment listFragment = new FeedbackListFragment();
fragmentTransaction.replace(R.id.feedback_placeholder, listFragment);
fragmentTransaction.commit();
这是 FeedbackListFragment 的代码:
public class FeedbackListFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG,"create feedbacklistview");
View view = inflater.inflate(R.layout.feedback_popup, container, false);
return view;
}
}
这是我的主要布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_normal"
android:id="@+id/main_layout">
<FrameLayout
android:id="@+id/feedback_placeholder"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="match_parent"></FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="90dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/overview_scan_text"
android:id="@+id/scantagLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="42sp"
android:textIsSelectable="false"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:id="@+id/cleanjack_image"
android:src="@drawable/logo_clean_jack"
android:layout_gravity="left|center_vertical"
android:contentDescription="@string/app_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/messageLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textIsSelectable="false"/>
</LinearLayout>
我已经删除了 android:visibility="gone"
以查看是否是问题所在,但它没有用。
来自 ListFragment 中 onCreateView 的文档:
Provide default implementation to return a simple list view. Subclasses can override to replace with their own layout. If doing so, the returned view hierarchy must have a ListView whose id is {@link android.R.id#list android.R.id.list} and can optionally have a sibling view id {@link android.R.id#empty android.R.id.empty} that is to be shown when the list is empty.
If you are overriding this method with your own custom content, consider including the standard layout {@link android.R.layout#list_content} in your layout file, so that you continue to retain all of the standard behavior of ListFragment. In particular, this is currently the only way to have the built-in indeterminant progress state be shown.
我试过你的示例代码
Activity :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FeedbackListFragment listFragment = new FeedbackListFragment();
fragmentTransaction.replace(R.id.feedback_placeholder, listFragment);
fragmentTransaction.commit();
}
片段列表:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.feedback_popup, container, false);
return view;
}
XML Activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bcbcbc"
android:id="@+id/main_layout">
<FrameLayout
android:id="@+id/feedback_placeholder"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="match_parent"></FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="90dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="overview_scan_text"
android:id="@+id/scantagLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="42sp"
android:textIsSelectable="false"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:id="@+id/cleanjack_image"
android:src="@drawable/ic_launcher_background"
android:layout_gravity="left|center_vertical"
android:contentDescription="@string/app_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/messageLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textIsSelectable="false"/>
</LinearLayout>
Xml 片段
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</android.support.constraint.ConstraintLayout>
你是否设置了fragmentList的listView的id为
android:id="@android:id/list"
希望这对您有所帮助 :)