我在android.support.v4.app.Fragment
的应用程序中使用导航抽屉。问题是我的导航菜单总是透明的,有时当我更改片段时,我可以看到新的片段背后的前一个"覆盖" (我有时并不总是说。)
这是我的主要代码:
public class Main extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private ImageLoader mLoader;
static String destinationFile;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private boolean isTracking = false;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
Bundle extras;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
String countnotif="";
static int mNotifCount = 0;
Boolean etat=false;
Bitmap btmap;
String myid,fbname;
String value;
CallbackManager callbackManager;
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES="My profile info";
public static final String keyid="myaccountid";
public static final String keypwd="myaccountpwd";
public static final String keyhotel="myhotelname";
public static final String keyidhotel="idH";
public static final String keynotifcount="countnotif";
public static final String keynotif="notifmsg";
public static final String keynotifmsg="notificationmsg";
private String myidhotel;
String myaccountname;
TextView myemail;
public static final String keyusername="myusername";
public static final String keymail="mymail";
TextView myname,mymail;
LinearLayout drawerll;
JSONArray thumburl = null;
static String imgfromurl;
String datapass;
ProfileTracker profileTracker;
ImageView myimage;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//save the fragment state here
outState.putString("fbuser", myname.getText().toString());
// outState.putString("fbmail",emailT.getText().toString());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.tray);
extras = getIntent().getExtras();
if (extras != null) {
value = extras.getString("hotelname");
}
Toast.makeText(this, "VALUE" + value, Toast.LENGTH_SHORT).show();
mTitle = mDrawerTitle = getTitle();
forceShowActionBarOverflowMenu();
final ActionBar bar = getActionBar();
// bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.blue_pressed_want)));
// getting items of slider from array
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// getting Navigation drawer icons from res
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
drawerll = (LinearLayout) findViewById(R.id.drawerll);
myname=(TextView)findViewById(R.id.usernameacount);
myimage = (ImageView) findViewById(R.id.myimg);
myemail=(TextView)findViewById(R.id.usermailaccount);
// adding nav drawer items to array
// Home
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
// Pages
countnotif = sharedpreferences.getString(keynotifcount, "");
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1), etat, countnotif));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));
// Recycle array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting list adapter for Navigation Drawer
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// Enable action bar icon_luncher as toggle Home Button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.app_name, R.string.app_name) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
MenuInflater inflater = getMenuInflater();
// calling onPrepareOptionsMenu() to show action bars icons
// onCreateOptionsMenu();
invalidateOptionsMenu(); //Setting, Refresh and Rate App
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
displayView(0);
}
Intent intent = getIntent();
if (intent != null)
{
try {
Bundle data = intent.getExtras();
datapass = data.getString("content");
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onActivityResult(int requestCode, int responseCode, Intent data) {
super.onActivityResult(requestCode, responseCode, data);
if(extras!=null) {
callbackManager.onActivityResult(requestCode, responseCode, data);
}
}
/**
* Slider menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected item
displayView(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();//getSupportMenuInflater();
getMenuInflater().inflate(R.menu.activity_main, menu);
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.menu_notif).getActionView();
ImageView bell= (ImageView)badgeLayout.findViewById(R.id.bell);
if (sharedpreferences.contains(keynotifcount)) {
countnotif = sharedpreferences.getString(keynotifcount, "");
mNotifCount= Integer.parseInt(countnotif);
}
if(mNotifCount!=0) {
TextView notifCount = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
notifCount.setVisibility(View.VISIBLE);
notifCount.setText(String.valueOf(mNotifCount));
etat=true;
}
if(mNotifCount==0) {
TextView notifCount = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textviewoff);
etat=false;
}
Toast.makeText(this, "notifcount" + mNotifCount, Toast.LENGTH_SHORT).show();
bell.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//startActivity(new Intent(Main.this, Management.class));
displayView(4);
etat=false;
mNotifCount=0;
countnotif= String.valueOf(mNotifCount);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(keynotifcount, countnotif);
editor.commit();
}
});
// return true;
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// title/icon
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.menu_notif:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//called when invalidateOptionsMenu() invoke
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if Navigation drawer is opened, hide the action items
//boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
boolean drawerOpen = mDrawerLayout.isDrawerOpen(drawerll);
// menu.findItem(R.id.menu_notif).setVisible(drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private void displayView(int position) {
// update the main content with called Fragment
// new JSONParseimg().execute();
Fragment fragment = new Fragment();
//Fragment fragment = null;
/* android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = new Fragment();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();*/
case 0:
fragment = new Home();
break;
case 1:
fragment = new Profile();
break;
case 2:
fragment = new VraiCateg();
break;
case 3:
fragment = new MyLocation();
break;
case 4:
fragment = new Notification();
break;
case 5:
fragment = new Share();
break;
case 6:
fragment = new About();
break;
case 7:
logout();
Intent in = new Intent(this, Auth.class);
startActivity(in);
finish();
break;
default:
break;
}
if (fragment != null) {
android.support.v4.app.FragmentManager fragmentManager= getSupportFragmentManager();
// Fragment fragment = new Fragment();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(drawerll);
} else {
Log.e("this is mainActivity", "Error in else case");
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void forceShowActionBarOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是我的第一个片段:
public class VraiRela extends Fragment implements StaggeredGridView.OnItemClickListener {
ArrayList<VraiRelations> relationsList;
StaggeredAdapterVraiRelations adapter;
StaggeredGridView staggeredGridView;
String passtitle="";
public static final String inputFormat = "HH:mm:ss";
SimpleDateFormat inputParser = new SimpleDateFormat(inputFormat, Locale.FRANCE);
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES="My profile info";
private String myidhotel;
public static final String keyidhotel="idH";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.vrai_rela_main, container, false);
myidhotel = getArguments().getString("idH");
final String ref_type = getArguments().getString("ref_type");
staggeredGridView = (StaggeredGridView) view.findViewById(R.id.grid_view);
relationsList = new ArrayList<VraiRelations>();
StaggeredAdapterVraiRelations adapter = new StaggeredAdapterVraiRelations(getActivity().getApplicationContext(),R.layout.vrai_rela_list_item_sample,relationsList);
staggeredGridView.setAdapter(adapter);
adapter.notifyDataSetChanged();
staggeredGridView.setOnItemClickListener(this);
return view;
}
@Override
public void onItemClick(StaggeredGridView parent, View view, int position, long id) {
String ref_relation=relationsList.get(position).getRefrelation();
Fragment newFragment = new RelationDetails();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Bundle args=new Bundle();
args.putString("ref_relation",ref_relation);
newFragment.setArguments(args);
Toast.makeText(getActivity().getApplicationContext(), "ref_relation"+ref_relation, Toast.LENGTH_LONG).show();
transaction.remove(VraiRela.this);
transaction.replace(R.id.frame_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
private Date parseDate(String date) {
try {
return inputParser.parse(date);
} catch (java.text.ParseException e) {
return new Date(0);
}
}}
这是我的布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:background="@color/grey"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<LinearLayout
android:id="@+id/drawerll"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list_item_pressed"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@color/blue_head_want"
android:gravity="center_vertical"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:background="@drawable/profile_head"
android:layout_height="wrap_content">
<ImageView
android:layout_width="135px"
android:layout_height="135px"
android:id="@+id/myimg"
android:src="@drawable/profile" />
</LinearLayout>
<LinearLayout
android:id="@+id/drawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/blue_head_name"
android:id="@+id/usernameacount"
android:textSize="9sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:id="@+id/usermailaccount"
android:textColor="@color/blue_under_head_name"
android:gravity="center"
android:textSize="7sp" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/blue_under_head_name" />
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="@color/list_background"
android:choiceMode="singleChoice"
android:layout_gravity="start"
android:textColor="@color/blue_item_name"
android:dividerHeight="1dp"
android:alpha="255"
android:listSelector="@drawable/list_selector" />
</LinearLayout>