Skip to content

Commit

Permalink
library updated with viewpager support
Browse files Browse the repository at this point in the history
  • Loading branch information
Krupen committed Jun 28, 2017
1 parent 0eae80e commit 7522a56
Show file tree
Hide file tree
Showing 13 changed files with 1,207 additions and 38 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ dependencies {
compile ('com.google.android:flexbox:0.2.7'){
exclude group: 'com.android.support'
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onClick(View v) {
MyFabFragment dialogFrag = MyFabFragment.newInstance();
dialogFrag.setParent_fab(fab);
dialogFrag.setParentFab(fab);
dialogFrag.show(getSupportFragmentManager(), dialogFrag.getTag());
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@
import java.util.List;
import java.util.Map;

import biz.laenger.android.vpbs.BottomSheetUtils;

import static android.R.attr.key;
import static com.allattentionhere.fabulousfiltersample.R.id.imgbtn_refresh;
import static com.allattentionhere.fabulousfiltersample.R.id.nsv;


/**
Expand All @@ -47,7 +44,7 @@ public class MyFabFragment extends AAH_FabulousFragment {
List<TextView> textviews = new ArrayList<>();

TabLayout tabs_types;
ViewPager vp_types;

ImageButton imgbtn_refresh,imgbtn_apply;
SectionsPagerAdapter mAdapter;
private DisplayMetrics metrics;
Expand Down Expand Up @@ -75,14 +72,19 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
}

@Override

public void setupDialog(Dialog dialog, int style) {


View contentView = View.inflate(getContext(), R.layout.filter_view, null);

RelativeLayout rl_content = (RelativeLayout) contentView.findViewById(R.id.rl_content);
LinearLayout ll_buttons = (LinearLayout) contentView.findViewById(R.id.ll_buttons);
imgbtn_refresh = (ImageButton) contentView.findViewById(R.id.imgbtn_refresh);
imgbtn_apply = (ImageButton) contentView.findViewById(R.id.imgbtn_apply);
vp_types = (ViewPager) contentView.findViewById(R.id.vp_types);
ViewPager vp_types = (ViewPager) contentView.findViewById(R.id.vp_types);
tabs_types = (TabLayout) contentView.findViewById(R.id.tabs_types);

imgbtn_apply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -107,13 +109,15 @@ public void onClick(View v) {
mAdapter.notifyDataSetChanged();
tabs_types.setupWithViewPager(vp_types);


//params to set
setAnimationDuration(600); //optional; default 500ms
setPeekHeight(300); // optional; default 400dp
setCallbacks((Callbacks) getActivity()); //optional; to get back result
setViewgroupStatic(ll_buttons); // optional; layout to stick at bottom on slide
setViewMain(rl_content); //necessary; main bottomsheet view
setMainContentView(contentView);// necessary; call at end before super
setViewPager(vp_types); //optional; if you use viewpager that has scrollview
setMainContentView(contentView); // necessary; call at end before super
super.setupDialog(dialog, style);
}

Expand Down
17 changes: 5 additions & 12 deletions app/src/main/res/layout/view_filters_sorters.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="MissingPrefix">

<android.support.v4.widget.NestedScrollView
<android.support.v4.widget.NestedScrollView 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:id="@+id/nsv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
>
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

<LinearLayout
android:id="@+id/ll_scroll"
Expand All @@ -21,12 +15,12 @@
android:orientation="vertical">

<com.google.android.flexbox.FlexboxLayout
android:paddingTop="8dp"
android:id="@+id/fbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:paddingTop="8dp"
app:alignContent="flex_start"
app:alignItems="flex_start"
app:flexDirection="row"
Expand All @@ -35,5 +29,4 @@
</LinearLayout>

</android.support.v4.widget.NestedScrollView>
</LinearLayout>

3 changes: 2 additions & 1 deletion fabulousfilter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ext {
siteUrl = 'https://github.com/Krupen/FabulousFilter'
gitUrl = 'https://github.com/Krupen/FabulousFilter.git'

libraryVersion = '0.0.1'
libraryVersion = '0.0.2'

developerId = 'krupen'
developerName = 'Krupen Ghetiya'
Expand Down Expand Up @@ -48,6 +48,7 @@ android {
dependencies {
compile 'com.android.support:appcompat-v7:25.3.0'
compile "com.android.support:design:25.3.0"

}

apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package android.support.v4.view;

import android.view.View;

public class ViewPagerUtils {

public static View getCurrentView(ViewPager viewPager) {
final int currentItem = viewPager.getCurrentItem();
for (int i = 0; i < viewPager.getChildCount(); i++) {
final View child = viewPager.getChildAt(i);
final ViewPager.LayoutParams layoutParams = (ViewPager.LayoutParams) child.getLayoutParams();
if (!layoutParams.isDecor && currentItem == layoutParams.position) {
return child;
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,36 @@
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.FrameLayout;

import com.allattentionhere.fabulousfilter.viewpagerbottomsheet.BottomSheetUtils;
import com.allattentionhere.fabulousfilter.viewpagerbottomsheet.ViewPagerBottomSheetBehavior;
import com.allattentionhere.fabulousfilter.viewpagerbottomsheet.ViewPagerBottomSheetDialog;
import com.allattentionhere.fabulousfilter.viewpagerbottomsheet.ViewPagerBottomSheetDialogFragment;


/**
* Created by krupenghetiya on 05/10/16.
*/

public class AAH_FabulousFragment extends BottomSheetDialogFragment {
public class AAH_FabulousFragment extends ViewPagerBottomSheetDialogFragment {

private FloatingActionButton parent_fab;
private DisplayMetrics metrics;
private int fab_size = 56, fab_pos_y, fab_pos_x;
private float scale_by = 12f;
private FrameLayout bottomSheet;
private BottomSheetBehavior mBottomSheetBehavior;
private ViewPagerBottomSheetBehavior mBottomSheetBehavior;
private int fab_outside_y_offest = 0;
private boolean is_fab_outside_peekheight;

Expand All @@ -52,25 +57,26 @@ public class AAH_FabulousFragment extends BottomSheetDialogFragment {
private ColorStateList fab_background_color_resource;
private View contentView;
private Callbacks callbacks;
private ViewPager viewPager;


private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
private ViewPagerBottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new ViewPagerBottomSheetBehavior.BottomSheetCallback() {

@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_HIDDEN:
case ViewPagerBottomSheetBehavior.STATE_HIDDEN:
if (callbacks != null) {
callbacks.onResult("swiped_down");
}
dismiss();
break;
case BottomSheetBehavior.STATE_COLLAPSED:
case ViewPagerBottomSheetBehavior.STATE_COLLAPSED:
ViewGroup.LayoutParams params = view_main.getLayoutParams();
params.height = ViewGroup.LayoutParams.MATCH_PARENT;
view_main.setLayoutParams(params);
break;
case BottomSheetBehavior.STATE_EXPANDED:
case ViewPagerBottomSheetBehavior.STATE_EXPANDED:
ViewGroup.LayoutParams params1 = view_main.getLayoutParams();
params1.height = ViewGroup.LayoutParams.MATCH_PARENT;
view_main.setLayoutParams(params1);
Expand Down Expand Up @@ -117,6 +123,10 @@ public static int getStatusBarHeight(final Context context) {
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
if (viewPager != null) {
BottomSheetUtils.setupViewPager(viewPager);
}

dialog.setContentView(contentView);

int[] location = new int[2];
Expand All @@ -132,7 +142,7 @@ public void setupDialog(Dialog dialog, int style) {

((View) contentView.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));

mBottomSheetBehavior = BottomSheetBehavior.from(((View) contentView.getParent()));
mBottomSheetBehavior = ViewPagerBottomSheetBehavior.from(((View) contentView.getParent()));
if (mBottomSheetBehavior != null) {
mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
if ((fab_pos_y - (metrics.heightPixels - (metrics.density * peek_height)) + (fab_size * metrics.density) - (fab_size * metrics.density)) <= 0) {
Expand All @@ -147,9 +157,9 @@ public void setupDialog(Dialog dialog, int style) {
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
ViewPagerBottomSheetDialog d = (ViewPagerBottomSheetDialog) dialog;
bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_COLLAPSED);
ViewPagerBottomSheetBehavior.from(bottomSheet).setState(ViewPagerBottomSheetBehavior.STATE_COLLAPSED);
if (viewgroup_static != null) {
int range = (int) (metrics.heightPixels - (metrics.density * peek_height) - getStatusBarHeight(getContext()));
viewgroup_static.animate().translationY(-range).setDuration(0).start();
Expand All @@ -166,8 +176,8 @@ public void onShow(DialogInterface dialog) {
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();

if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
if (behavior != null && behavior instanceof ViewPagerBottomSheetBehavior) {
((ViewPagerBottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
}

scale_by = (float) (peek_height * 1.6 / fab_size) * metrics.density;
Expand Down Expand Up @@ -214,7 +224,7 @@ public void onAnimationEnd(Animation animation) {
@Override
public void run() {
mBottomSheetBehavior.setPeekHeight((int) (metrics.density * peek_height));
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_COLLAPSED);
ViewPagerBottomSheetBehavior.from(bottomSheet).setState(ViewPagerBottomSheetBehavior.STATE_COLLAPSED);
if (is_fab_outside_peekheight) {
bottomSheet.requestLayout();
}
Expand Down Expand Up @@ -263,8 +273,8 @@ public void onAnimationRepeat(Animation animation) {
}

public void closeFilter(final Object o) {
if (BottomSheetBehavior.from(bottomSheet).getState() == BottomSheetBehavior.STATE_EXPANDED) {
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_COLLAPSED);
if (ViewPagerBottomSheetBehavior.from(bottomSheet).getState() == ViewPagerBottomSheetBehavior.STATE_EXPANDED) {
ViewPagerBottomSheetBehavior.from(bottomSheet).setState(ViewPagerBottomSheetBehavior.STATE_COLLAPSED);
}
fabulous_fab.setVisibility(View.VISIBLE);
view_main.setVisibility(View.INVISIBLE);
Expand All @@ -279,7 +289,7 @@ public void onAnimationEnd(Animator animation) {
fabulous_fab.setImageDrawable(fab_icon_resource);
if (is_fab_outside_peekheight) {
mBottomSheetBehavior.setPeekHeight(metrics.heightPixels - fab_pos_y);
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_COLLAPSED);
ViewPagerBottomSheetBehavior.from(bottomSheet).setState(ViewPagerBottomSheetBehavior.STATE_COLLAPSED);
bottomSheet.requestLayout();
// fabulous_fab.setY(fab_outside_y_offest - fab_pos_y + getStatusBarHeight(getContext()));
} else {
Expand Down Expand Up @@ -358,11 +368,15 @@ public void setCallbacks(Callbacks callbacks) {
}


public void setParent_fab(FloatingActionButton parent_fab) {
public void setParentFab(FloatingActionButton parent_fab) {
this.parent_fab = parent_fab;
}

public void setAnimationDuration(int anim_duration) {
this.anim_duration = anim_duration;
}

public void setViewPager(ViewPager viewPager) {
this.viewPager = viewPager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.allattentionhere.fabulousfilter.viewpagerbottomsheet;

import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;


public final class BottomSheetUtils {

public static void setupViewPager(ViewPager viewPager) {
final View bottomSheetParent = findBottomSheetParent(viewPager);
if (bottomSheetParent != null) {
viewPager.addOnPageChangeListener(new BottomSheetViewPagerListener(viewPager, bottomSheetParent));
}
}

private static class BottomSheetViewPagerListener extends ViewPager.SimpleOnPageChangeListener {
private final ViewPager viewPager;
private final ViewPagerBottomSheetBehavior<View> behavior;

private BottomSheetViewPagerListener(ViewPager viewPager, View bottomSheetParent) {
this.viewPager = viewPager;
this.behavior = ViewPagerBottomSheetBehavior.from(bottomSheetParent);
}

@Override
public void onPageSelected(int position) {
viewPager.post(new Runnable() {
@Override
public void run() {
behavior.invalidateScrollingChild();
}
});
}
}

private static View findBottomSheetParent(final View view) {
View current = view;
while (current != null) {
final ViewGroup.LayoutParams params = current.getLayoutParams();
if (params instanceof CoordinatorLayout.LayoutParams && ((CoordinatorLayout.LayoutParams) params).getBehavior() instanceof ViewPagerBottomSheetBehavior) {
return current;
}
final ViewParent parent = current.getParent();
current = parent == null || !(parent instanceof View) ? null : (View) parent;
}
return null;
}

}
Loading

0 comments on commit 7522a56

Please sign in to comment.