Skip to content

Commit

Permalink
Support Groupie with AND without data binding #16 (#93)
Browse files Browse the repository at this point in the history
Vanilla groupie is now in module library (com.xwray.groupie) and groupie with databinding is now in module library-databinding (com.xwray.groupie.databinding).

Items that use data binding should now extend BindableItem.

A fun side effect of this rewrite is that you can mix and match regular Items and BindableItems (maybe in case you want to do a gradual rewrite of an existing RecyclerView?)
  • Loading branch information
lisawray authored Jun 4, 2017
1 parent 270b427 commit 7fcca8b
Show file tree
Hide file tree
Showing 131 changed files with 1,865 additions and 506 deletions.
5 changes: 3 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ machine:
GRADLE_OPTS: '-Dorg.gradle.jvmargs="-Xmx2048m -XX:+HeapDumpOnOutOfMemoryError"'
dependencies:
pre:
- echo y | android update sdk --no-ui --all --filter android-`grep -oP '(?<=compileSdkVersion )[0-9].*[0-9]' groupie/build.gradle`,extra-android-m2repository,build-tools-`grep -oP "(?<=buildToolsVersion .)[0-9].*[0-9]" groupie/build.gradle`
- echo y | android update sdk --no-ui --all --filter android-25,extra-android-m2repository,build-tools-25.0.2
test:
post:
- cp -r groupie/build/test-results/* $CIRCLE_TEST_REPORTS
- cp -r library/build/test-results/* $CIRCLE_TEST_REPORTS


File renamed without changes.
36 changes: 36 additions & 0 deletions example-databinding/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "com.xwray.groupie.example.databinding"
minSdkVersion 17
targetSdkVersion 25
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary true
}
buildTypes {
release {
minifyEnabled false
}
}
dataBinding {
enabled = true
}

lintOptions {
abortOnError false
}
}

dependencies {
compile project(':library-databinding')
compile project(':example-shared')
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:support-vector-drawable:25.3.1'
compile 'com.android.support:animated-vector-drawable:25.3.1'
}
20 changes: 20 additions & 0 deletions example-databinding/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.xwray.groupie.example.databinding"
xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.xwray.groupie.example.databinding;

import com.xwray.groupie.Group;
import com.xwray.groupie.GroupDataObserver;
import com.xwray.groupie.Item;
import com.xwray.groupie.databinding.BindableItem;

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

/**
* A simple, non-editable, non-nested group of Items which displays a list as vertical columns.
*/
public class ColumnGroup implements Group {

private List<BindableItem> items = new ArrayList<>();

public ColumnGroup(List<? extends BindableItem> items) {
for (int i = 0; i < items.size(); i++) {
// Rearrange items so that the adapter appears to arrange them in vertical columns
int index;
if (i % 2 == 0) {
index = i / 2;
} else {
index = (i - 1) / 2 + (int) (items.size() / 2f);
// If columns are uneven, we'll put an extra one at the end of the first column,
// meaning the second column's indices will all be increased by 1
if (items.size() % 2 == 1) index++;
}
BindableItem trackItem = items.get(index);
this.items.add(trackItem);
}
}

public void setGroupDataObserver(GroupDataObserver groupDataObserver) {
// no real need to do anything here
}

public BindableItem getItem(int position) {
return items.get(position);
}

@Override
public int getPosition(Item item) {
return items.indexOf(item);
}

@Override public int getItemCount() {
return items.size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.xwray.groupie.example.databinding;

import android.graphics.drawable.Animatable;
import android.support.annotation.StringRes;
import android.view.View;

import com.xwray.groupie.ExpandableGroup;
import com.xwray.groupie.ExpandableItem;
import com.xwray.groupie.example.databinding.databinding.ItemHeaderBinding;
import com.xwray.groupie.example.databinding.item.HeaderItem;

public class ExpandableHeaderItem extends HeaderItem implements ExpandableItem {

private ExpandableGroup expandableGroup;

public ExpandableHeaderItem(@StringRes int titleStringResId, @StringRes int subtitleResId) {
super(titleStringResId, subtitleResId);
}

@Override public void bind(final ItemHeaderBinding viewBinding, int position) {
super.bind(viewBinding, position);

// Initial icon state -- not animated.
viewBinding.icon.setVisibility(View.VISIBLE);
viewBinding.icon.setImageResource(expandableGroup.isExpanded() ? R.drawable.collapse : R.drawable.expand);
viewBinding.icon.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
expandableGroup.onToggleExpanded();
bindIcon(viewBinding);
}
});
}

private void bindIcon(ItemHeaderBinding viewBinding) {
viewBinding.icon.setVisibility(View.VISIBLE);
viewBinding.icon.setImageResource(expandableGroup.isExpanded() ? R.drawable.collapse_animated : R.drawable.expand_animated);
Animatable drawable = (Animatable) viewBinding.icon.getDrawable();
drawable.start();
}

@Override public void setExpandableGroup(ExpandableGroup onToggleListener) {
this.expandableGroup = onToggleListener;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.xwray.groupie.example.databinding;

import android.support.annotation.ColorInt;
import android.support.annotation.LayoutRes;

public class HeaderItemDecoration extends com.xwray.groupie.example.core.decoration.HeaderItemDecoration {
public HeaderItemDecoration(@ColorInt int background, int sidePaddingPixels) {
super(background, sidePaddingPixels, R.layout.item_header);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.xwray.groupie.example.databinding;

import android.support.annotation.ColorInt;
import android.support.annotation.Dimension;

public class InsetItemDecoration extends com.xwray.groupie.example.core.decoration.InsetItemDecoration {
public InsetItemDecoration(@ColorInt int backgroundColor, @Dimension int padding) {
super(backgroundColor, padding, MainActivity.INSET_TYPE_KEY, MainActivity.INSET);
}
}
Loading

0 comments on commit 7fcca8b

Please sign in to comment.