Change the NavigationDrawer selectedItem from another fragment - android

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.

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.

Related

onClickListener is called earlier than Fragment onCreateView

I have an Recycler.Adapter and my onBindViewHolder is like this:
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final String url = urls.get(position);
final width_height wh = whs.get(position);
holder.imageView.setClickable(true);
Picasso.with(context)
.load(url).resize(wh.width, wh.height).centerCrop()
.into(holder.imageView);
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialogFragment.show(fragmentManager, "");
dialogFragment.setImage(url);
dialogFragment.setRating(0);
}
});
holder.textView.setText(position + "");
holder.textView.setClickable(true);
holder.textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialogFragment.show(fragmentManager, "");
dialogFragment.setImage(url);
dialogFragment.setRating(0);
}
});
}
The dialogFragment is already instantiated in the contructor of Adapter. However, its onCreateView has not been called yet when the holder is clicked. And the setImage and setRating need to modify its imageView and RatingBar, which are null unless the onCreateView of the fragment is called. Is there a way to resolve this?
That's because DialogFragment.show commits asynchronously. In your case you'd want it to use DialogFragment.showNow instead so it commits synchronously.
#Override
public void onClick(View v) {
dialogFragment.showNow(fragmentManager, "");
dialogFragment.setImage(url);
dialogFragment.setRating(0);
}
Otherwise I'd suggest to create new DialogFragment every time the button is clicked, and pass image url and rating as arguments before it is shown.
A more correct way would be to have the setImage method and setRating method check for null. If null, they should save the value to variable, and onCreate should use those values to set.
For example (not tested);
void setImage(src) {
if (mImage == null)
mImageSource = src;
else
mImage.setImage(src);
}
void onCreateView(...) {
...
if (mImageSource != null)
mImage.setImage(mImageSource);
mImageSource = null;
}
This way you can correctly use the asynchronous dialog calls.

Android RadioGroup onCheckedChanged is called again and again when refreshing Fragment

I want to implement a setup assistant for my app which is shown when the user starts the app for the first time. I use a PageViewer for this which seems to work fine.
The first page asks the user for the default language. I want to refresh the page when the user selects its language from the RadioGroup. So I need a way to reload the fragment in the chosen language.
public class SetupAssistantLanguageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.setup_language_fragment, container, false);
((RadioGroup) view.findViewById(R.id.setup_language_radiogroup)).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
String langcode = "";
if (i == R.id.setup_fragment1_languagechooser_en) {
langcode = "en";
} else {
langcode = "de";
}
((SetupAssistantActivity) getActivity()).changeLanguage(langcode);
SetupAssistantLanguageFragment.this.refreshView();
System.out.println("test1");
}
});
return view;
}
public void refreshView() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
System.out.println("test2");
}
}
Changing the language works but only for the pages that follow the first page. This is because the fragment needs to be reloaded when the language changed. I try to reload in refreshView().
The problem is: As soon as I press one radio button, I get an endless loop printing "test1" and "test2". Why is this method called again and again?
How can I reload the fragment exactly one time?
I got it:
In my fragment I first save the current language.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentDisplayLang = getResources().getConfiguration().locale.getLanguage();
}
In onCreateView I only refresh the fragment when the language really changed.
((RadioGroup) view.findViewById(R.id.lecture_translator_setup_language_radiogroup)).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
String chosenLangcode = i == R.id.setup_fragment1_languagechooser_en ? "en" : "de";
if (!chosenLangcode.equals(currentDisplayLang)) {
currentDisplayLang = chosenLangcode.toString();
((SetupAssistantActivity) getActivity()).changeLanguage(chosenLangcode);
SetupAssistantLanguageFragment.this.refreshView();
}
}
});
Is this the recommended way?

recyclerview item changes button style after scroll

