我的主要活动中有一个导航抽屉。现在我需要调用onNavigationDrawerItemSelected
方法中的所有片段。但奇怪的是,我没有碎片。我还有活动。所以我用Fragment Activity
扩展了它。
MainActivity
是导航抽屉活动。
FeedListActivity
是Fragment活动。
现在我需要在我的主要活动中添加Feedlistactivity
作为片段。我怎样才能做到这一点?
我看到很多与此相关的问题。但我无法与我联系。
主要活动
public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks{
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public void onNavigationDrawerItemSelected(int position) {
if (position == 1){
Fragment newFragment = new Fragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, newFragment);
transaction.commit();
}
}
}
FeedListActivity
public class FeedListActivity extends FragmentActivity{
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
private static final String TAG = "FeedListActivity";
private ListView NewsView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
ProgressBarCircularIndetermininate progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed_list);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
progressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
NewsView = (ListView) findViewById(R.id.feed_list);
NewsView.setAdapter(listAdapter);
// making fresh volley request and getting json
// Adding request to volley request queue
AppController.getInstance().addRequest(gsonRequest, TAG);
getimg();
}
}
答案 0 :(得分:1)
FeedListActivity是Fragment活动。 现在我需要在我的Main中添加Feedlistactivity作为片段 活动。我怎样才能做到这一点?
fragmentActivity不是片段。它是一个支持片段的活动,它被引入以使较旧的Android构建向后兼容片段 - 如果您只是针对较新的API,则只能忽略它。要使FeedListActivity()成为片段......您必须从片段扩展。
Fragment newFragment = new Fragment();
此处的这一行应该是在您将其作为片段后创建FeedList的新实例。