Java 片段的onCreateView方法是什么样子的: public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return null; }
Java 片段的onCreateView方法是什么样子的: public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return null; },java,android,xml,android-layout,android-fragments,Java,Android,Xml,Android Layout,Android Fragments,TitlesFragment扩展了ListFragment,默认情况下,该布局包含ListView项的布局,以及TextView列表为空时标签的布局和ProgressBar。在这种情况下,用户不必返回自己的视图,可以返回通过超级调用获得的对象列表片段的onCreateView: public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TitlesFragment
扩展了ListFragment
,默认情况下,该布局包含ListView
项的布局,以及TextView
列表为空时标签的布局和ProgressBar
。在这种情况下,用户不必返回自己的视图,可以返回通过超级调用获得的对象<代码>列表片段的onCreateView:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(com.android.internal.R.layout.list_content,
container, false);
}
. 查看源代码Raghunandan,你知道android如何知道它必须将标题片段膨胀到main.xml中吗?在Quote片段的代码中,我们有mDetailsFragment=(QuotesFragment)getFragmentManager()。标题片段没有类似的代码。我猜main activity.xml是通过class=“course.examples.Fragments.StaticLayout.TitlesFragment”来引用它的,因此它会膨胀,但我不确定…是的,请问一个新问题,我会回答谢谢你的解释,这很有意义@Simon事实上,您可以删除ListFragments中的
onCreateView
,谢谢您的解释,这很有意义@Simon事实上,您可以在ListFragment
中去掉onCreateView
,它无论如何都不会影响代码HMMM。。。你说得对,拉古南丹!我刚刚在AVD上测试了它,它对应用程序没有影响。我想知道为什么课程协调员会把它写进代码里。。。也许是为了向我们展示fragment类的生命周期,同时也让新手们感到困惑。
package course.examples.Fragments.StaticLayout;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TitlesFragment extends ListFragment {
private ListSelectionListener mListener = null;
private static final String TAG = "TitlesFragment";
public interface ListSelectionListener {
public void onListSelection(int index);
}
@Override
public void onListItemClick(ListView l, View v, int pos, long id) {
getListView().setItemChecked(pos, true);
mListener.onListSelection(pos);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (ListSelectionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnArticleSelectedListener");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, getClass().getSimpleName() + ":entered onCreate()");
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, getClass().getSimpleName() + ":entered onCreate()");
**return super.onCreateView(inflater, container, savedInstanceState);**
}
@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.title_item, QuoteViewerActivity.mTitleArray));
}
<?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:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="@+id/titles"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
class="course.examples.Fragments.StaticLayout.TitlesFragment" />
<fragment
android:id="@+id/details"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="2"
class="course.examples.Fragments.StaticLayout.QuotesFragment" />
</LinearLayout>
<?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" >
<TextView
android:id="@+id/quoteView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dip"
android:textSize="32sp" >
</TextView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical"
android:padding="5dip"
android:textSize="32sp" >
</TextView>
android:id="@android:id/list"
return super.onCreateView(inflater, container, savedInstanceState);
189
190 @Override
191 public View onCreateView(LayoutInflater inflater, ViewGroup container,
192 Bundle savedInstanceState) {
193 return inflater.inflate(com.android.internal.R.layout.list_content,
194 container, false);
195 }
public class QuotesFragment extends Fragment {
return inflater.inflate(R.layout.quote_fragment, container, false);
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return null;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(com.android.internal.R.layout.list_content,
container, false);
}