我想在片段中创建一个列表(比如左侧导航)。为此,我使用了以下代码,但无法在片段中填充列表。
主要.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment android:id="@+id/categoryList"
android:layout_height="fill_parent"
android:name="com.example.fragments.CategoryListFragment"
android:layout_width="wrap_content" />
</LinearLayout>
主要活动:
public class MainActivity extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
CategoryListFragment 类:
public class CategoryListFragment extends ListFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "AAAA", "BBBB", "CCCC" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
}