add Viewpager dynamically in a framelayout - android

Is it possible to replace a fragment in a FrameLayout with the .replace() to use a ViewPager? since both are ViewGroups.
For example, in xml, I have only
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id ="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
and I would like to replace the FrameLayout with id content that is holding a fragment to a ViewPager of fragments in runtime. Is it possible??

i would like to replace the framelayout with id content that is
holding a fragment to a viewpager of fragments in runtime. is it
possible??
You can't replace a Fragment with a simple ViewGroup like a ViewPager, but thanks to the new changes in 4.2 you could put the ViewPager itself in a Fragment and use nested Fragments as the children of the ViewPager. Then you could replace the initial Fragment with the Fragment that holds the ViewPager.
See this guide, nested Fragments are also available to lower APIs through the compatibility package. See this question for an implementation example.

Related

How to add RecyclerView in extended fragment to display the list

I have extended 3 fragment in main activity..also how do i add recyclerview to display a list in one of my fragment.
Please help
Never mind you are begineer :) . Look first of all look how the fragment works.Fragment requires a activity . In activity you have to create framelayout and load or commit fragment in framelayout(Activity).
Now if you want to change any view in the activity you have to change it in fragment and call respective fragment in activity of change its view.
This means you have to add recyclerview in the activity but first off all you must add that fragment containing recyclerview to framelayout of the activity.
You are looking for something like this:
Note: Make sure you add this to your app gradle:
compile 'com.android.support:recyclerview-v7:23.4.0'
You have fragment1.java with layout fragment1.xml:
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/fragment1_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Initialize this RecyclerView inside fragment1.java and write is adapter.
For more on this, try these links:
How to implement RecyclerView with CardView rows in a Fragment with TabLayout
http://www.androidhive.info/2016/01/android-working-with-recycler-view/

how to set position of fragment in center in RelativeLayout

hi guys I was playing aroung with fragments in android.
I read that there are two ways to bind a fragment in an activity.
1)using <fragment> tag in layout(XML) file.
2)using FragmentManager and FragmentTransaction .
I want my Activity to handle fragment dynamically so i went for the 2nd approach.
But when a fragment is added it is added to left-top corner in RelativeLayout Activity(which appears to be the default behaviour of RelativeLayout)
I want my fragment to appear at center of the Activity Layout.
I just can't figure it out how to give position to fragmet in JAVA code.
I looked into stackoverflow also,
which uses --RelativeLayout.LayoutParams
which appears to work with View objects(Button,TextView etc).
Any suggestions.
You can set a placeholder FrameLayout inside RelativeLayout in your XML and align it with android:layout_centerInParent="true". Then replace this placeholder with your fragment in the code FragmentTransaction#replace(int containerViewId, Fragment fragment, String tag)
In your Activity XML layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#id/fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
In your Activity class:
getFragmentManager().beginTransaction()
.replace(R.id.fragment, new MyFragment())
.commit();
In your Activity's layout use the attribute android:layout_gravity="center" for RelativeLayout in which Fragment resides. This will make all it's child element appear in center.

Replace ViewPager in other fragment

I have a viewpager in my main activity, and viewpager contains a few pages (exactly a few fragments). I want to go from my viewpager to another fragment which isn't in viewpager. How can I do this?
Here is my activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.slpd.Activities.SLPDActivity">
<com.slpd.SlpdViewPager
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Why do you want to go to another fragment?
If there is one or more fragments you want to transition to, simply add a new activity which looks like your SLDPActivity and inflates a layout with another ViewPager.
If it is only one fragment you want to transition to, why not simply making it a new activity in the first place?

Android fragments overlap previous view and button listeners

I have an activity A with a fragment A inside.
Activity A uses layout X, and fragment A uses layout A.
code of layout X:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="#+id/fragment1"
android:name="android.app.DialogFragment"
android:layout_width="wrap_content"
android:layout_height="500dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="113dp"
class="com.example.fragtester.FragA" />
</RelativeLayout>
Layout A is just textview + linearlayout.
I set up another fragment B that uses layout B.
Now that I use the following code in activity A to change the fragments:
Fragment f = new FragB();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment1, f);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
I end up having layout B displaying under layout A.
So I use a FrameLayout to wrap the fragment in layout X and use
ft.replace(R.id.FrameLayout1, f);
Now the view is working nicely. Though, another problem arises.
Although layout B covers layout A, but the buttons are still active.
That means when I am viewing layout B, I can still click buttons on layout A, even if I am not seeing it.
And even when I add fragment C/D/E..... (layouts C/D/E....), the buttons on layout A is still active.
Can anybody explain why is that? Am I using fragments wrongly? Thanks!
A way to get through is to make layout A blank, and use other layout to cover it. But it doesn't seems to be the "right" way??
Remove the fragment and add a FrameLayout
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
</FrameLayout>
then add fragments programmatically.
In android fragment button click pass through the fragments (i dont know if the fragments are suppose to work like that). what I used to do in such a situation is to make the layout of the fragment clickable. so the clicks wont pass through.
Instead of having fragment in your xml, try to create empty container for a fragments. For example empty frame layout. And then programmatically put your fragments in there.
Add the following attribute to the XML root layout of the fragment that goes on top.
android:clickable="true"
This will ensure that touch events will not propagate further than the top layer.

How to embed view of Fragment in the header of a ListFragment

I have a FragmentActivity where I'm dynamically adding Fragments in my onCreate. I want a Fragment above a ListFragment. Of course, the first thing I tried was the fragments in one LinearLayout in one ScrollView, but then the ListFragment would only show one row at a time.
I found Scrolling 2 fragment (one Fragment and one ListFragment) as one, but I don't know how to get the parent view of the Fragment to add to the header view of the ListFragment. I obviously get an NPE when call fragment.getView() and fragment.getView().getParent() because I'm still in the onCreate of my activity, so the views haven't been initialized yet.
How can I put a Fragment above a ListFragment?
Create a layout that contains your first fragment, like so:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/content"
android:name="your.fragment.YourFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
And then, in your ListFragment, add this layout as a header, like so:
View header = inflater.inflate(R.layout.widget_user_fragment, null, false);
getListView().addHeaderView(header, null, true);

Categories

Resources