Fragment is blank in ViewPager - android

First off: I am aware there are a number of similar questions, however, none of the suggestions worked.
I have a ViewPager that I would like to use with fragments. The ViewPager works, and I can see in the log that I can scroll left and right. I can see the animation when I try to scroll left and there's no previous item (or right when there's no following item). However, the screen remains blank. The fragment itself is functional, however, I've tried it in a different context.
I am using the androidx namespaced packages.
Things I've tried (<--> means I've tried both):
FragmentPagerAdapter <--> FragmentStatePagerAdapter
getFragmentManager() <--> getChildFragmentManager()as the FragmentManager for the adapter
setSaveFromParentEnabled to false.
setOffscreenPageLimit to the number of fragments.
Relevant code (I have taken out sections of code that are irrelevant to this question):
The Fragment that contains the ViewPager:
public class AddListingMediaFragment extends Fragment {
private ViewPager viewPagerTop;
private AddMediaAdapter addMediaAdapter;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_listing_media, container, false);
setupViewPagerTop(view);
return view;
}
private void setupViewPagerTop(View view) {
viewPagerTop = view.findViewById(R.id.view_pager_top);
addMediaAdapter = new AddMediaAdapter(getChildFragmentManager());
viewPagerTop.setAdapter(addMediaAdapter);
viewPagerTop.setSaveFromParentEnabled(false);
viewPagerTop.setOffscreenPageLimit(5);
}
}
The Adapter (please do note, as mentioned, that I've tried both FragmentPagerAdapter and FragmentStatePagerAdapter:
public class AddMediaAdapter extends FragmentPagerAdapter {
private static final String TAG = AddMediaAdapter.class.getSimpleName();
public AddMediaAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 5;
}
#Override
public Fragment getItem(int position) {
Log.i(TAG, "getItem: " + String.valueOf(position));
return new AddListingMediaCameraFragment();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
The layout file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager_top"
android:layout_width="300dp"
android:layout_height="300dp"
/>
<include
layout="#layout/bottom_sheet_add_listing_media_gallery"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

You do not need to override isViewFromObject in your adapter, FragmentPagerAdapter has already taken care of it. ViewPager calls this method to decide how to render the pages.
Your view == object will return false because the object is always a Fragment not a View. So just drop the override, and it will fix the issue.

Related

Swipe between fragment with ViewPager and without TabLayout

I have 3 fragments (totally different from each other) and one activity (MainActivity). What I would like to do is to be able to swipe between them (with finger, not with buttons) with a transition like a TabLayout.
According to what I saw, I can do it using ViewPager. But the problem is that ViewPager uses TabLayout.
There is a way to swipe betweens fragments, using Viewpager, without TabLayout ?
This code will help you
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Set up the child Fragments you would like to display. I made 3 child Fragments, calling them ChildFragment1, ChildFragment2, and ChildFragment3. Remember to have them extend support.v4.app.fragment. Now make layouts for all three of the Fragments. I call them child_fragment_1_layout, child_fragment_2_layout, and child_fragment_3_layout.
public class ChildFragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.child_fragment_1_layout, container, false);
Button buttonInFragment1 = rootView.findViewById(R.id.button_1);
buttonInFragment1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(), "button in fragment 1", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
Make an adapter for the ViewPager. You will have to extend either theFragmentPagerAdapter or the FragmentStatePagerAdapter. For this tutorial we will use the FragmentPagerAdapter . The difference between the two can be found here. After extending the FragmentPagerAdapter, you will need to call super(FragmentManager) in your constructor and implement the methods getItem(position) and getCount(). The getItem(position)method is used to return the fragment at the corresponding position, ordered from left to right. So ChildFragment1 would be at position 0, ChildFragment2 would be at position 1, and ChildFragment3 would be at position 2. The getCount() method is to count how many Fragments there are to display, and in this case, there are 3.
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return new ChildFragment1(); //ChildFragment1 at position 0
case 1:
return new ChildFragment2(); //ChildFragment2 at position 1
case 2:
return new ChildFragment3(); //ChildFragment3 at position 2
}
return null; //does not happen
}
#Override
public int getCount() {
return 3; //three fragments
}
}
Now find your ViewPager in MainActivity and call the setAdapter() method and pass in your custom adapter. If you are doing this in another Fragment (Nested Fragments), you will have to pass in getChildFragmentManager() in the argument of your adapter instead. Now your ViewPager is all set
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
}
}

How can I populate an adapter in a fragment which is within a viewPager within a dialogFragment?

