Skip to content

Commit

Permalink
support initial number of actions, when more actions need to be shown…
Browse files Browse the repository at this point in the history
…, a more button will be shown
  • Loading branch information
soarcn committed Dec 9, 2014
1 parent f4ce454 commit 1cf9f4c
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 70 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:0.14.0'
classpath 'com.android.tools.build:gradle:1.0.0'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void onClick(DialogInterface dialog, int which) {
for (int i=0;i<25;i++) {
builder.sheet(i,getRoundedBitmap(R.drawable.icon),"Test"+i);
}
sheet = builder/**.limit(R.integer.bs_initial_row)**/.build();
sheet = builder.limit(R.integer.bs_initial_grid_row).build();
break;

}
Expand Down
12 changes: 1 addition & 11 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ BUILD_TOOLS_VERSION=21.1.1

POM_GROUP_ID=com.cocosw
POM_ARTIFACT_ID=bottomsheet
POM_VERSION=0.4
POM_VERSION=0.5
POM_NAME=bottomsheet
POM_PACKAGING=pom
POM_DESCRIPTION=One way to present a set of actions to a user is with bottom sheets, a sheet of paper that slides up from the bottom edge of the screen. Bottom sheets offer flexibility in the display of clear and simple actions that do not need explanation.
Expand All @@ -36,15 +36,5 @@ [email protected]:soarcn/BottomSheet.git
POM_SCM_CONNECTION=scm:git:[email protected]:soarcn/BottomSheet.git
POM_SCM_DEV_CONNECTION=scm:git:[email protected]:soarcn/BottomSheet.git

POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo
POM_LICENCE_COMMENTS=A business-friendly OSS license

POM_DEVELOPER_ID=soarcn
POM_DEVELOPER_NAME=Liao Kai
POM_DEVELOPER_EMAIL=[email protected]
POM_DEVELOPER_URL=http://www.cocosw.com

