Android: How to update a TextView in swipe view sibling fragment? - android

I implemented swipe views in my app following this guide: http://developer.android.com/training/implementing-navigation/lateral.html#horizontal-paging
In my Fragment, I have a TextView, whos content might get updated due to user action. This update is not reflected, when I swipe to another sibling (left or right).
Furthermore, once I have swiped to another Fragment and perform an action to update the TextViews content, the text does not update. However, when I swipe back to the Fragment I came from, the TextView in the first fragment reflects the update I made in the second fragment.
I call upon the TextView by its ID. Looks like, this doesn't get me the TextView in the actual fragment, but always and only the one in the first fragment.
I would like to accomplish two things:
When I swipe to another fragment, I would like the TextView to always show the actual value.
When I perform an action in a Fragment other then the first, I would like the TextView of the current Fragment to get updated.

Since said TextView should be shown in every fragment, it shouldn't be part of the fragment in the first place, but should belong to the parent activity. This way, it can be modified easily from any fragment and always accesses the right one, since there is only one.

Related

How to fix misplacement of view outside of the RecyclerView until user scrolls the RecyclerView

This is the problem I have: https://imgur.com/a/GKZpdX9
The AdView that I have anchored to the bottom of the layout will be slightly pulled out of place and will only come back to its intended place when the user scrolls through the RecyclerView again.
The activity uses two fragments, one for the banner at the bottom and another one on top for the rest of views so when the user clicks a ViewHolder the upper fragment is replaced.
I have tried to use methods such as .scrollToView to force the RecyclerView to move, but it kept the misplacement of the banner. ONLY when the user does scroll comes back into the right place.
I have checked that if instead of replacing the upper fragment the user was taken to a different activity and then the back button was pressed, this problem would not happen. But I need to use a fragment.
Instead of creating seperate fragment for banner in Activity. you should use this code technique in your fragmentActivity. use relative layout for top view in activity then add bannerView with specific height and set alignParentBottom to true. then add another layout which contain your recyclerview fragment and set this property (layoutAbove and pass the addview id to id). it will place exact top of the banner.

Navigate between several ListView

I have a ListView that represents a list of folders and when I click one item, I want to load another list that shows the content of this folder. How can I link these views together and to be able to go back to the first one with the back button ?
Well, since you didn't provide a code in your question, I will try giving an answer in a descriptive manner.
You can use fragments to do this. Your base Activity's layout must have a fragment container which you will use to display your fragment containing the first ListView data. Once after you click on a cell, you call the constructor of the second ListView, and replace the current content of the fragment container with the newly created fragment.
You may implement a back feature by implementing an ArrayList in your Activity and appending the fragments into that array list as the user navigates through the list. onBack pressed you can call the top most fragment from that Array list and assign it to the fragment container.
This should work well, given there are not too many types of ListViews that you may want to implement.

SwipeView that starts with the layout of an intermediary fragment

How can creat a SwipeView that starts with the layout of an intermediary fragment from the collection instead of the first?
e.g.
I have a list of 10 emails of the same person and I click the 3th most recent to show the content in another Activity.
After that, I want to swipe to left and go to the 2nd most recent and swipe to right and go to 4th most recent.
I am following the tutorial
but it seems that the first layout displayed to the user always will correspond to first position from the collection of Fragments.
In my example the user will just be able to swipe to righ and see the emails from 3 to 10.
You can use ViewPager's setCurrentItem() method to specify the initially shown fragment by index, although you should only do this only on the first initialization (i.e. when savedInstanceState is null) so that the ViewPager can persist it's selection through Activity restarts.

Bug? Programmatically added content not shown in Fragment

i have some strange behavior with the content of a fragment.
I created an app with swipeable tab menu, total 4 fragments/tabs.
On the fourth fragment, i add content (TextViews) programmatically, according to 2 spinner on the top of the fragment. So when you choose a value of one of the spinner, the content (inside a LinearLayout) is being replaced with new generated TextViews.
Everything is working nice, BUT:
When both spinner are on the first value (and just there), the content disapears when i swipe to another fragment and come back to the fourth fragment. After selecting another value from one of the spinners, the content is being generated as normal, also when i select the first values again.
When i swipe the first time to the fourth fragment, i see the content.
Any hint?
Looks like your states of the fragments are not saved correctly.
Try setting:
myViewPager.setOffscreenPageLimit(4);

How to swipe to same Fragment but with other data:?

I'm building this application where I have 2 activities. Both of them consist of 3 fragment - one for title, one for content and one for tab control. It is shown at image below.
First activity serves for showing list of some data's headers (item name etc.), search, app info etc. When user presses item in list, app takes him to another activity to show him detail of chosen item. This "details" activity has 6 different content fragments and user switch between them via buttons in tab control fragment (I did switching between content fragments by showing chosen one and hiding all others - I don't know if it's right way, it's my firs app so it came to my mind at first :) ).
And what I would like to do is: When I'm in detail and I swipe left/right then I want app to take me to previous/next item's detail, to same fragment where I currently was in (so not to next content fragment, but to detail of next item in 1st activity's list).
Is this somehow possible please? Because I have totally no clue how to do it :)
And what I would like to do is: When I'm in detail and I swipe
left/rigt then I want app to take me to previous/next item's detail,
to same fragment where I currently was in (so not to next content
fragment, but to detail of next item in 1st activity's list).
If you want to swipe left-right then you would need a ViewPager widget. I'm not sure how should your details activity behave so I'm providing you with two options. Do you want to be able to switch to the next/previous item's details only when a certain fragment is the one currently viewed by the user(from the 6 content fragments, which I assume are related and show various data for a single item)? If yes then in that desired fragment you would replace the current content of the fragment(which will only act as a container) with a ViewPager and use nested fragments for the actual content. If the user switches to the details of a previous/next item's details and then suddenly wants to see the data for that item from one of the remaining 5 content fragments then you would need to have some updates method on them to refresh the data to show the current item(a OnPageChangeListener will be useful here).
Second option, is if you want to allow the user to swipe left/right from any of the 6 content fragments. If this is the case you would use the same method as above but you'll modify each of those 6 fragments.
Showing the next/previous item is easy, just get some sort of identifier of the data(a position, id), retrieve the whole used data(as in the first activity) and then cycle between it.

Categories

Resources