I have a activity that implements a CustomView to inflate the ActionBar with a Checkbox.
This Checkbox is a "LikeButton" to a article from a source and when the user swipe from left/right it load another articles from the same source.
The code is working fine for the first article that the user open, but when he swipe the Checkbox stays with the same state from the firt article.
How can I build a CustomView diferent for every article and inflate it when the user swipe?
Use a ViewPager with Fragments containing your custom views. Also, for each fragment, make sure that you enable it's own options menu by calling Fragment.setHasOptionsMenu(boolean hasMenu);. Once your do this, just Override your optionsMenu methods within your fragment classes.
Related
I am making an app which has a navigation drawer. This navigation drawer has 50 menu items. These items have the same layout, same activity but different data.
I want to implement ViewPager in my app so that the user could swipe left and right (manually and automatically).
Issue: I am facing an issue of how to implement this. The activity (for these menu items) has its own methods and functionality. A ViewPager can slide fragments but my issue is that since all of these menu items have the same layout and functionality then it would be much better to use one fragment whose data change with swipes for each menu items.
I don't know how to approach this. Please guide me.
You can do it by using a recycleView,and you will need an arrayAdapter which will help you adapt the data accordingly and you will need to have a separate class that will have all the data you want, you can also have different constructor in that class. You can check this to know more https://www.codexpedia.com/android/a-very-simple-example-of-android-recyclerview/
I have an Activity that holds some fragments.
This Activity is associated with a view pager, this view pager uses FragmentPagerAdapter, so every page of view pager is treated as a fragment.
Now, suppose I have customize the action bar view in any one of the fragment, and that custom view could be seen in other fragments also.
getActivity().getSupportActionBar().setCustomView(R.layout.custom_view_home);
getActivity().getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
This is because, we are customising the view, using the activity context.
My Question :
Q. Cant we set the Custom View of action bar within a fragment ? So, it would not get reflected to other fragment.
Short answer: Yes.
You should only allow currently visible fragment to add custom view to the ActionBar. Of course, you can do it right from the Fragment, with whichever context.
Requesting invalidation of the options menu will remove current custom view with the new one, i.e invalidating entire ActionBar. Similar approach you can use from the link above.
I am using a Custom FragmentPagerAdapter for SwipeViews in ActionBar.NAVIGATION_MODE_TABS mode.
I want the Fragments inside the adapter to provide their custom Menus. But I observed that onCreateOptionsMenu() method inside the Fragment is not getting called even when I used setHasOptionsMenu(true);
inside the onCreate() callback of the Fragment.
In short,How can I get custom Options Menu for each Fragment in ViewPager?
Please write the code in onCreateOptionsMenu & onPrepareOptionsMenu of your activity to inflate the from menu xml. You can then customize the menu in your fragment by using these functions inside your fragment again. I tried it and it worked for me.
Actually your problem seems to lie in ViewPager. Either its not instantiated properly or its Visibility is set to Visibility.GONE.
Make sure that ViewPager is Visible on screen by default or there is no code which changes its visibility. Get back to me if problem persists.
I'm using SlidingMenu lib to implement a sliding side menu. I can add a list view, or other views to the menu as: menu.setMenu(R.layout.static_list_layout); and it's working perfectly as wanted.
My needs are more than a static list, I used to have an activity that relies on an empty list_view and programmatically fill up the list according to user profile, the contents are dynamic.
Just as facebook left menu, where user groups are listed. I want to attach this same activity to dynamically fill the list in the menu. Is this possible? Any example code?
Even if you find a way to do it you shouldn't use an Activity for what you are describing.
The better option is to use a Fragment class, a quick search found me an example here:
Dynamic UI with sliding menu and actionbarsherlock
The good thing about fragments is that they have pretty much the same lifecycle as activities so you get all the things that an activity has.
The other option is to use your own view (that is extend it).
you can create a class that extends FrameLayout for example and add an inflated view to it. This will also enable you to write additional logic based on dynamic properties.
However i suggest you to look into fragments and do it the "fragments" way.
Activities span the full screen, this is not possible.
You can try to use a fragment however.
I'm having a bit of trouble with communication between a parentActivity and its child fragment.
I am using actionbarsherlock.
I have looked around on SO, but I haven't found something that deals with ViewPagers and Spinners yet.
Background:
My child fragment has its own action bar specifying a couple of extra action items not available in other fragments, one of these action items is a spinner with multiple options - A, B and C etc
By default the MainActivity will load Fragment_A(child fragment) which contains a ViewPager.
This view pager handles swipes between views A and B(Spinner has options A,B, C etc)
Fragment_A also has spinner adapter. If you select A on the spinner, the viewPager switches to A, if you select B on the spinner, the viewPager switches to B. (This works fine).
My question is: How do I capture when the user "swipes" between A and B(They don't use the spinner) and change my "spinner" to say A or B respectively?
Note: My pager adapter(for ViewPager) is created separately in a different class.
Any help is appreciated...Thank you all!
This is what you need. You'll have to set up an interface in your Fragment and implement that interface in the parent activity. In your Fragment instantiate an instance of the interface and in whatever method you use to listen to page changes, call the method to communicate that data to the activity.
It seems kinda "hand-wavy" but I hope that clears it up a little.