How to do a "swipe information" activity? - java

In my project I have an Activity that loads a list of news. When the list is loaded, this Activity is populated with the information of the first item on the list:
But since this loads all the news from the server, I want the user to be able to swap the news with his finger. For example: we start with news[0] and user swipe left, the content change to news[1]. The user swipe left again and the content changes to news[2] and so on... if the user swipes right, then it return to the previous news (news[1]). The header should not have a swipe movement, only the main frame (image and body should change):
What would be the best aproach for this behavior? I was thinking about using a TabLayout with ViewPager but I'm not sure if this is the correct way to do this. If I have something like 100 news this could be a overkill to load 100 fragments in the ViewPager right?
Does android have a better way to do this? How should I do this?

You can use ViewPager for that. Having 100 fragments inside is totally fine because ViewPager will only load as much as you set using setOffscreenPageLimit(pageLimit). For instance, if you set pageLimit = 3, ViewPager will only initialize 3 (up to 6) neighbour fragments that are on the right/left side of your currently visible fragment. And while you're swiping through fragments, it will kill the fragments that are out of this limit, and load new ones. So, there will be no overkill.

Related

I don't understand how to organise my fragments

I have realised an NFC reader application
So i have 3 activities :
MainActivity, which is an activity who contains a Button. If button is clicked, the scan is activated and the user can put his NFC tag against the device to detect it.
WebActivity, who is launched if the NFC tag contains and URL (and open a WebView) or if the user want to launch WebActivity by himself
HistoryActivity, who gonna contains a list of every scans.
Now, I would like to swipe activity with a finger gesture. according to my research on Internet. I need fragments and ViewPager.
But every example that I saw is bases on ONE activity and multiple fragments.
But in my case, I have to create 3 fragments (one per activity), right ?
And I really don't know how to manage my fragment. I mean, what to put inside ?
All I want to do is create a transition/animation while changing activity... That's crazy
This is too broad of a question but hopefully my answer will steer you in right direction.
You should definitely go with single activity/multiple fragments model. Aside of recommendations by Google, you could use navigation components, deep linking much easier then without single activity.
Yes you should be using ViewPager for the purpose (and likely your implementation of FragmentPagerAdapter as well) however I do not understand what kind of swiping will you be doing
Reading your setup, I would suggest to use bottom view with 2 items (good example is here https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample/app/src/main/java/com/example/android/navigationadvancedsample). 2nd one would show history, first one would offer a button that activates your action, and then displays fragment with your WebView.
As a side effect of such implementation, you'd be able to go back from 2nd bottom view item to whatever first one holds - by pressing system back button - which I think is nice touch.
UPDATE to "swiping takes place anytime. " comment:
You could have single activity, ViewPager with 2 fragments. First fragment would display a button, 2nd fragment would display a history. You could freely swipe between them, as you want to. However to me it does not make sense to put WebView screen into this. WebView screen is result of action (NFC detection) and it should probably display as full screen, without any chance of swiping between main/history and itself. Hope it helps or I'm missing some important piece of info you did not share.

How to keep 1000 dynamic fragments inside viewpager android?

I need to show one thousands question with 4 options to fill using one activity. I got an idea to use viewpager, but cant able to show 1000 fragment.
It is possible to use ViewPager with 1000 fragments. Just make sure that your adapter extends FragmentStatePagerAdapter.
From the documentation:
This version of the pager is more useful when there are a large number
of pages, working more like a list view. When pages are not visible to
the user, their entire fragment may be destroyed, only keeping the
saved state of that fragment. This allows the pager to hold on to much
less memory associated with each visited page as compared to
FragmentPagerAdapter at the cost of potentially more overhead when
switching between pages.

Issue with Camera in fragment

I have Activity which contains ViewPager which implements FragmentPagerAdapter with 3 fragments and I have implemented Camera in one of the fragments using SurfaceView. So when users swipes to the left from my home fragment he is able to use Camera (like in WhatsApp). The issue is when starting app (where I get some data across internet and put into listview) or sometimes swipping to the left or right Camera appears in main or home fragment for a second or less and then disappers and home fragment is presented at it should be.
Here is the problem when downloading data, camera occupies home fragment until
data is loaded. And for now that is for a second or less:
https://i.stack.imgur.com/wsEUy.jpg
And this is how home fragment actually looks after downloading data:
https://i.stack.imgur.com/AUv3V.png
I tried with adding background color to my home Fragment container xml
but that didn't work.
I followed this example to implement camer in fragment:
https://github.com/rexstjohn/UltimateAndroidCameraGuide/blob/master/camera/src/main/java/com/ultimate/camera/fragments/NativeCameraFragment.java
Can someone help or give some suggestions what's happening and what to do? Thanks
I found solution which fits my needs. So here it is if someone in future stuck with same issue. Somehow the problem was when I add my CamerPreview to View in my case that was FrameLayout and that was in OnCreateView of CameraFragment. Even if I try to start camera preview when CameraFragment is visible to user and stop preview when it's not visible that didn't help I was always getting flash over home fragment by Camera preview when swipping to the left or right.
So I create new Fragment which is like placeholder and I added it to FragmentPagerAdapter and then there was an option to create my CameraFragment using FragmentTransaction when my placeholder fragment is visible to user (that works too) but I used ViewPager detecting when user starts swipping to the left then FragmentTransaction begins to replace my placeholder fragment with actuall CameraFragment.
Transaction isn't quite somooth like for example in WhatsApp (If someone knows how they done that and wants to share, that would be great) but it's working without issues and no flashing over home fragment.
Of course if someone have and idea how to improve solution or some another better solution feel free to share it.

2 Dimensional List View

I've got a question regarding list views in Android. I want to create an activity that can display lots of information. I want to display each player horizontally (there could be anywhere from 2 to 256 players, but most likely there will be like 8) and there will be 10 - 20 rows under each player.
Is there a way to create a ListView that can scroll both directions? If not, what is the correct design approach to handle this type of problem?
I've tried to look into this a bit, and a GridView won't work because there will be items hanging off the screen; they won't all fit in the grid.
Thanks!
-Justin
Use a ViewPager instead so that the user can scroll through the player in the horizontal way that you want.
This way each player will be in a fragment, and the fragment can contain a vertical list that will show the rows that you require.
The benefit of this is that the viewpager can handle a huge number of players and rows for each player without impacting memory performance and in a less cramped space.
You can try using an ExpandableListView or a ViewPager.

Keep d-pad focus changes within current fragment

I have an android app that primary receives input using a d-pad (up, down, left, right, OK, back)
The app runs on a TV so uses fragments to separate sections.
I would like to keep directional buttons from focusing views in other fragments. And handle directional buttons differently depending on what fragment is set as the "current"
What would be the most elegant solution to this?
also, Fragments are dynamically changed at runtime.
When the d-pad is the only means of navigation for a user, such as on some Google TV's, then it's not a good idea to stop the navigation at the edge of a fragment. How I solved this, is by allowing cross-border navigation and laying out elements so that the navigation is not awkward and works by finding the path automatically (after all, there does not seem to be a way to statically provide the next element to focus on in each direction, when in another fragment).
Like you I'm looking for reference advice on how to deal with fragments and navigation on Google TV.

Categories