1
Ich habe meine App von allen Aktivitäten zu einem NavigationDrawer Setup mit Fragmenten geändert. Eine meiner Aktivitäten hat oben ein Menü, das wird mit diesem neuen Setup nicht mehr angezeigt. Hier ist mein Code:Navigationdrawer und Menü-Inflater
Java
public class BurgerMenu extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_burger_menu);
Toolbar toolbar = setToolBar();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
LoginFragment firstFragment = new LoginFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
}
private Toolbar setToolBar() {
Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tb);
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
return tb;
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.burger_menu, menu);
return true;
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<include layout="@layout/toolbar"/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_burger_menu"
app:menu="@menu/activity_burger_menu_drawer" />
Und hier ist das Fragment mit dem Menü:
public class MainActivity extends Fragment {
public List<MoodEntry> moodList;
private MoodEntryDataSource dataSource;
public Date month_year_date;
private int status = 0;
private View v;
private FragmentActivity fragmentActivity;
private Context context;
private LineChart chart;
private TextView textTop;
private TextView textMid;
private TextView textBot;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragmentActivity = super.getActivity();
v = inflater.inflate(R.layout.activity_main, container, false);
context = fragmentActivity.getApplicationContext();
dataSource = new MoodEntryDataSource(context);
month_year_date = new Date();
final RadioGroup radioGroup = (RadioGroup) v.findViewById(R.id.radioButtonGroupStatus);
final RadioButton private_radio = (RadioButton) v.findViewById(R.id.radioButtonPrivatStart);
final RadioButton business_radio = (RadioButton) v.findViewById(R.id.radioButtonBusinessStart);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (private_radio.isChecked()) {
status = MoodEntry.PRIVATE;
} else if (business_radio.isChecked()) {
status = MoodEntry.BUSINESS;
}
loadListEntries();
}
});
// Graph
textTop = (TextView) v.findViewById(R.id.textView7);
textMid = (TextView) v.findViewById(R.id.textView9);
textBot = (TextView) v.findViewById(R.id.textView8);
setYLabelVisibility(false);
chart = (LineChart) v.findViewById(R.id.chart);
chart.setTouchEnabled(false);
chart.setDescription("Datum");
chart.setNoDataText(this.getString(R.string.no_data_to_show));
XAxis xAxis = chart.getXAxis();
//xAxis.setLabelsToSkip(5);
xAxis.setDrawAxisLine(true);
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setAvoidFirstLastClipping(true);
YAxis left = chart.getAxisLeft();
left.setAxisMinValue(-5);
left.setAxisMaxValue(5);
left.setDrawZeroLine(true);
left.setLabelCount(0, false);
left.setDrawLabels(false);
//left.setShowOnlyMinMax(true);
chart.getAxisRight().setEnabled(false);
return v;
};
@Override
public void onResume() {
super.onResume();
dataSource.open();
loadListEntries();
}
@Override
public void onPause() {
super.onPause();
dataSource.close();
}
/**
* Shows the entries for month in month_year_date in diagram and list
*/
private void loadListEntries() {
TextView textView = (TextView) v.findViewById(R.id.textView_MonthYear);
textView.setText(new SimpleDateFormat("MMMM yyyy", Locale.GERMAN).format(month_year_date));
List<MoodEntry> moodListForGraph = dataSource.getMoodEntriesByMonthAndByStatus(month_year_date, MoodEntry.SORT_BY_DATE,status);
updateData(moodListForGraph);
moodList = dataSource.getMoodEntriesByMonthAndByStatus(month_year_date, MoodEntry.SORT_BY_MOOD,status);
MoodEntryListAdapter adapter = new MoodEntryListAdapter(fragmentActivity, moodList);
ListView moodListView = (ListView) v.findViewById(R.id.listview_moods);
moodListView.setAdapter(adapter);
moodListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
showDetailView(position);
}
});
}
/**
* Shows the entries of the month selected by date
* @param date date within month that should be shown
*/
public void setMonthYear(Date date) {
month_year_date = date;
loadListEntries();
}
/**
* Opens the Detail View of list item on pos
* @param pos pos of selected item
*/
public void showDetailView(int pos) {
Bundle bundle = new Bundle();
bundle.putLong("entry_id", moodList.get(pos).getId());
Fragment moodDetail = new MoodDetailActivity();
moodDetail.setArguments(bundle);
FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, moodDetail);
transaction.addToBackStack(null);
transaction.commit();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_entry:
Fragment addEntry = new AddEntryActivity();
FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, addEntry);
transaction.addToBackStack(null);
transaction.commit();
return true;
case R.id.action_date_picker:
Calendar cal = Calendar.getInstance();
cal.setTime(month_year_date);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = 1;
// TODO 0 durch Theme ersetzen. 0 war: AlertDialog.THEME_HOLO_LIGHT
DatePickerDialog dpd = new DatePickerDialog(getActivity(), 0,datePickerListener,year, month, day){
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
int day = getContext().getResources().getIdentifier("android:id/day", null, null);
if(day != 0){
View dayPicker = findViewById(day);
if(dayPicker != null){
dayPicker.setVisibility(View.GONE);
}
}
}
};
dpd.show();
//DatePickerFragment datePickerFragment = new DatePickerFragment();
//datePickerFragment.show(fragmentActivity.getFragmentManager(), "Date Picker");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.set(year, month, 1);
setMonthYear(cal.getTime());
}
};
// Graph
public void updateData(List<MoodEntry> moodEntries) {
chart.clear();
ArrayList<Entry> entryList = new ArrayList<Entry>();
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < moodEntries.size(); i++) {
MoodEntry moodEntry = moodEntries.get(i);
int mood_value = moodEntry.getMood();
xVals.add(moodEntry.getDisplayDateOnly());
entryList.add(new BarEntry(mood_value, i));
}
LineDataSet dataSet = new LineDataSet(entryList, "Einträge");
dataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
dataSet.setLineWidth(2f);
dataSet.setColor(getColor(R.color.graph_line));
dataSet.setCircleColor(getColor(R.color.graph_line));
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(dataSet);
LineData data = new LineData(xVals, dataSets);
data.setDrawValues(false);
if (moodEntries.size()!=0) {
chart.setData(data);
Legend legend = chart.getLegend();
legend.setEnabled(false);
setYLabelVisibility(true);
chart.notifyDataSetChanged();
chart.invalidate();
} else {
setYLabelVisibility(false);
}
}
private int getColor(int colorRes) {
return ContextCompat.getColor(getContext(), colorRes);
}
private void setYLabelVisibility(boolean visible) {
if (visible) {
textTop.setVisibility(View.VISIBLE);
textMid.setVisibility(View.VISIBLE);
textBot.setVisibility(View.VISIBLE);
} else {
textTop.setVisibility(View.INVISIBLE);
textMid.setVisibility(View.INVISIBLE);
textBot.setVisibility(View.INVISIBLE);
}
}
}
Die Symbolleiste der Navigationsleiste wird nicht mit diesem Fragment angezeigt, auch nicht das Fragmentmenü.
Jede Lösung?