I have the following . I have a fragment on the left which is grid view of questions Q1,Q2 etc., If user clicks on Q1 on the right there is a fragment which shows the question details with 4 options. Now I want to implement view pager for the right hand side fragment.
My main looks like -
Fragment 1
Fragment 2
For the grid i have an activity which instantiates the question list and I have another fragment to show the questions.
Please can someone tell me how I can add a view pager only for the fragment on the right whilst using the existing logic .
You can add ViewPager to the layout of your Fragment2, either programmatically or using xml layout file.
If you wan't it to navigate through questions in Fragment1 and Fragment1 to react to it (by highlighting items) - you can broadcast an intent every time a page is flipped and handle it in Fragment1.
I'm not sure I understand what kind of problem you're having, can you provide more details?
Related
I am trying to create a main page in my android app in which there are multiple things a user can select to do. For this page, I am thinking of using a RecyclerView with a LinearLayoutManager. For each of the views in the RecyclerView (none of which are as big as the screen), I want a user to be able to swipe into a Fragment (which is as big as the screen).
I have already tried using multiple ViewPagers to solve this problem. When keeping the ViewPager as big as the screen (the default), the Fragment would be the correct size, except the view in the RecyclerView would be as big as the screen (I want it to only wrap_content). When shrinking the size of the ViewPager, the Fragment would not be as big as the screen, but the View in the RecyclerView would be the correct size.
I have heard that one can listen for a user's swipe on a view, except:
- I don't know if that is what I should do in my case, and
- I don't know how to do it for my situation
If listening for a user's swipe is the correct way to go, please give some direction and/or code on how to complete what I want to do. If it is not the way to go, please provide an alternate way.
Thanks in advance!
Update: Here is a picture describing what I mean:
// define your fragment with recycle view
class FragmentA extends Fragment {
// with recycler view
// in click on recycler item method as i posted
// 1) do check if pager fragment exist in pager adapter
// 2) if not create one
// 3) and move to it using pager method
}
// define second without
class FragmentB extends Fragment {
// without
}
// create pager
ViewPager vp = new ViewPager(Context);
// create list with fragments for pager adapter
// when you will swipe you will move from A to B
List<Fragment> // add FragmentA + FragmentB
// https://developer.android.com/reference/android/support/v4/view/PagerAdapter.html
// create pager adapter - implement own or user FragmentPagerAdapter
PagerAdapter ... = new MyPagerAdapter(List<Fragment>);
// set view pager with adapter
viewPager.setAdapter(PagerAdapter);
// add view pager to activity view - or some content view
I already tried this on my own; the viewpager would not allow for the
correct sizes. Either the full-screen fragment size was shrunk down to
the size of the View in the recyclerview, or the view in the
recyclerview was full-screen. – Flare Cat
its working:
Oh wait, read the code wrong. But still, I need to base FragmentB off
of what was swiped in FragmentA – Flare Cat
solution:
when you click on recycler item dynamic create new fragmentB and call move to fragmentB on pager (before create new one do a check if fragment not exist already!)
or if you don't use any click on item method but just constant recycler item content create such fragments corresponding to each entry in recycler
or create some sort of map which will map each entry in recycler to to its corresponding fragment content
As I understand it, I would need to create an onClickListener for each
view in the RecyclerView. But, would that onClickListener be called
when a user swipes the corresponding view? – Flare Cat
Actually, this might work with an onTouchListener. I'll have to try it out. – Flare Cat
once again:
it depends what u want to achieve - if recycler is a way of brief for each fragments and you know the content and want to present each entry for recycler item counting from top to bottom then you need add those fragments one by one in same order from left to right
#FlareCat but why using pager here ? not better to open new activity or add fragment with transition on recycler item click ?
First , Sorry for my vague title ,
I really don't know how to describe the problem I met.
I will try to draw a picture to explain.
In the following picture , there is a ViewPager with a TabLayout pointing to the first page.
so it works like this :
And the problem is , How to switch the current page to another page which is not included in the ViewPager , with keeping the Tablayout at the bottom and pointing to the current tab. Like this:
In the other words ,
Is it possible to group several pages(fragments) to a tab?
I think you should create custom FragmentPageAdapter for your ViewPager. That allows you replacing Tab1's base fragment (Current Page on your last picture) with another fragment (Another Page on your last picture).
You can do it e.g. after click on button on base fragment.
If you wont to go back to base fragment after click on Tab 1 you have implement OnTabSelectedListener having regard to setup with view pager.
I don't attach any code because I don't know how your code looks like.
I have in my app a drawer navigation, which contain few fragments to go to. when my app starts it opens by default the first fragment - which contains a view pager that has 2 fragments in it as well. in those 2 fragments I have a text view and a button as well.
the problem is that if I click on another fragment in the drawer, and then go back to the first fragment, the button and the text view are gone. in the first time this view pager fragment is creating the two other fragments. I think that when I go back to this fragment again by replacing it, it wont load the other two fragments and their text and button view. I'm going crazy because of this and I cant continue my work..what is wrong here?
I don't know how to upload code snippets..sorry
Thank you for your help :)
I have 2 viewpagers containing different fragments now I have to move to the fragment of one viewpager to fragment of another viewpager. But when I hit back the previous fragments data have to hold. The problem here is I need infinite loop navigation i.e., from fragment1 of Viewpager1 contains a textview when I click on that it has to go to details screen which is in fragment1 of viewpager2 and another textview which is in fragment1 of viewpager2 it has to go to fragment1 of viewpager1.
I have tried using stack and I can navigate but when I go to 3 or 4 levels and hit back I have lost the previous data so getting nullpointer exception.
Can anybody help me?
The way I accomplish similar setup is to use an array list to hold the data and pass it around from fragment to fragment using Intent.
Alternatively create a class on the host Activity with public access method so it can be get and set from the Fragment or better still implement a callback that updates the class on the Activity from the Fragment.
This way you can have a text view for firstname input in one fragment and each time and when a text is inputed in that text view you will update the Class.Firstname property in the Activity and the when you go to the details Fragment you can get the Class.Firstname and display it.
Thanks
My problem
I am using a ViewPager to display fragments inside a FragmentActivity. ViewPager gets fragments from the attached FragmentPagerAdapter.
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mAdapter = new HomePagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
Suppose ViewPager has 3 fragments to display: Fragment1, Fragment2, Fragment3. Fragment1 is a grid fragment and displays a grid. Fragment2 and Fragment3 have their own content to display.
When the use swipes on the screen, ViewPager will display the next fragment - Fragment2. And so on.
What I want?
What I want is that, when an item from the grid (displayed by Fragment1) is clicked, Fragment1 should be completely replaced with some other fragment, say Fragment4 (this is different fragment and is not returned by the attached adapter). User will work on Fragment4 and after ButtonBack click (just button iside Fragment4), Fragment1 with the grid should be displayed again. Meanwhile ViewPager should behave the same i.e on swipe, the next fragment (in our case Fragment2) will be displayed.
So I just want to get the same behavior as in example:
http://developer.android.com/training/basics/fragments/fragment-ui.html#Replace
My question
So, is this possible to achieve? If yes, then how to?
I would greatly appreciate for your help. Alex.
P.S. Sorry for my English:)
I've made a little example that shows how to achieve it:
https://github.com/danilao/fragments-viewpager-example
I think the point is to use another fragment as a container.
One way to achieve this is by adding another FragmentActivity that displays your Fragment4. Use Intent to start this activity and sent grid item position or other data in Extra.
On press of backbutton this activity will be finished and last activity with view pager will be displayed.
[EDIT] Old response. Better solutions are provided in other answers,
You will have to do the fragment transaction and add the existing fragment into backstack.
I created a sample project to show you how to do it.
The code is not fine tuned, I just created it to show you how to do it.
https://drive.google.com/folderview?id=0BxHClVwHSqq5dVRvT1Qyd0hYN0k&usp=sharing
But please be aware that even if you press back at any of the view pager screen, the previous fragment ends up showing as it is the same activity.
IF this is not expected behavior, then you should consider having multiple activities instead ( like tab layout )