I have a recyclerview with simple items - an item has an image, title and a button. When the user clicks on the button it needs to change its layout -> indicating that button is clicked (similar to checkbox functionality).
Problem is that when I click on a button, for example the second item, it behaves weirdly when I scroll - multiple items are toggled or the original one is untoggled. You can check it out in image here:
GIF PREVIEW
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
holder.button.setOnClickListener(view -> {
toggleButtonStyle((Button)view);
});
}
public void toggleButtonStyle(Button toggle)
{
Context ctx = toggle.getContext();
if (toggle.isActivated()) {
toggle.setActivated(false);
toggle.setBackground(ContextCompat.getDrawable(ctx, R.drawable.btn_purple_corners));
toggle.setTextColor(ContextCompat.getColor(ctx, R.color.purple_light));
} else {
toggle.setActivated(true);
toggle.setBackground(ContextCompat.getDrawable(ctx, R.drawable.btn_purple));
toggle.setTextColor(ContextCompat.getColor(ctx, R.color.white));
}
}
You need to specify position for a onBindView method, because it's called multiple times. If you are using AutoValue immutable types, you can try to make an Array of booleans where you'll be storing toggled states (true/false). Then in onBindViewMethod check a value of current position and style a button.
private boolean[] toggledChoices = new boolean[yourListWithItems.size()];
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (toggledChoices[position]) {
toggleButtonStyle(true, holder.toggleButton);
} else {
toggleButtonStyle(false, holder.toggleButton;
}
holderVote.bToggleItem.setOnClickListener(v -> {
if (!toggledChoices[position]) {
toggledChoices[position] = true;
} else {
toggledChoices[position] = false;
}
});
}
I had some strange behavior too in the past.
For me it was solved by simply calling
toggle.invalidate();
after your if/else block. This forces the redraw. Try it, I was surprised too, that this fixed it for me.
Check Out my code
#Override
public void onBindViewHolder(GifGridAdapter.ViewHolder holder, final int position) {
final DataModel model = arrayList.get(position);
if (model.isToggle()) {
//Code for changing the button background color and text color
} else {
//Code for changing the button background color and text color
}
holder.ivGif.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
model.setToggle(!model.isToggle());
notifyDataSetChanged();
}
});
}

MainActivity Get Position Of Item From PagerAdapter [duplicate]

