【发布时间】:2015-04-27 03:12:38
【问题描述】:
首先;我想为这个冗长的问题道歉。我虽然这是了解我正在尝试做的事情的唯一方法。反正。我有一个应用程序,它使用一个导航抽屉,它将为用户提供 3 个选择。我希望 Test1Fragment 成为一个列表视图。我似乎无法弄清楚如何做到这一点。请帮忙。
根据选择控制加载哪个片段的MainActivity方法:
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Test1Fragment();
break;
case 1:
fragment = new Test2Fragment();
break;
case 2:
fragment = new Test3Fragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
Test1Fragment:
public class Test1Fragment extends Fragment {
public Test1Fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_create, container, false);
return rootView;
}
fragment_create.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--<TextView-->
<!--android:id="@+id/txtLabel"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_centerInParent="true"-->
<!--android:text="Create View"-->
<!--android:textSize="16dp" />-->
<!--<ImageView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_below="@id/txtLabel"-->
<!--android:layout_centerHorizontal="true"-->
<!--android:layout_marginTop="10dp"-->
<!--android:src="@drawable/ic_action_copy" />-->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
我知道我缺少列表视图的代码,但这就是我想要的 xml 视图。
谢谢
【问题讨论】:
-
发布您的
R.layout.fragment_create
标签: android listview fragment navigation-drawer