Skip to content

Commit 7fcca8b

Browse files
authored
Support Groupie with AND without data binding #16 (#93)
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?)
1 parent 270b427 commit 7fcca8b

131 files changed

Lines changed: 1865 additions & 506 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

circle.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ machine:
33
GRADLE_OPTS: '-Dorg.gradle.jvmargs="-Xmx2048m -XX:+HeapDumpOnOutOfMemoryError"'
44
dependencies:
55
pre:
6-
- 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`
6+
- echo y | android update sdk --no-ui --all --filter android-25,extra-android-m2repository,build-tools-25.0.2
77
test:
88
post:
9-
- cp -r groupie/build/test-results/* $CIRCLE_TEST_REPORTS
9+
- cp -r library/build/test-results/* $CIRCLE_TEST_REPORTS
10+
1011

example-databinding/build.gradle

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 25
5+
buildToolsVersion '25.0.2'
6+
defaultConfig {
7+
applicationId "com.xwray.groupie.example.databinding"
8+
minSdkVersion 17
9+
targetSdkVersion 25
10+
versionCode 1
11+
versionName "1.0"
12+
vectorDrawables.useSupportLibrary true
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
}
18+
}
19+
dataBinding {
20+
enabled = true
21+
}
22+
23+
lintOptions {
24+
abortOnError false
25+
}
26+
}
27+
28+
dependencies {
29+
compile project(':library-databinding')
30+
compile project(':example-shared')
31+
compile 'com.android.support:appcompat-v7:25.3.1'
32+
compile 'com.android.support:design:25.3.1'
33+
compile 'com.android.support:cardview-v7:25.3.1'
34+
compile 'com.android.support:support-vector-drawable:25.3.1'
35+
compile 'com.android.support:animated-vector-drawable:25.3.1'
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest package="com.xwray.groupie.example.databinding"
3+
xmlns:android="http://schemas.android.com/apk/res/android">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:supportsRtl="true"
10+
android:theme="@style/AppTheme">
11+
<activity android:name=".MainActivity">
12+
<intent-filter>
13+
<action android:name="android.intent.action.MAIN"/>
14+
15+
<category android:name="android.intent.category.LAUNCHER"/>
16+
</intent-filter>
17+
</activity>
18+
</application>
19+
20+
</manifest>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.xwray.groupie.example.databinding;
2+
3+
import com.xwray.groupie.Group;
4+
import com.xwray.groupie.GroupDataObserver;
5+
import com.xwray.groupie.Item;
6+
import com.xwray.groupie.databinding.BindableItem;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
/**
12+
* A simple, non-editable, non-nested group of Items which displays a list as vertical columns.
13+
*/
14+
public class ColumnGroup implements Group {
15+
16+
private List<BindableItem> items = new ArrayList<>();
17+
18+
public ColumnGroup(List<? extends BindableItem> items) {
19+
for (int i = 0; i < items.size(); i++) {
20+
// Rearrange items so that the adapter appears to arrange them in vertical columns
21+
int index;
22+
if (i % 2 == 0) {
23+
index = i / 2;
24+
} else {
25+
index = (i - 1) / 2 + (int) (items.size() / 2f);
26+
// If columns are uneven, we'll put an extra one at the end of the first column,
27+
// meaning the second column's indices will all be increased by 1
28+
if (items.size() % 2 == 1) index++;
29+
}
30+
BindableItem trackItem = items.get(index);
31+
this.items.add(trackItem);
32+
}
33+
}
34+
35+
public void setGroupDataObserver(GroupDataObserver groupDataObserver) {
36+
// no real need to do anything here
37+
}
38+
39+
public BindableItem getItem(int position) {
40+
return items.get(position);
41+
}
42+
43+
@Override
44+
public int getPosition(Item item) {
45+
return items.indexOf(item);
46+
}
47+
48+
@Override public int getItemCount() {
49+
return items.size();
50+
}
51+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.xwray.groupie.example.databinding;
2+
3+
import android.graphics.drawable.Animatable;
4+
import android.support.annotation.StringRes;
5+
import android.view.View;
6+
7+
import com.xwray.groupie.ExpandableGroup;
8+
import com.xwray.groupie.ExpandableItem;
9+
import com.xwray.groupie.example.databinding.databinding.ItemHeaderBinding;
10+
import com.xwray.groupie.example.databinding.item.HeaderItem;
11+
12+
public class ExpandableHeaderItem extends HeaderItem implements ExpandableItem {
13+
14+
private ExpandableGroup expandableGroup;
15+
16+
public ExpandableHeaderItem(@StringRes int titleStringResId, @StringRes int subtitleResId) {
17+
super(titleStringResId, subtitleResId);
18+
}
19+
20+
@Override public void bind(final ItemHeaderBinding viewBinding, int position) {
21+
super.bind(viewBinding, position);
22+
23+
// Initial icon state -- not animated.
24+
viewBinding.icon.setVisibility(View.VISIBLE);
25+
viewBinding.icon.setImageResource(expandableGroup.isExpanded() ? R.drawable.collapse : R.drawable.expand);
26+
viewBinding.icon.setOnClickListener(new View.OnClickListener() {
27+
@Override public void onClick(View view) {
28+
expandableGroup.onToggleExpanded();
29+
bindIcon(viewBinding);
30+
}
31+
});
32+
}
33+
34+
private void bindIcon(ItemHeaderBinding viewBinding) {
35+
viewBinding.icon.setVisibility(View.VISIBLE);
36+
viewBinding.icon.setImageResource(expandableGroup.isExpanded() ? R.drawable.collapse_animated : R.drawable.expand_animated);
37+
Animatable drawable = (Animatable) viewBinding.icon.getDrawable();
38+
drawable.start();
39+
}
40+
41+
@Override public void setExpandableGroup(ExpandableGroup onToggleListener) {
42+
this.expandableGroup = onToggleListener;
43+
}
44+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.xwray.groupie.example.databinding;
2+
3+
import android.support.annotation.ColorInt;
4+
import android.support.annotation.LayoutRes;
5+
6+
public class HeaderItemDecoration extends com.xwray.groupie.example.core.decoration.HeaderItemDecoration {
7+
public HeaderItemDecoration(@ColorInt int background, int sidePaddingPixels) {
8+
super(background, sidePaddingPixels, R.layout.item_header);
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.xwray.groupie.example.databinding;
2+
3+
import android.support.annotation.ColorInt;
4+
import android.support.annotation.Dimension;
5+
6+
public class InsetItemDecoration extends com.xwray.groupie.example.core.decoration.InsetItemDecoration {
7+
public InsetItemDecoration(@ColorInt int backgroundColor, @Dimension int padding) {
8+
super(backgroundColor, padding, MainActivity.INSET_TYPE_KEY, MainActivity.INSET);
9+
}
10+
}

0 commit comments

Comments
 (0)