I'm trying to create the following scenario:
User clicks a Button > this shows a DialogFragment
The DialogFragment contains a ViewPager, which has 2 tabs - one shows a colour picker fragment and the other a symbol picker fragment
Each picker fragment contains an adapter which transforms an array of colours/symbols into a nice grid.
However, it's currently showing up blank - the DialogFragment appears, with the other parts of the fragment, but no adapter view.
Here's what I have:
The initial activity is called EditActivity. It has a Button, which, when clicked, calls this function to reveal the DialogFragmentPickers:
public void showPickers() {
if(mDialogFragmentPickers == null) return;
mDialogFragmentPickers.setColourSet(mColourSet);
mDialogFragmentPickers.show(mFragmentManager, "Pickers");
}
DialogFragmentPickers is initialised in the onCreate method of EditActivity like this:
mDialogFragmentPickers = new DialogFragmentPickers();
The ColourSet being passed in is basically just an array of the colours that are to be shown in the picker. DialogFragmentPickers should then pass it on through to the FragmentColourPicker when it's initialised.
The DialogFragmentPickers class looks like this (I've only shown the parts pertaining to the ColourPicker, but the SymbolPicker works in the same way):
public class DialogFragmentPickers extends DialogFragment {
private PickersPagerAdapter mPagerAdapter;
private ViewPager mViewPager;
private ColourSet mColourSet;
private FragmentColourPicker mColourPicker;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
// Inflate layout
View view = inflater.inflate(R.layout.dialog_fragment_pickers, container, false);
mPagerAdapter = new PickersPagerAdapter(getChildFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = view.findViewById(R.id.pager);
mViewPager.setAdapter(mPagerAdapter);
return view;
}
public void setColourSet(ColourSet colourSet) {
mColourSet = colourSet;
if(mColourPicker == null) getColourPickerFragment();
else mColourPicker.setColourSet(mColourSet);
}
private void getColourPickerFragment() {
if (mPagerAdapter == null) return;
mColourPicker = (FragmentColourPicker) mPagerAdapter.getItem(0);
if(mColourSet != null) mColourPicker.setColourSet(mColourSet);
}
public class PickersPagerAdapter extends FragmentPagerAdapter
{
PickersPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new FragmentColourPicker();
case 1:
return new FragmentSymbolPicker();
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getResources().getString(R.string.colours);
case 1:
return getResources().getString(R.string.symbols);
}
return null;
}
}
The layout file for this contains:
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Current + adjacent page titles...-->
<android.support.v4.view.PagerTitleStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
The first Fragment used in this pager is FragmentColourPicker. It is defined like this:
public class FragmentColourPicker extends Fragment {
GridView mColourGrid;
Context mContext;
ColourSet mColourSet;
ChartColourAdapter mColourAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_colour_picker, container, false);
mColourGrid = view.findViewById(R.id.gridview_colourPicker);
initialiseWithColourSet(); // Returns without doing anything if colourSet is not set up yet
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContext = getActivity();
}
#Override
public void onDestroy(){ super.onDestroy();}
public void setColourSet(ColourSet thisColourSet) {
mColourSet = thisColourSet;
initialiseWithColourSet(); // Returns without doing anything if the colourGrid is not set up yet
}
public void initialiseWithColourSet() {
// Make sure that the colourSet and the colourGrid have both been initialised
if(mColourSet == null) return;
if(mColourGrid == null) return;
// Now set up ChartColourAdapter to display colours in grid
// Initialise currently highlighted colour to default
// Set on click listener for each colour
mColourAdapter = new ChartColourAdapter(mContext,
R.layout.adapter_pattern_colour_cell_layout,
mColourSet.getColours());
mColourGrid.setAdapter(mColourAdapter);
}
}
and the layout file for FragmentColourPicker (fragment_colour_picker.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/colour_picker_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/fui_transparent"
android:orientation="vertical">
<GridView
android:id="#+id/gridview_colourPicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBg"
android:paddingBottom="#dimen/activity_standard_margin"
android:columnWidth="#dimen/grid_cell_column_width"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:padding="#dimen/activity_small_margin"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/prompt_swipe_for_symbols"
android:padding="#dimen/activity_small_margin"
android:background="#color/colorBg"
/>
</LinearLayout>
When I run all of this, the DialogFragment appears when the button is clicked, and shows the FragmentColourPicker. I can swipe to the FragmentSymbolPicker and back. However, the only thing shown in the ColourPicker is the TextView which prompts the user to swipe across to see the symbols. The GridView with the actual colours in it doesn't show at all.
From the debugger, I think the issue is that initialiseWithColourSet only ever seems to have either colourSet OR colourGrid set - never both. This makes it return without attempting to set up the colour adapter.
If initialiseWithColourSet is called from onCreateView, then colourGrid is set, but colourSet is not (since it hasn't been passed in yet). That's reasonable enough. However, when colourSet is passed in later (from the DialogFragment), colourGrid has gone back to being null, so again the function returns without doing anything.
I clearly have something in the wrong order, or am re-initialising the colourGrid / ColourPicker without realising it.
It looks like an order issue to me. To ensure you're getting your data to the Fragment at the right time, I would pass the data that the fragments need into the Adapter via the constructor. I would keep a reference of them there and then instantiate the fragments using a static newInstance(... requiredData) method. Then, you can be sure that when they are "created" by the adapter, they'll have the required information.
Something like this:
Your fragment class:
...
Object myRequiredData;
static FragmentColourPicker newInstance(... requiredData) {
FragmentColourPicker f = new FragmentColourPicker();
f.setRequiredData(requiredData);
return f;
}
public void setRequiredData(Object reqData){
this.myRequiredData = reqData;
}
...
Your adapter class:
public class PickersPagerAdapter extends FragmentPagerAdapter {
Object reqData0;
Object reqData1;
PickersPagerAdapter(FragmentManager fm, Object reqData0, Object reqData1) {
super(fm);
this.reqData0 = reqData0;
this.reqData1 = reqData1;
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return FragmentColourPicker.newInstance(reqData0);
case 1:
return FragmentSymbolPicker.newInstance(reqData1);
default:
return null;
}
}
...
}

The Horizontal ViewPager Wont work the same time as my VerticalView Pager

I basically have everything set up already and I don't why it's not working. The only problem I have is that the Vertical won't work when I set it up together. Look at the code down below.
My Main Acitvity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//HorizontalViewPagerAdapter
View background = findViewById(R.id.am_background_view);
ViewPager viewPager = findViewById(R.id.am_view_pager);
HorizontalViewPagerAdapter adapter = new HorizontalViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(1);
}
My Controller Fragment
public class ControllerFragment extends Fragment {
public static ControllerFragment create() {
return new ControllerFragment();
}
VerticalViewPager verticalViewPager;
VerticalViewPagerAdapter verticalPageAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.vertical, null);
verticalViewPager = view.findViewById(R.id.am_scrollView);
verticalPageAdapter = new VerticalViewPagerAdapter(getChildFragmentManager());
verticalViewPager.setAdapter(verticalPageAdapter);
return inflater.inflate(R.layout.vertical, null);
}
public class VerticalViewPagerAdapter extends FragmentPagerAdapter {
public VerticalViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return SettingsFragment.create();
case 1:
return EmptyFragment.create();
case 2:
return ExtrasFragment.create();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
}
This is my HorizontalAdapter
public class HorizontalViewPagerAdapter extends FragmentPagerAdapter {
public HorizontalViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return ChatFragment.create();
case 1:
return ControllerFragment.create();
case 2:
return StoryFragment.create();
}
return null;
}
#Override
public int getCount() {
return 3;
}
Its like the whole code is completely ignoring the VerticalView Pager because it doesnt show at all in my app and its completely the same as a normal ViewPager
Having understood what you want to achieve, here's how you'd achieve it!
There are 6 fragments in total.
Home
Left
Right
Top
Botton
Controller
Update
The previous method overrides and prevents horizontal gestures from being captured. I've searched a bit and found an alternative.
First of all, you need a different vertical pager library. I tried the one here and it works perfectly.
So what you wanna do is, get that java file on your project and change your controller's XML like this
<?xml version="1.0" encoding="utf-8"?>
<c.kristofer.jax2.VerticalPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/am_scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/top"
class="c.kristofer.jax2.Fragments.SettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/home"
class="c.kristofer.jax2.Fragments.HomeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/bottom"
class="c.kristofer.jax2.Fragments.SettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</c.kristofer.jax2.VerticalPager>
This ViewPager does not take an adapter, rather it takes <fragment> tags for its child views. The Controller's java code will also change a bit
VerticalPager verticalViewPager = view.findViewById(R.id.am_scrollView);
verticalViewPager.snapToPage(1);
The result can be seen here. You can play around with the code and see what suits you best.
Previous Method
The activity will have one of the ViewPagers, either the Horizontal one or the Vertical one. The middle fragment of that ViewPager will be the Controller. Controller will be responsible for the second ViewPager. The middle fragment of this ViewPager will be Home.
This way, you will have 2 degrees of freedom from the Activity, and 2 degrees of freedom from the Controller.

