How to add a fragment on tap of grid view items? - android

I am having tabs under which they have there own fragment.There is one tab whose fragment is having gridview items.I want that on tap of each item a new fragment should be open.Tab must be remain but fragment should be different.
Is this possible?...
Suggest me some possible way to do it..
Thanks..

Yeas it is possible. First, you have to use GridView's setOnItemClickListener(), to set a listener gridviews click.
Then you can detect wich element has been clicked and make the transition. How do you implemented the tabs stuff? To make the transition you need the fragment manager and replace the fragment.
Fragment fragment = new Fragment();
getFragmentManager().beginTransaction().replace(R.id.content_frame,fragment).commit();

Related

How to swipe into a Fragment as big as the screen from a View not as big?

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 ?

How to stop the currently selected Fragment from reloading in a Navigation Drawer

I have a Navigation drawer with 4 items each of which load 4 different fragments when clicked. All fragments contain a listview that displays data fetched from the web using an AsyncTask. My problem is this: Let's say I click on the second item and that fragment loads just fine. But when I click the same item again, the fragment also loads again. Is there a way I can stop the currently selected fragment from loading again? Maybe there's some way I can disable the currently selected item from being clicked. I'm sure there must be a better way. Thanks.
You're probably adding your Fragment to a FrameLayout or any other kind of container. Before starting the transaction and replacing the Fragment in the container you can check if the Fragment you're trying to add is present already via the following code:
private void openFragment(Fragment newFragment){
Fragment containerFragment = getFragmentManager().findFragmentById(R.id.container);
if (containerFragment.getClass().getName().equalsIgnoreCase(newFragment.getClass().getName()))
return;
else
// Start transaction and replace fragment
}

How to replace Fragment inside ViewPager, using PagerAdapter?

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 )

How to Start Any Fragment at the bigining by using ViewPager

Hey i have 5 Fragments like Fragment1,Fragment2,Fragment3,..Which i am Displaying in a ViewPager But I want Fragment2 should display first instead of Fragment1when i am starting my app.
After setting the adapter for ViewPager, you can simply use setCurrentItem(position) to set the page selected at required position.
mViewPager.setCurrentItem(position);

Android: Master/Detail Flow on tablet

I've created application using master detail flow. I have custom ListView and FrameLayout as container to show detail of selected items from ListView. Everythig's fine but my detail is shown after onClick on ListView item. I would like to show detail of first item in ListView automatically after activity is created (sth. like android Settings activity). How should I do that? Thank you for response and your help.
Put your Fragment directly on onCreate() on the FrameLayout.
FragmentActivity frag = new FragmentActivity();
getSupportFragmentManager().beginTransaction().add(frag, details).commit();
and than always replace() via FragmentManager...

Categories

Resources