这里我使用NavigationDrawer进行我的活动。它工作正常。我正在使用Recyclerview来显示NavigationDrawer项目。现在我想为不同的Activity显示不同的NavigationDrawer项目。我在FragmentDrawer中使用nav_drawer_labels来显示items.How如何在另一个activity中显示nav_drawer_labels1。我的意思是我必须在每个活动中显示不同的NavigationDrawer。这是我的代码。
CompProfileDisplay.java
public class CompProfileDisplay extends AppCompatActivity implements OnItemClickListener, FragmentDrawer.FragmentDrawerListener {
private static String TAG = CompProfileDisplay.class.getSimpleName();
private MenuItem menuitems;
private Toolbar mToolbar;
private FragmentDrawer drawerFragment1;
private ImageView img;
private int count;
private TextView textView;
private String doc;
public static final String UPLOAD_URL = "http://website.com/aboutme_update.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//getActionBar().setDisplayShowTitleEnabled(false);
setContentView(R.layout.compprofile_display);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
img = (ImageView) findViewById(R.id.imageView10);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
final Context context = this;
drawerFragment1 = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment1.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment1.setDrawerListener(this);
// display the second navigation drawer view on app launch
displayView(0);
SharedPreferences sharedPreferences = getSharedPreferences(ProfileLogin.SHARED_PREF_NAME, MODE_PRIVATE);
SharedPreferences sharedPreferences1 = getSharedPreferences(ClickToComp.SHARED_PREF_NAME, MODE_PRIVATE);
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//no title in dialog.
dialog.getWindow().setBackgroundDrawable( new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.im);
Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );
TextView txt = (TextView) dialog.findViewById(R.id.custom_fullimage_placename);
txt.setTypeface(font);
String url1 = "http://www.website.com/" + doc;
new DownloadImageTask((ImageView) dialog.findViewById(R.id.fullimage))
.execute(url1);
//on click of cancel icon/btn popup screen will close.
txt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
});
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent;
switch (position) {
default:
intent = new Intent(CompProfileDisplay.this, Banking.class);
startActivity(intent);
break;
}
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.testAction);
menuItem.setIcon(buildCounterDrawable(count, R.drawable.add));
return true;
}
private Drawable buildCounterDrawable(int count, int backgroundImageId) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.action_bar_notifitcation_icon, null);
view.setBackgroundResource(backgroundImageId);
if (count == 0) {
View counterTextPanel = view.findViewById(R.id.counterValuePanel);
counterTextPanel.setVisibility(View.GONE);
} else {
textView = (TextView) view.findViewById(R.id.count);
textView.setText("" + count);
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(CompProfileDisplay.this, "You have new friend request", Toast.LENGTH_SHORT).show();
}
});
}
view.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return new BitmapDrawable(getResources(), bitmap);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.testAction) {
if(count>0) {
Toast.makeText(getApplicationContext(), "You have new friend request", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "You dont have any new notification", Toast.LENGTH_SHORT).show();
}
return true;
}
if(id == R.id.action_search){
/*Toast.makeText(getApplicationContext(), vault_no, Toast.LENGTH_SHORT).show();
return true;*/
//logout();
}
if(id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
/*fragment = new HomeFragment();*/
title = "Profile";
break;
case 1:
/*fragment = new ProfileFragment();
title = getString(R.string.title_profile);
break;*/
startActivity(new Intent(this, CompProfileDisplay.class));
return;
case 2:
startActivity(new Intent (this,DisplayContact.class));
return;
case 3:
startActivity(new Intent(this, DocUpload.class));
return;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
}
FragmentDrawer.class
public class FragmentDrawer extends Fragment {
private static String TAG = FragmentDrawer.class.getSimpleName();
public static final String SHARED_PREF_NAME = "myloginapp";
private RecyclerView recyclerView;
private String doc;
private String fullname;
private String vault;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private NavigationDrawerAdapter adapter;
private View containerView;
private static String[] titles = null;
private FragmentDrawerListener drawerListener;
public FragmentDrawer() {
}
public void setDrawerListener(FragmentDrawerListener listener) {
this.drawerListener = listener;
}
public static List<NavDrawerItem> getData() {
List<NavDrawerItem> data = new ArrayList<>();
// preparing navigation drawer items
for (int i = 0; i < titles.length; i++) {
NavDrawerItem navItem = new NavDrawerItem();
navItem.setTitle(titles[i]);
data.add(navItem);
}
return data;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// here i am displaying nav_drawer_labels items in one activity.
titles = getActivity().getResources().getStringArray(R.array.nav_drawer_labels);
//i want to display nav_drawer_labels1 in another activity.
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflating view layout
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
ImageView img = (ImageView)layout.findViewById(R.id.img);
TextView name = (TextView)layout.findViewById(R.id.name);
TextView vault_no = (TextView)layout.findViewById(R.id.vault);
SharedPreferences sharedPreferences1 = this.getActivity().getSharedPreferences(FragmentDrawer.SHARED_PREF_NAME, Context.MODE_PRIVATE);
vault = sharedPreferences1.getString(ProfileLogin.EMAIL_SHARED_PREF,"Not Available");
SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences(FragmentDrawer.SHARED_PREF_NAME, Context.MODE_PRIVATE);
doc = sharedPreferences.getString(ClickToLogin.DOC_SHARED_PREF,"Not Available");
fullname = sharedPreferences.getString(ClickToLogin.FULLNAME_SHARED_PREF,"Not Available");
name.setText(fullname);
vault_no.setText(vault);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
String url1 = "http://www.miisky.com/"+doc;
new DownloadImageTask((ImageView) layout.findViewById(R.id.img))
.execute(url1);
adapter = new NavigationDrawerAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
drawerListener.onDrawerItemSelected(view, position);
mDrawerLayout.closeDrawer(containerView);
}
@Override
public void onLongClick(View view, int position) {
}
}));
return layout;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {
containerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
toolbar.setAlpha(1 - slideOffset / 2);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
}
public static interface ClickListener {
public void onClick(View view, int position);
public void onLongClick(View view, int position);
}
static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
public interface FragmentDrawerListener {
public void onDrawerItemSelected(View view, int position);
}
}
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="nav_item_home1">Home</string>
<string name="nav_item_profile1">Profile</string>
<string name="nav_item_customersupport">Customer Support</string>
<string name="nav_item_dealer">Dealer Network</string>
<string-array name="nav_drawer_labels1">
<item>@string/nav_item_home1</item>
<item>@string/nav_item_profile1</item>
<item>@string/nav_item_customersupport</item>
</string-array>
<string name="nav_item_home">Home</string>
<string name="nav_item_profile">Profile</string>
<string name="nav_item_contacts">Contacts</string>
<string name="nav_item_docupload">Upload Documents</string>
<!-- navigation drawer item labels -->
<string-array name="nav_drawer_labels">
<item>@string/nav_item_home</item>
<item>@string/nav_item_profile</item>
<item>@string/nav_item_contacts</item>
</string-array>
</resources>
答案 0 :(得分:0)
导航抽屉意味着与碎片一起使用。
主要活动托管导航抽屉。
片段可以轻松交换抽屉内容并保持其状态。
如果你想使用acitvity,那么你应该创建一个基础活动,让你的所有活动继承该活动。
这样导航抽屉可以从活动持续到活动。