I want to know the current page position of the ViewPager. I create an adapter and set this adapter to the ViewPager. Now I want to know the current page number or position of the current view, because I want to start some extra events on that particular page.
I used viewPager.setCurrentItem(1); for setting the first item.
Is there a similar method for getting the current page?
in the latest packages you can also use
vp.getCurrentItem()
or
vp is the viewPager ,
pageListener = new PageListener();
vp.setOnPageChangeListener(pageListener);
you have to put a page change listener for your viewPager. There is no method on viewPager to get the current page.
private int currentPage;
private static class PageListener extends SimpleOnPageChangeListener{
public void onPageSelected(int position) {
Log.i(TAG, "page selected " + position);
currentPage = position;
}
}
There is a method object_of_ViewPager.getCurrentItem() which returns the position of currently Viewed page of view pager
If you only want the position, vp.getCurrentItem() will give it to you, no need to apply the onPageChangeListener() for that purpose alone.
You will figure out that setOnPageChangeListener is deprecated, use addOnPageChangeListener, as below:
ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position == 1){ // if you want the second page, for example
//Your code here
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
For this problem Onpagechange listener is the best one But it will also have one small mistake that is it will not detect the starting time time of 0th position Once you will change the page it will starts to detect the Page selected position...For this problem I fount the easiest solution
1.You have to maintain the selected position value then use it....
2. Case 1: At the starting of the position is always Zero....
Case 2: Suppose if you set the current item means you will set that value into maintain position
3.Then do your action with the use of that maintain in your activity...
Public int maintain=0;
myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i2) {
//Toast.makeText(MyActivity.this, i+" Is Selected "+data.size(), Toast.LENGTH_SHORT).show();
}
#Override
public void onPageSelected( int i) {
// here you will get the position of selected page
maintain = i;
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
updateButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, i+" Is Selected "+data.size(), Toast.LENGTH_SHORT).show();
data.set(maintain, "Replaced "+maintain);
myViewPager.getAdapter().notifyDataSetChanged();
}
});
getCurrentItem(), doesn't actually give the right position for the first and the last page I fixed it adding this code:
public void CalcPostion() {
current = viewPager.getCurrentItem();
if ((last == current) && (current != 1) && (current != 0)) {
current = current + 1;
viewPager.setCurrentItem(current);
}
if ((last == 1) && (current == 1)) {
last = 0;
current = 0;
}
display();
last = current;
}
The setOnPageChangeListener() method is deprecated. Use
addOnPageChangeListener(OnPageChangeListener) instead.
You can use OnPageChangeListener and getting the position inside onPageSelected() method, this is an example:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.d(TAG, "my position is : " + position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Or just use getCurrentItem() to get the real position:
viewPager.getCurrentItem();
There is no any method getCurrentItem() in viewpager.i already checked the API

Hide the view in recyclerview's item

i facing a problem in recyclerview's item.
My Adapter's code :
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Profile item = mListChatting.get(position);
Log.d("TAG", "CEK : " + viewable);
if(viewable==true){
holder.mFormBookingan.setVisibility(View.GONE);
holder.mDetailBookingan.setVisibility(View.VISIBLE);
}else{
//assume that one way is show first as default
holder.mViewOneWay.setVisibility(View.VISIBLE);
holder.mViewRoundTrip.setVisibility(View.GONE);
holder.mOneOway.setBackgroundResource(R.drawable.round_just_left_white_focus);
holder.mRoundTrip.setBackgroundResource(R.drawable.state_pressed_booking_button_left);
holder.mSendBooking.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewable = true;
Log.d("TAG", "CEK 2 : " + viewable);
}
});
}
Like my code above, I want to hide mFormBookingan after mSendBooking has pressed. mFormBookingan never show anymore until user calls it again.
I have tried with a lot of ways but still can't find like what i need. After i press mSendBooking the form hide, but when i send new item to recyclerview, the from mFormBookingan that has been hide, appears again.
My Question, how to hide mFormBookingan forever? Until user call it again.
Thank in advance, i will appreciate anyone who help me for this one.
I not sure what the clear situation you want.
But if you want to set View invisible you can try this code then check it.
You need to add ismFormBookingVisible in viewHolder class as a boolean attribute.
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Profile item = mListChatting.get(position);
Log.d("TAG", "CEK : " + viewable);
if(holder.ismFormBookingVisible==true){
holder.mFormBookingan.setVisibility(View.GONE);
holder.mDetailBookingan.setVisibility(View.VISIBLE);
}else{
//assume that one way is show first as default
holder.mViewOneWay.setVisibility(View.VISIBLE);
holder.mViewRoundTrip.setVisibility(View.GONE);
holder.mOneOway.setBackgroundResource(R.drawable.round_just_left_white_focus);
holder.mRoundTrip.setBackgroundResource(R.drawable.state_pressed_booking_button_left);
holder.mSendBooking.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.ismFormBookingVisible = false;
Log.d("TAG", "CEK 2 : " + viewable);
}
Try this :
Create a boolean in your model class "Profile" to keep track of visibility of button : say boolean isBookingVisible;
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Profile item = mListChatting.get(position);
if(!item.isBookingVisible){
holder.mFormBookingan.setVisibility(View.GONE);
holder.mDetailBookingan.setVisibility(View.VISIBLE);
}else{
//assume that one way is show first as default
holder.mViewOneWay.setVisibility(View.VISIBLE);
holder.mViewRoundTrip.setVisibility(View.GONE);
holder.mOneOway.setBackgroundResource(R.drawable.round_just_left_white_focus);
holder.mRoundTrip.setBackgroundResource(R.drawable.state_pressed_booking_button_left);
holder.mSendBooking.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
item.isBookingVisible = false;
//Use notiyItemChanged(position); or notifyDataSetChanged(); here as per your selection criterion
Log.d("TAG", "CEK 2 : " + viewable);
}
});
}
You may have to call notifyDataSetChanged() after changing the value of viewable.
onBindViewHolder will be called when you notify your data set.
So you need to save the viewable in mListChatting. When you click the button, change the viewable in the mListChatting.
And then, change the code in onBindViewHolder
holder.mFormBookingan.setVisibility(item.getViewable() ? View.VISIBLE : View.GONE);
On refresh android will destroy the view and create new view with new adapter data. So you have to track the current state (visibility) of mFormBookingan. You can use a simple visibility list. When mFormBookingan state (visibility) change update it in visibility list so that whenever the list is refreshed, you can use it to check and set the last state (visibility) of your mFormBookingan. Here is an example
private ArrayList<Boolean> isVisible;
public MyAdapter(ArrayList<Boolean> isVisible){
// initial state list of mFormBookingan for each row of list
this.isVisible = isVisible;
}
public void onBindViewHolder(final MyViewHolder holder, final int position) {
if (isVisible.get(position)) {
holder.mFormBookingan.setVisibility(View.VISIBLE);
}else {
holder.mFormBookingan.setVisibility(View.GONE);
}
holder.mSendBooking.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.mFormBookingan.getVisibility() == View.GONE){
holder.mFormBookingan.setVisibility(View.VISIBLE);
isVisible.set(position, true);
}else {
holder.mFormBookingan.setVisibility(View.GONE);
isVisible.set(position, false);
}
}
});
}
when you click mSendBooking the mFormBookingan visibility will change and it will remain same after sending new item to recyclerview.

Categories

Resources