How to add swipe between navigation tabs in android?

I got 3 navigation tabs with 3 different fragments in them.
How to add swipe between the fragments?
Android made something for this exact reason called ViewPager!
in your onCreate method, do something like this:
pager = (ViewPager) findViewById(R.id.pager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
then,
Include the following into your MainActivity:
public class MyPagerAdapter extends FragmentStatePagerAdapter {
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
private final String[] TITLES = getResources().getStringArray(R.array.tabs);
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
#Override
public int getCount() {
return TITLES.length;
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0: return OneFragment.newInstance(position);
case 1: return TwoFragment.newInstance(position);
case 2: return ThreeFragment.newInstance(position);
}
return null;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
The code I posted is code I use. It is more sophisticated than usual. It keeps track of the Fragments (each tab), allowing you to access Fragments from one another. So if you were in OneFragment, and wanted access to TwoFragment, you'd do something like:
in OneFragment
private void getTwoFragmentData() {
data = (MainActivity) getActivity.getTwoFragmentData();
}
in MainActivity
public Data getTwoFragmentData() {
TwoFragment twoFragment = (TwoFragment) adapter.getRegisteredFragment(1);
return twoFragment.getData();
}
Data above is just a placeholder for however you would interface with it, but the above code is the general structure of how you'd do it.
Also, you would edit the getItem method. And make the switch, case accordingly.
As an extra, you could do pager.setOffScreenPageLimit(x), to also load the content x tabs away from your current tab, which is useful for retaining information. For 3 tabs, I would use x = 2, but ultimately, be wary because that is extra memory used.
Finally, the last little extra is that I use a library called com.astuetz.PagerSlidingTabStrip which is awesome for getting the bar underneath the tabs to swipe with you and make it look really clean. Adding it is very simple and you'll find more details here
My activity_main.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- <com.astuetz.PagerSlidingTabStrip -->
<!-- android:id="#+id/tabs" -->
<!-- android:layout_width="match_parent" -->
<!-- android:layout_height="48dip" /> -->
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<!-- android:layout_below="#+id/tabs" -->
tools:context=".MainActivity" />
</RelativeLayout>
Ignore the commented out stuff. That is what I use in addition to get the library I stated above integrated.

Android: Using ViewPager with multiple dynamic Fragments

I am using a ViewPager to show 9 fragments. In each of these fragments, I want to just show a different picture. I want to use one single fragment layout, but dynamically add in the picture. Also, would like add a "Continue" button on the last fragment that when pressed will go to another activity.
How do I go about making a fragment layout dynamic?
Main Activity
public class StoryboardPageActivity extends FragmentActivity {
// The number of pages (wizard steps) to show in this demo.
private static final int NUM_PAGES = 9;
// The pager widget, which handles animation and allows swiping horizontally to access previous and next wizard steps.
private ViewPager mPager;
// The pager adapter, which provides the pages to the view pager widget.
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_storyboard_page);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.storyboardPager);
mPagerAdapter = new StoryboardPagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
// A simple pager adapter that represents 5 fragment objects, in sequence.
private class StoryboardPagerAdapter extends FragmentStatePagerAdapter {
public StoryboardPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return StoryboardFragment.newInstance(position);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
Fragment
public class StoryboardFragment extends Fragment {
private static final String KEY_POSITION = "position";
static StoryboardFragment newInstance(int position) {
StoryboardFragment frag = new StoryboardFragment();
Bundle args = new Bundle();
args.putInt(KEY_POSITION, position);
frag.setArguments(args);
return(frag);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_storyboard_page, container, false);
ImageView image = (ImageView)rootView.findViewById(R.id.imgStoryboard);
int position = getArguments().getInt(KEY_POSITION, -1);
int[] images = {R.drawable.storyboard1, R.drawable.storyboard2, R.drawable.storyboard3,
R.drawable.storyboard4, R.drawable.storyboard5, R.drawable.storyboard6,
R.drawable.storyboard7, R.drawable.storyboard8, R.drawable.storyboard9};
image.setImageResource(images[position]);
return rootView;
}
}
Fragment XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<ImageView
android:id="#+id/imgStoryboard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/storyboardSlide" />
</RelativeLayout>
How do I go about making a fragment layout dynamic?
The same way you make any other "layout dynamic". If you want to put an image in an ImageView, call setImageBitmap() or setImageDrawable() or whatever. For example, the PagerAdapter could supply the position to the fragment (via a factory method), and the fragment could then know what image to load.
This sample project demonstrates populating the hint of an EditText with a custom value based upon the page's position.
With respect to the "Continue" button, either have a separate fragment class for that (and appropriate smarts in your PagerAdapter, or always have the button in your layout, but set to android:visibility="gone" by default, toggling it via setVisibility(View.VISIBLE) for the fragment that needs it.

Categories

Resources