POM_ISSUE_MANAGEMENT_SYSTEM=Github
POM_ISSUE_MANAGEMENT_URL=https://github.com/soarcn/BottomSheet/issues
103 changes: 61 additions & 42 deletions library/src/main/java/com/cocosw/bottomsheet/BottomSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -22,8 +20,10 @@
import android.support.annotation.StringRes;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.transition.ChangeBounds;
import android.transition.Transition;
import android.transition.TransitionManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -58,7 +58,9 @@
*/
public class BottomSheet extends Dialog implements DialogInterface {

private int columnNum =1;
private String moreText;
private Drawable close;
private Drawable more;
private int mStatusBarHeight;
private GridView list;
private List<MenuItem> menuItem;
Expand Down Expand Up @@ -86,12 +88,12 @@ public BottomSheet(Context context) {
@SuppressWarnings("WeakerAccess")
public BottomSheet(Context context, int theme) {
super(context, theme);
TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{R.attr.bs_numColumns});
try {
columnNum = a.getInt(0, 1);
} finally {
a.recycle();
}

TypedArray a = getContext()
.obtainStyledAttributes(null, R.styleable.BottomSheet, R.attr.bottomSheetStyle, 0);
more = a.getDrawable(R.styleable.BottomSheet_bs_moreDrawable);
close = a.getDrawable(R.styleable.BottomSheet_bs_closeDrawable);
moreText = a.getString(R.styleable.BottomSheet_bs_moreText);

// https://github.com/jgilfelt/SystemBarTint/blob/master/library/src/com/readystatesoftware/systembartint/SystemBarTintManager.java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Expand Down Expand Up @@ -207,27 +209,19 @@ private boolean isNavigationAtBottom() {
return (mSmallestWidthDp >= 600 || mInPortrait);
}

private Drawable tintDrawable(Drawable input, int iColor) {
int red = (iColor & 0xFF0000) / 0xFFFF;
int green = (iColor & 0xFF00) / 0xFF;
int blue = iColor & 0xFF;

float[] matrix = {0, 0, 0, 0, red
, 0, 0, 0, 0, green
, 0, 0, 0, 0, blue
, 0, 0, 0, 1, 0};

ColorFilter colorFilter = new ColorMatrixColorFilter(matrix);

input.setColorFilter(colorFilter);
return input;
private int getNumColumns() {
try {
Field numColumns = GridView.class.getDeclaredField("mRequestedNumColumns");
numColumns.setAccessible(true);
return numColumns.getInt(list);
} catch (Exception e) {
return 1;
}
}



private void init(Context context) {
private void init(final Context context) {
setCanceledOnTouchOutside(true);
ClosableSlidingLayout mDialogView = (ClosableSlidingLayout) View.inflate(context, R.layout.bottom_sheet_dialog, null);
final ClosableSlidingLayout mDialogView = (ClosableSlidingLayout) View.inflate(context, R.layout.bottom_sheet_dialog, null);
setContentView(mDialogView);
mDialogView.setSlideListener(new ClosableSlidingLayout.SlideListener() {
@Override
Expand All @@ -248,6 +242,12 @@ public void onShow(DialogInterface dialogInterface) {
actions = menuItem;
list.setAdapter(adapter);
list.startLayoutAnimation();
if (builder.icon==null)
icon.setVisibility(View.GONE);
else {
icon.setVisibility(View.VISIBLE);
icon.setImageDrawable(builder.icon);
}
}
});

Expand All @@ -256,25 +256,19 @@ public void onShow(DialogInterface dialogInterface) {
mDialogView.getChildAt(0).setPadding(0, 0,0, mNavBarAvailable?getNavigationBarHeight(getContext())+mDialogView.getPaddingBottom():0);
}

TextView title = (TextView) mDialogView.findViewById(R.id.bottom_sheet_title);
final TextView title = (TextView) mDialogView.findViewById(R.id.bottom_sheet_title);
if (builder.title != null) {
title.setVisibility(View.VISIBLE);
title.setText(builder.title);
}

icon = (ImageView) mDialogView.findViewById(R.id.bottom_sheet_title_image);
if (builder.icon==null)
icon.setVisibility(View.GONE);
else {
icon.setVisibility(View.VISIBLE);
icon.setImageDrawable(builder.icon);
}


list = (GridView) mDialogView.findViewById(R.id.bottom_sheet_gridview);
mDialogView.mTarget = list;
if (!builder.grid) {
list.setNumColumns(1);
columnNum = 1;
}

menuItem = builder.menuItems;
Expand All @@ -289,22 +283,20 @@ else if (item.icon==null) {
throw new IllegalArgumentException("You should set icon for each items in grid style");
}
}
// list.setPadding(R.dimen.bs_grid_left_padding,R.dimen.bs_grid_top_padding,R.dimen.bs_grid_right_padding,R.dimen.bs_grid_bottom_padding);
}

builder.limit = builder.limit*columnNum;
builder.limit = builder.limit*getNumColumns();

// over the initial numbers
if (menuItem.size() > builder.limit) {
fullMenuItem = new ArrayList<>(menuItem);
menuItem = menuItem.subList(0,builder.limit-2);
menuItem.add(new MenuItem(R.id.bs_more, "More", context.getResources().getDrawable(R.drawable.bs_ic_more)));
menuItem = menuItem.subList(0,builder.limit-1);
menuItem.add(new MenuItem(R.id.bs_more, moreText,more));
}
actions = menuItem;

adapter = new BaseAdapter() {


@Override
public int getCount() {
return actions.size();
Expand Down Expand Up @@ -394,11 +386,35 @@ class ViewHolder {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (((MenuItem) adapter.getItem(position)).id==R.id.bs_more) {
actions = fullMenuItem;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Transition changeBounds = new ChangeBounds();
changeBounds.setDuration(300);
TransitionManager.beginDelayedTransition(list,changeBounds);
}

adapter.notifyDataSetChanged();
ViewGroup.LayoutParams params = list.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
list.setLayoutParams(params);
icon.setVisibility(View.VISIBLE);
icon.setImageDrawable(close);
icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
actions = menuItem;
adapter.notifyDataSetChanged();
setListLayout();

if (builder.icon==null)
icon.setVisibility(View.GONE);
else {
icon.setVisibility(View.VISIBLE);
icon.setImageDrawable(builder.icon);
}
}
});
return;
}

Expand All @@ -412,7 +428,10 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
if(builder.dismissListener != null){
setOnDismissListener(builder.dismissListener);
}
setListLayout();
}

