对于我的论文,我做了一个Android应用程序,它或多或少差不多完成但我的问题是我开始使用活动,现在我真的应该换成片段,我想改变为尽可能少。由于我的国家/地区的这种应用程序的版权,我只能知道它的外观。 我有顶部标题与用户信息等基于活动,它应该保持原样,然后我有按钮面板构建作为活动,然后有我需要重写为片段的内容。 app的基本结构是在BaseActivity类上构建的,所有其他类都扩展了BaseActivity:
public class BaseActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// some code here, db connectors etc
}
// Some more functions like sending session context to server, retriving it
void setNavigationDrawer() {
setUserInfo(sessionContext);
DrawerLayout sDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
sDrawerLayout = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
sDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, DisplayedUserInfo));
// Set the list's click listener
sDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
sDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
// Set the drawer toggle as the DrawerListener
sDrawerLayout.setDrawerListener(sDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
protected void setUpNavigation() {
// setting nav buttons i.e
if(userInfo != null) {
userInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), userInfoActivity.class);
intent.putExtra("SessionContext", sessionContext);
startActivityForResult(intent, GET_CURRENT_USER);
}
});
}
}
}
和我需要更改的示例活动类。
public class UserInfoActivity extends BaseActivity {
// var declarations
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_info_layout);
setNavigationDrawer();
setUpNavigation();
tableLayout = (TableLayout) findViewById(R.id.user_table);
loadData();
Button btnUsrNav = (Button) findViewById(R.id.go_to_user);
btnUsrNav.setBackgroundColor(getResources().getColor(R.color.nav));
btnUsrkNav.setEnabled(false);
}
// some functions as creating tables etc
}
我的基本想法是创建扩展Fragment的BaseFragment类,在这里移动所有来自BaseActivity的函数,如setUpNavigation()setNavigationDrawer()(我是否需要更改其中的任何内容)然后只需更改所有扩展BaseActivity以扩展BaseFragment但我不真的知道如何在BaseActivity中设置所有片段。但它唯一的想法,我不确定它是否会成功。