Skip to content

Commit

Permalink
Introduce ExpandableItemIndicator widget
Browse files Browse the repository at this point in the history
  • Loading branch information
h6ah4i committed Jul 27, 2015
1 parent c253a4c commit 8d736ec
Show file tree
Hide file tree
Showing 31 changed files with 282 additions and 267 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2015 Haruki Hasegawa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.h6ah4i.android.example.advrecyclerview.common.widget;

import android.content.Context;
import android.os.Build;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.widget.FrameLayout;

public class ExpandableItemIndicator extends FrameLayout {
static abstract class Impl {
public abstract void onInit(Context context, AttributeSet attrs, int defStyleAttr, ExpandableItemIndicator thiz);

public abstract void setExpandedState(boolean isExpanded, boolean animate);
}

private Impl mImpl;

public ExpandableItemIndicator(Context context) {
super(context);
onInit(context, null, 0);
}

public ExpandableItemIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
onInit(context, attrs, 0);
}

public ExpandableItemIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
onInit(context, attrs, defStyleAttr);
}

protected void onInit(Context context, AttributeSet attrs, int defStyleAttr) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// NOTE: MorphButton only supports API level 14 or later
mImpl = new ExpandableItemIndicatorImplAnim();
} else {
mImpl = new ExpandableItemIndicatorImplNoAnim();
}
mImpl.onInit(context, attrs, defStyleAttr, this);
}

@Override
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
super.dispatchFreezeSelfOnly(container);
}

@Override
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
super.dispatchThawSelfOnly(container);
}

public void setExpandedState(boolean isExpanded, boolean animate) {
mImpl.setExpandedState(isExpanded, animate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2015 Haruki Hasegawa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.h6ah4i.android.example.advrecyclerview.common.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.h6ah4i.android.example.advrecyclerview.R;
import com.wnafee.vector.MorphButton;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
class ExpandableItemIndicatorImplAnim extends ExpandableItemIndicator.Impl {
private MorphButton mMorphButton;

@Override
public void onInit(Context context, AttributeSet attrs, int defStyleAttr, ExpandableItemIndicator thiz) {
View v = LayoutInflater.from(context).inflate(R.layout.widget_expandable_item_indicator_anim, thiz, true);
mMorphButton = (MorphButton) v.findViewById(R.id.morph_button);
}

@Override
public void setExpandedState(boolean isExpanded, boolean animate) {
MorphButton.MorphState indicatorState = (isExpanded) ? MorphButton.MorphState.END : MorphButton.MorphState.START;

if (mMorphButton.getState() != indicatorState) {
mMorphButton.setState(indicatorState, animate);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2015 Haruki Hasegawa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.h6ah4i.android.example.advrecyclerview.common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.h6ah4i.android.example.advrecyclerview.R;

class ExpandableItemIndicatorImplNoAnim extends ExpandableItemIndicator.Impl {
private ImageView mImageView;

@Override
public void onInit(Context context, AttributeSet attrs, int defStyleAttr, ExpandableItemIndicator thiz) {
View v = LayoutInflater.from(context).inflate(R.layout.widget_expandable_item_indicator_no_anim, thiz, true);
mImageView = (ImageView) v.findViewById(R.id.image_view);
}

@Override
public void setExpandedState(boolean isExpanded, boolean animate) {
int resId = (isExpanded) ? R.drawable.ic_expand_less : R.drawable.ic_expand_more;
mImageView.setImageResource(resId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@

package com.h6ah4i.android.example.advrecyclerview.demo_e;

import android.support.v4.view.ViewCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.h6ah4i.android.example.advrecyclerview.R;
import com.h6ah4i.android.example.advrecyclerview.common.compat.MorphButtonCompat;
import com.h6ah4i.android.example.advrecyclerview.common.data.AbstractExpandableDataProvider;
import com.h6ah4i.android.example.advrecyclerview.common.utils.ViewUtils;
import com.h6ah4i.android.example.advrecyclerview.common.widget.ExpandableItemIndicator;
import com.h6ah4i.android.widget.advrecyclerview.expandable.RecyclerViewExpandableItemManager;
import com.h6ah4i.android.widget.advrecyclerview.utils.AbstractExpandableItemAdapter;
import com.h6ah4i.android.widget.advrecyclerview.utils.AbstractExpandableItemViewHolder;
import com.wnafee.vector.MorphButton;

public class MyExpandableItemAdapter
extends AbstractExpandableItemAdapter<MyExpandableItemAdapter.MyGroupViewHolder, MyExpandableItemAdapter.MyChildViewHolder> {
Expand All @@ -50,11 +47,11 @@ public MyBaseViewHolder(View v) {
}

public static class MyGroupViewHolder extends MyBaseViewHolder {
public MorphButtonCompat mMorphButton;
public ExpandableItemIndicator mIndicator;

public MyGroupViewHolder(View v) {
super(v);
mMorphButton = new MorphButtonCompat(v.findViewById(R.id.indicator));
mIndicator = (ExpandableItemIndicator) v.findViewById(R.id.indicator);
}
}

Expand Down Expand Up @@ -132,21 +129,18 @@ public void onBindGroupViewHolder(MyGroupViewHolder holder, int groupPosition, i

if ((expandState & RecyclerViewExpandableItemManager.STATE_FLAG_IS_UPDATED) != 0) {
int bgResId;
MorphButton.MorphState indicatorState;
boolean isExpanded;

if ((expandState & RecyclerViewExpandableItemManager.STATE_FLAG_IS_EXPANDED) != 0) {
bgResId = R.drawable.bg_group_item_expanded_state;
indicatorState = MorphButton.MorphState.END;
isExpanded = true;
} else {
bgResId = R.drawable.bg_group_item_normal_state;
indicatorState = MorphButton.MorphState.START;
isExpanded = false;
}

holder.mContainer.setBackgroundResource(bgResId);

if (holder.mMorphButton.getState() != indicatorState) {
holder.mMorphButton.setState(indicatorState, true);
}
holder.mIndicator.setExpandedState(isExpanded, true);
}
}

Expand Down
Loading

0 comments on commit 8d736ec

Please sign in to comment.