Skip to content

Commit

Permalink
Parallax*Fragment; support multiple parallax-layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
meierjan committed Jun 1, 2017
1 parent 98a2068 commit 45565d1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,45 @@
import android.view.View;
import android.view.ViewGroup;

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

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

private List<Parallaxable> parallaxableChildren = Collections.emptyList();

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

public Parallaxable findParallaxLayout(View root) {
public List<Parallaxable> findParallaxLayouts(View root) {
List<Parallaxable> parallaxableChildren = new ArrayList<>();
Queue<View> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
View child = queue.remove();
if (child instanceof Parallaxable) {
return (Parallaxable) child;
parallaxableChildren.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 null;
return parallaxableChildren;
}

@Override
public void setOffset(@FloatRange(from = -1.0, to = 1.0) float offset) {
if (parallaxLayout != null)
parallaxLayout.setOffset(offset);
if (!parallaxableChildren.isEmpty()) {
for (Parallaxable parallaxable : parallaxableChildren) {
parallaxable.setOffset(offset);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,52 @@

import android.os.Bundle;
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;

import com.heinrichreimersoftware.materialintro.app.SlideFragment;

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

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

private List<Parallaxable> parallaxableChildren = Collections.emptyList();

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

public Parallaxable findParallaxLayout(View root) {
public List<Parallaxable> findParallaxLayouts(View root) {
List<Parallaxable> parallaxableChildren = new ArrayList<>();
Queue<View> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
View child = queue.remove();
if (child instanceof Parallaxable) {
return (Parallaxable) child;
parallaxableChildren.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 null;
return parallaxableChildren;
}

@Override
public void setOffset(@FloatRange(from = -1.0, to = 1.0) float offset) {
if (parallaxLayout != null)
parallaxLayout.setOffset(offset);
if (!parallaxableChildren.isEmpty()) {
for (Parallaxable parallaxable : parallaxableChildren) {
parallaxable.setOffset(offset);
}
}
}
}

0 comments on commit 45565d1

Please sign in to comment.