Skip to content

Commit

Permalink
Merge pull request janheinrichmerker#211 from meierjan/feature/multi-…
Browse files Browse the repository at this point in the history
…parallaxable-layouts

Parallax*Fragment; support multiple parallax-layouts
  • Loading branch information
janheinrichmerker committed Jul 10, 2017
2 parents ede071e + 4c4b266 commit f227a0b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,23 @@
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;

import java.util.LinkedList;
import java.util.Queue;
import com.heinrichreimersoftware.materialintro.view.parallax.util.ParallaxUtil;

import java.util.ArrayList;
import java.util.List;

public class ParallaxFragment extends Fragment implements Parallaxable {
@Nullable
private Parallaxable parallaxLayout;

private final List<Parallaxable> parallaxableChildren = new ArrayList<>();

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
parallaxLayout = findParallaxLayout(view);
}

public Parallaxable findParallaxLayout(View root) {
Queue<View> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
View child = queue.remove();
if (child instanceof Parallaxable) {
return (Parallaxable) child;
} else if (child instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) child;
for (int i = viewGroup.getChildCount() - 1; i >= 0; i--) {
queue.add(viewGroup.getChildAt(i));
}
}
}
return null;
parallaxableChildren.addAll(ParallaxUtil.findParallaxableChildren(view));
}

@Override
public void setOffset(@FloatRange(from = -1.0, to = 1.0) float offset) {
if (parallaxLayout != null)
parallaxLayout.setOffset(offset);
ParallaxUtil.setOffsetToParallaxableList(parallaxableChildren, offset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,26 @@
import android.support.annotation.FloatRange;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;

import com.heinrichreimersoftware.materialintro.app.SlideFragment;
import com.heinrichreimersoftware.materialintro.view.parallax.util.ParallaxUtil;

import java.util.ArrayList;
import java.util.List;

import java.util.LinkedList;
import java.util.Queue;

public class ParallaxSlideFragment extends SlideFragment implements Parallaxable {
@Nullable
private Parallaxable parallaxLayout;

private final List<Parallaxable> parallaxableChildren = new ArrayList<>();

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
parallaxLayout = findParallaxLayout(view);
parallaxableChildren.addAll(ParallaxUtil.findParallaxableChildren(view));
}

public Parallaxable findParallaxLayout(View root) {
Queue<View> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
View child = queue.remove();
if (child instanceof Parallaxable) {
return (Parallaxable) child;
} else if (child instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) child;
for (int i = viewGroup.getChildCount() - 1; i >= 0; i--) {
queue.add(viewGroup.getChildAt(i));
}
}
}
return null;
}

@Override
public void setOffset(@FloatRange(from = -1.0, to = 1.0) float offset) {
if (parallaxLayout != null)
parallaxLayout.setOffset(offset);
ParallaxUtil.setOffsetToParallaxableList(parallaxableChildren, offset);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.heinrichreimersoftware.materialintro.view.parallax.util;

import android.support.annotation.FloatRange;
import android.view.View;
import android.view.ViewGroup;

import com.heinrichreimersoftware.materialintro.view.parallax.Parallaxable;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class ParallaxUtil {
private ParallaxUtil() {}

public static List<Parallaxable> findParallaxableChildren(View root) {
List<Parallaxable> parallaxableChildrenFound = new LinkedList<>();
Queue<View> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
View child = queue.remove();
if (child instanceof Parallaxable) {
parallaxableChildrenFound.add((Parallaxable) child);
} else if (child instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) child;
for (int i = viewGroup.getChildCount() - 1; i >= 0; i--) {
queue.add(viewGroup.getChildAt(i));
}
}
}
return parallaxableChildrenFound;
}

/**
* Set the provided offset to a list of parallaxable items.
*
* @param parallaxableChildren The list of parallaxable items to set the offset to.
* @param offset The offset to assign.
*/
public static void setOffsetToParallaxableList(
final List<Parallaxable> parallaxableChildren,
@FloatRange(from = -1.0, to = 1.0) float offset
) {
if (!parallaxableChildren.isEmpty()) {
for (Parallaxable parallaxable : parallaxableChildren) {
parallaxable.setOffset(offset);
}
}
}

}

0 comments on commit f227a0b

Please sign in to comment.