private void setListLayout() {
list.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Expand Down Expand Up @@ -732,7 +751,7 @@ public Builder grid() {
* @param limitRes
* @return
*/
private Builder limit(@IntegerRes int limitRes) {
public Builder limit(@IntegerRes int limitRes) {
limit = context.getResources().getInteger(limitRes);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package com.cocosw.bottomsheet;

import android.annotation.TargetApi;
import android.gesture.Gesture;
import android.gesture.GestureUtils;
import android.content.Context;
import android.os.Build;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewConfigurationCompat;
import android.util.Log;
import android.view.ViewConfiguration;
import android.view.animation.Animation;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.FrameLayout;

/**
* Project: gradle
Expand Down Expand Up @@ -93,6 +85,10 @@ public boolean onInterceptTouchEvent(MotionEvent event) {
mIsBeingDragged = true;
mDragHelper.captureChildView(getChildAt(0), 0);
}
// else if(yDiff < -mDragHelper.getTouchSlop() && !mIsBeingDragged) {
// if (mListener!=null) mListener.onOpened();
// }

break;
}
mDragHelper.shouldInterceptTouchEvent(event);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed library/src/main/res/drawable-xxxhdpi/bs_ic_more.png
Binary file not shown.
7 changes: 5 additions & 2 deletions library/src/main/res/layout/bottom_sheet_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
android:layout_height="wrap_content" android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">

<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/bottom_sheet_title_image"
android:layout_marginLeft="16dp"
android:visibility="gone"/>
android:layout_marginStart="16dp"
android:visibility="gone"
android:contentDescription="icon" />

<TextView
android:id="@+id/bottom_sheet_title"
Expand Down
3 changes: 2 additions & 1 deletion library/src/main/res/values-land/integer.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="bs_grid_colum">4</integer>
<integer name="bs_initial_row">2</integer>
<integer name="bs_initial_grid_row">2</integer>
<integer name="bs_initial_list_row">3</integer>
</resources>
4 changes: 4 additions & 0 deletions library/src/main/res/values-zh/string.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="bs_more">更多</string>
</resources>
3 changes: 3 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<attr name="bs_titleTextAppearance" format="reference" />
<attr name="bs_listItemTitleTextAppearance" format="reference" />
<attr name="bs_gridItemTitleTextAppearance" format="reference" />
<attr name="bs_moreDrawable" format="reference"/>
<attr name="bs_moreText" format="string"/>
<attr name="bs_closeDrawable" format="reference"/>
</declare-styleable>

</resources>
3 changes: 2 additions & 1 deletion library/src/main/res/values/integer.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="bs_grid_colum">3</integer>
<integer name="bs_initial_row">5</integer>
<integer name="bs_initial_grid_row">3</integer>
<integer name="bs_initial_list_row">5</integer>
</resources>
4 changes: 4 additions & 0 deletions library/src/main/res/values/string.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="bs_more">More</string>
</resources>
5 changes: 5 additions & 0 deletions library/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<item name="bs_titleTextAppearance">@style/Text.Headline</item>
<item name="bs_listItemTitleTextAppearance">@style/Text.Title</item>
<item name="bs_gridItemTitleTextAppearance">@style/Text.Hint</item>
<item name="bs_moreText">@string/bs_more</item>
<item name="bs_closeDrawable">@drawable/bs_ic_clear_light</item>
<item name="bs_moreDrawable">@drawable/bs_ic_more_light</item>
</style>

<style name="BottomSheet.Dialog.Dark" parent="BottomSheet.Dialog">
Expand All @@ -33,6 +36,8 @@
<item name="bs_dialogBackground">@android:color/background_dark</item>
<item name="bs_dividerColor">@color/bs_dark_divider_color</item>
<item name="bs_listSelector">@drawable/bs_list_dark_selector</item>
<item name="bs_closeDrawable">@drawable/bs_ic_clear</item>
<item name="bs_moreDrawable">@drawable/bs_ic_more</item>
</style>

<style name="Text"/>
Expand Down

0 comments on commit 1cf9f4c

Please sign in to comment.