我开发了一个包含5个片段的基本应用程序。每个片段显示不同的标题,只有第一个片段也显示subTitle。
这是代码:
switch (position) {
case 0:
setTitle("fragment_0",set);
if(searchFragment == null) searchFragment = new SearchFragment();
currentFragment = searchFragment;
mapButton.setVisibility(View.VISIBLE);
btTopQuery.setVisibility(View.INVISIBLE);
break;
case 1:
setTitle("fragment_1",null);
if(queryFragment == null) queryFragment = new QueryFragment();
((QueryFragment)queryFragment).viewStatus = 0;
currentFragment = queryFragment;
btTopQuery.setVisibility(View.VISIBLE);
break;
case 2:
setTitle("fragment_2",null);
showDownLoadDialog();
return;
case 3:
setTitle("fragment_3",null);
if(newsFragment == null) newsFragment = new NewsFragment();
currentFragment = newsFragment;
CustomSharedPreferences.init(getApplicationContext());
CustomSharedPreferences.setPreferences(Constants.READ_NEWS_COUNT,FirstProjectApplication.allNews.size());
menu_adapter.notifyDataSetChanged();
break;
case 4:
setTitle("fragment_4",null);
if(infoFragment == null) infoFragment = new InfoFragment();
currentFragment = infoFragment;
break;
case 5:
setTitle("fragment_5",null);
if(socialFragment == null) socialFragment = new SocialFragment();
currentFragment = socialFragment;
break;
case 6:
setTitle("fragment_6",null);
if(settingsFragment == null) settingsFragment = new SettingsFragment();
currentFragment = settingsFragment;
break;
}
getSupportFragmentManager().beginTransaction()
.replace(R.id.frameContainer, currentFragment)
.commit();
//.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
mDrawerLayout.closeDrawer(navdrawer);
}
});
和:
if(currentFragment == null) {
setTitle("fragment_0",set);
if(searchFragment == null) searchFragment = new SearchFragment();
currentFragment = searchFragment;
mapButton.setVisibility(View.VISIBLE);
btTopQuery.setVisibility(View.INVISIBLE);
getSupportFragmentManager().beginTransaction().add(R.id.frameContainer, currentFragment).commit();
}
变量set
具有我从JSON读取的值,使用以下代码:
String set;
//Reading text
String url1 ="url_json";
StringRequest strReq1 = new StringRequest(Request.Method.GET,
url1, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("", response.toString());
try {
FirstProjectApplication.allNews.clear();
JSONObject featureCollection=new JSONObject(response);
String news=featureCollection.getString("TAG");
set = news;
Log.d("test_json", news);
Log.d("test", "news count:"+news.length());
for(int i=0;i<news.length();i++) {
NewsResponse newsResponse = new NewsResponse();
FirstProjectApplication.allNews.add(newsResponse);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(menu_adapter != null) {
menu_adapter.notifyDataSetChanged();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("", "Error: " + error.getMessage());
}
});
FirstProjectApplication.getInstance().addToRequestQueue(strReq1, "str_req");
setTitle
方法:
public void setTitle(String title,String subTitle) {
getActionBar().setTitle(title);
getActionBar().setSubtitle(subTitle);
}
现在在case 0
进入setTitle
,我设置了set
变量并且显示正确,而当插入currentFragment的setTitle时,变量set
未显示,但如果我用硬编码的字符串替换它,它就会显示出来。