我有疯狂性,它扩展了FragmentActivity(带有片段的2个标签)。我需要在片段中放入以下listview acitivity。或者请告诉我如何使用这些资源制作列表片段。
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private ListView List;
// Navigation drawer title
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private List<Category> albumsList;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List = (ListView) findViewById(R.id.list_album);
navDrawerItems = new ArrayList<NavDrawerItem>();
// Getting the albums from shared preferences
albumsList = AppController.getInstance().getPrefManger().getCategories();
// Insert "Recently Added" in navigation drawer first position
Category recentAlbum = new Category(null,
getString(R.string.nav_drawer_recently_added));
albumsList.add(0, recentAlbum);
// Loop through albums in add them to navigation drawer adapter
for (Category a : albumsList) {
navDrawerItems.add(new NavDrawerItem(a.getId(), a.getTitle()));
}
// Setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
List.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* On menu item selected
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
// Selected settings menu item
// launch Settings activity
Intent intent = new Intent(MainActivity.this,
SettingsActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
答案 0 :(得分:1)
Fragment表示Activity中的行为或用户界面的一部分。您可以在单个活动中组合多个片段以构建多窗格UI,并在多个活动中重用片段。您可以将片段视为活动的模块化部分,它具有自己的生命周期,接收自己的输入事件,并且可以在活动运行时添加或删除。
片段由活动拥有,因此您无法将活动放入片段中。但你可以在片段中做类似的事情。例如:
public class MyListFragment extends Fragment {
private ListView List;
// Navigation drawer title
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private List<Category> albumsList;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate your layout here
View v = inflater.inflate(R.layout.activity_main, container, false);
// find your listview
List = (ListView) findViewById(v, R.id.list_album);
navDrawerItems = new ArrayList<NavDrawerItem>();
// just like what you do in your activity...
return v;
}
}