我目前正在开发Android应用。它有一个导航抽屉和一些碎片。我希望动作栏标题根据片段而改变。我设法做到了这一点。但问题是,当我按下后退按钮时,执行setTitle()方法并更改标题,但它没有明显反映在操作栏上。
编辑:我发现问题在于导航抽屉的打开和关闭。所以我从ondraweropened()和ondrawerclosed()中删除了更改的标题部分。它解决了当前的问题。但我希望名称在打开时更改,并在结束时保留当前片段名称。谁能帮助我呢?这是我的主要活动:
public class HomeActivity extends ActionBarActivity {
public static final String EXTRA_MESSAGE = "message";
public static final String PROPERTY_REG_ID = "registration_id";
private static final String PROPERTY_APP_VERSION = "appVersion";
static final String DISPLAY_MESSAGE_ACTION="com.example.test.DISPLAY_MESSAGE";;
static final String SERVER_URL = "http://doylefermi.x20.in/register.php";
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
public static String acc = "";
public String msg = "";
public static String accn = "";
String SENDER_ID = "1019787135827";
static final String TAG = "GCMDemo";
TextView mDisplay;
GoogleCloudMessaging gcm;
AtomicInteger msgId = new AtomicInteger();
SharedPreferences prefs;
Context context;
String regid="";
String email="";
String title="";
String name="";
private DrawerLayout mDrawerLayout;
private android.support.v4.app.ActionBarDrawerToggle mDrawerToggle;
private String[] navMenuTitles;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader= new ArrayList<String>();
HashMap<String, List<String>> listDataChild=new HashMap<String,List<String>>();
private LinearLayout mDrawerLinear;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setupActionBar();
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction;
transaction=fragmentManager.beginTransaction();
Fragment f=new DestinationsFragment();
transaction.replace(R.id.frame_layout, f).commit();
mDrawerLinear= (LinearLayout) findViewById(R.id.left_drawer);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
mDrawerLayout = (DrawerLayout) findViewById(R.id.nav_bar);
mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);
expListView = (ExpandableListView) findViewById(R.id.tourist_list);
gcmcheck();
listDataHeader.add(navMenuTitles[0]);
listDataHeader.add(navMenuTitles[1]);
listDataHeader.add(navMenuTitles[2]);
listDataHeader.add(navMenuTitles[3]);
listDataHeader.add(navMenuTitles[4]);
List<String> dest=new ArrayList<String>();
List<String> attr=new ArrayList<String>();
List<String> spl=new ArrayList<String>();
List<String> fest=new ArrayList<String>();
List<String> abtus=new ArrayList<String>();
attr.add("Water World");
attr.add("Temples");
attr.add("Arts & Crafts");
listDataChild.put(listDataHeader.get(0), dest); // Header, Child data
listDataChild.put(listDataHeader.get(1), attr);
listDataChild.put(listDataHeader.get(2), spl);
listDataChild.put(listDataHeader.get(3), fest); // Header, Child data
listDataChild.put(listDataHeader.get(4), abtus);
mDrawerLayout = (DrawerLayout) findViewById(R.id.nav_bar);
mDrawerToggle = new android.support.v4.app.ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
super.onDrawerClosed(view);
getSupportActionBar().setTitle(title);
}
public void onDrawerOpened(View drawerView) {
title=getTitle().toString();
invalidateOptionsMenu();
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(R.string.action_bar_title1);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
listAdapter=new ExpandableListAdapter(this,listDataHeader,listDataChild);
expListView.setAdapter(listAdapter);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener(){
@Override
public boolean onGroupClick(ExpandableListView parent,View v,int groupPosition,long id){
if(groupPosition!=1)
{
displayView(groupPosition,groupPosition);
return false;
}
else
return false;
}
});
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
displayView(groupPosition,childPosition);
return false;
}
});
}
private void displayView(int group,int position ) {
Fragment fragment = null;
switch (group) {
case 0: fragment=new DestinationsFragment();title="Destinations";break;
case 1: switch(position){
case 0:fragment=new WaterWorldFragment();title="Water World";break;
case 1:fragment=new TemplesFragment();title="Temples";break;
case 2:fragment=new ArtsCraftsFragment();title="Arts & Crafts";break;
}
break;
case 2: fragment=new SpecialInterestFragment();title="Special Interest";
break;
case 3: fragment=new FestivalsFragment();title="Festivals";
break;
case 4: fragment=new AboutUsFragment();title="About Us";
break;
default: break;
}
if (fragment != null) {
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction;
transaction=fragmentManager.beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.frame_layout, fragment).commit();
expListView.setItemChecked(position, true);
expListView.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerLinear);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
@Override
public void onBackPressed(){
if(mDrawerLayout.isDrawerOpen(Gravity.LEFT))
{
mDrawerLayout.closeDrawer(Gravity.LEFT);
}
else{
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater m=getMenuInflater();
m.inflate(R.menu.menu_home,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu (Menu menu){
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerLinear);
return super.onPrepareOptionsMenu(menu);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
public void setupActionBar(){
//getSupportActionBar().setTitle(R.string.app_name);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xffff5253));
}
private void gcmcheck()
{
Account[] accounts = AccountManager.get(this).getAccounts();
acc= accounts[1].name;
accn=acc.substring(0, acc.indexOf('@'));
name=accn;
email=acc;
context = getApplicationContext();
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(context);
if (regid=="") { //Toast.makeText(getApplicationContext(), "Registering device...", Toast.LENGTH_SHORT).show();
registerInBackground();
}
//else {Toast.makeText(getApplicationContext(), "Device registered, registration ID=" + regid,Toast.LENGTH_LONG).show(); }
}
private String getRegistrationId(Context context) {
final SharedPreferences prefs = getGCMPreferences(context);
String registrationId = prefs.getString(PROPERTY_REG_ID, "");
if (registrationId.isEmpty()) {
Log.i(TAG, "Registration not found.");
return "";
}
// Check if app was updated; setContentView(R.layout.activity_main);if so, it must clear the registration ID
// since the existing regID is not guaranteed to work with the new
// app version.
return registrationId;
}
private SharedPreferences getGCMPreferences(Context context) {
// This sample app persists the registration ID in shared preferences, but
// how you store the regID in your app is up to you.
return getSharedPreferences(HomeActivity.class.getSimpleName(),
Context.MODE_PRIVATE);
}
private void registerInBackground() {
new AsyncTask<Void,Void,String>() {
@Override
protected String doInBackground(Void... params) {
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;
// You should send the registration ID to your server over HTTP,
// so it can use GCM/HTTP or CCS to send messages to your app.
// The request to your server should be authenticated if your app
// is using accounts.
sendRegistrationIdToBackend();
// For this demo: we don't need to send it because the device
// will send upstream messages to a server that echo back the
// message using the 'from' address in the message.
// Persist the regID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
// If there is an error, don't just keep trying to register.
// Require the user to click a button again, or perform
// exponential back-off.
}
return msg;
}
private void sendRegistrationIdToBackend() {
final int MAX_ATTEMPTS = 5;
final int BACKOFF_MILLI_SECONDS = 2000;
final Random random = new Random();
Log.i(TAG, "registering device (regId = " + regid + ")");
String serverUrl = SERVER_URL;
Map<String, String> params = new HashMap<String, String>();
params.put("regId", regid);
params.put("name",name);
params.put("email",email);
long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
// Once GCM returns a registration id, we need to register on our server
// As the server might be down, we will retry it a couple
// times.
for (int i = 1; i <= MAX_ATTEMPTS; i++) {
Log.d(TAG, "Attempt #" + i + " to register");
try {
post(serverUrl, params);
// displayMessage(context, "Registered");
return;
} catch (IOException e) {
// Here we are simplifying and retrying on any error; in a real
// application, it should retry only on unrecoverable errors
// (like HTTP error code 503).
Log.e(TAG, "Failed to register on attempt " + i + ":" + e);
if (i == MAX_ATTEMPTS) {
break;
}
try {
Log.d(TAG, "Sleeping for " + backoff + " ms before retry");
Thread.sleep(backoff);
} catch (InterruptedException e1) {
// Activity finished before we complete - exit.
Log.d(TAG, "Thread interrupted: abort remaining retries!");
Thread.currentThread().interrupt();
return;
}
// increase backoff exponentially
backoff *= 2;
}
}
// String message = context.getString(R.string.server_register_error,
// MAX_ATTEMPTS);
//CommonUtilities.displayMessage(context, message);
}
private void post(String endpoint, Map<String, String> params)throws IOException{
URL url;
try {
url = new URL(endpoint);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("invalid url: " + endpoint);
}
StringBuilder bodyBuilder = new StringBuilder();
Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
// constructs the POST body using the parameters
while (iterator.hasNext()) {
Map.Entry<String, String> param = iterator.next();
bodyBuilder.append(param.getKey()).append('=')
.append(param.getValue());
if (iterator.hasNext()) {
bodyBuilder.append('&');
}
}
String body = bodyBuilder.toString();
Log.v(TAG, "Posting '" + body+ "' to " + url);
byte[] bytes = body.getBytes();
HttpURLConnection conn = null;
try {
Log.e("URL", "> " + url);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
// post the request
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
// handle the response
int status = conn.getResponseCode();
if (status != 200) {
throw new IOException("Post failed with error code " + status);
}
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
protected void onPostExecute(String msg) {
//setContentView(R.layout.activity_gcm_broadcast_receiver);
}
}.execute(null, null, null);}
private void storeRegistrationId(Context context, String regId) {
final SharedPreferences prefs = getGCMPreferences(context);
//int appVersion = getAppVersion(context);
// Log.i(TAG, "Saving regId on app version " + appVersion);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(PROPERTY_REG_ID, regId);
// editor.putInt(PROPERTY_APP_VERSION, appVersion);
editor.commit();
}
}
这是其中一个片段: -
public class DestinationsFragment extends Fragment {
ViewPager viewPager;
public class DestinationsFragment extends Fragment implements View.OnClickListener {
private TypedArray Icons;
PagerAdapter adapter;
String[] rank;
String[] country;
String[] population;
int[] flag;
public DestinationsFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_5, container, false);
Icons= getResources().obtainTypedArray(R.array.nav_drawer_icons);
View rootView = inflater.inflate(R.layout.activity_5, container, false);
Button button1;
rank = new String[] { "Heading1", "Heading2", "Heading3", "Heading4", "Heading5", "Heading6", "Heading7", "Heading8", "Heading9", "Heading10" };
country = new String[] { "Text1", "Text2", "Text3",
"Text4", "Text5", "Text6", "Text7", "Text8",
"Text9", "Text10" };
population = new String[] { "Subtext1", "Subtext2",
"Subtext3", "Subtext4", "Subtext5", "Subtext6",
"Subtext7", "Subtext8", "Subtext9", "Subtext10" };
flag = new int[] { R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher };
// Locate the ViewPager in viewpager_main.xml
//viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
//adapter = new ViewPagerAdapter(DestinationsFragment.this, rank, country, population, flag);
//adapter = new ViewPagerAdapter(getChildFragmentManager());
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(buildAdapter());
return rootView;
}
@Override
public void onResume(){
super.onResume();
getActivity().setTitle(R.string.dest);
}
private PagerAdapter buildAdapter() {
return(new ViewPagerAdapter(getActivity().getApplicationContext(), rank, country, population, flag));
}
population = new String[] { "Subtext1", "Subtext2",
"Subtext3", "Subtext4", "Subtext5", "Subtext6",
"Subtext7", "Subtext8", "Subtext9", "Subtext10" };
flag = new int[] { R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher };
ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.pager);
viewPager.setClipToPadding(false);
viewPager.setPageMargin(-100);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(buildAdapter());
button1 = (Button) rootView.findViewById(R.id.button1);
button1.setOnClickListener(this);
return rootView;
}
private PagerAdapter buildAdapter() {
return(new ViewPagerAdapter(getActivity().getApplicationContext(), rank, country, population, flag));
}
@Override
public void onClick(View v) {
Toast.makeText(getActivity().getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:1)
试试这个!。
//在家庭活动
public ActionBar getsupportactionbar() {
ActionBar mActionBar = getSupportActionBar();
return mActionBar;
}
关于你的片段:
ActionBar mActionBar = ((HomeActivity) getActivity()).getsupportactionbar();
if(mActionBar !=null)
mActionBar .setTitle(R.string.dest);
或者您可能需要安排OnactivityCreated。