Reputation: 5
I have an application with one Activity (ActivityMain) and some fragments. A NavigationDrawer controls the switch of the fragments. In some fragments the user has the opportunity to switch to another fragment without opening the NavigationDrawer (for example with a button click).
Everything works well, if I use the NavigationDrawer to switch between fragments, but if I use a control (eg. button) within a fragment to switch to another fragment, I cannot set the selectedItem property of the NavigationDraver's (actually a ListView in the ND) selectedItem property.
The NavigationDrawer's selectedItem property is stored with sharedPreferences, and restored in the onDrawerOpened method in the NavigationDrawer fragment.
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}
I've tried to put the selection index within the onClick event of the View to STATE_SELECTED_POSITION value, as follows, but it doesn't worked. I've also cannot get the value from the sharedPreferences in the other Fragment.
public void navigationRowClick(View view) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
switch(view.getId()) {
case R.id.tr_conv:
sp.edit().putInt(STATE_SELECTED_POSITION, 1);
((MainActivity)getActivity()).changeFrame(1);
((MainActivity)getActivity()).restoreActionBar();
break;
case R.id.trCalc:
sp.edit().putInt(STATE_SELECTED_POSITION, 2);
((MainActivity)getActivity()).changeFrame(2);
((MainActivity)getActivity()).restoreActionBar();
break;
case R.id.trCalo:
Integer i = sp.getInt(STATE_SELECTED_POSITION, 100); // get value test
String s = i.toString();
Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
break;
}
}
My question is, how should I set the selectedItem of the NavigationDrawer from another fragment? Do You have a best practice to this task?
Thanks is advance for the suggestions.
Upvotes: 0
Views: 320
Reputation: 3645
in the onClick event for the button that switches the fragment:
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
((MainActivity) getActivity()).changePosition(1);
sp.edit().putInt(STATE_SELECTED_POSITION, 1).**commit()**;
}
});
//in MainActivity.java
private void changePosition(int position)
{
list.setItemChecked(position, true);
}
this works if you have set the android:choiceMode="singleChoice" attribute to the list.
Another way of doing things is to do it in the adapter of the listview:
.....
{
private int mSelectedItem = 0;
public View getView(int position, View convertView, ViewGroup parent)
{
if(position == mSelectedItem)
{
}
else
{
}
}
public void setSelectedItem(int position)
{
mSelectedItem = position;
}
}
//in MainActivity.java
private void changePosition(int position)
{
adapter.setSelectedItem(position);
adapter.notifyDataSetChanged();
}
Also make sure to commit the changes to the SharedPreferences:
sp.edit().putInt(STATE_SELECTED_POSITION, 1).**commit()**;
maybe you are doing it in some other place, but I don't see it in the snippets you have shown.
Upvotes: 1