├── .gitignore ├── README.md ├── build.gradle ├── celladapter ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── io │ │ └── techery │ │ └── celladapter │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── io │ │ │ └── techery │ │ │ └── celladapter │ │ │ ├── Cell.java │ │ │ ├── CellAdapter.java │ │ │ ├── Layout.java │ │ │ └── select │ │ │ ├── SelectableCell.java │ │ │ ├── SelectableCellAdapter.java │ │ │ └── mode │ │ │ ├── MultiSelectionManager.java │ │ │ ├── SelectionManager.java │ │ │ └── SingleSelectionManager.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── io │ └── techery │ └── celladapter │ └── ExampleUnitTest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── sample ├── build.gradle ├── images │ └── ic_launcher.svg ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── io │ │ └── techery │ │ └── sample │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── io │ │ │ └── techery │ │ │ └── sample │ │ │ ├── BaseCell.java │ │ │ ├── BaseCellAdapter.java │ │ │ ├── BaseSelectableCell.java │ │ │ ├── DividerItemDecoration.java │ │ │ ├── base │ │ │ ├── BaseSampleActivity.java │ │ │ ├── cell │ │ │ │ ├── AlphaCell.java │ │ │ │ ├── BetaCell.java │ │ │ │ └── GammaCell.java │ │ │ └── model │ │ │ │ ├── AlphaModel.java │ │ │ │ ├── BetaModel.java │ │ │ │ └── GammaModel.java │ │ │ ├── home │ │ │ ├── MenuItem.java │ │ │ ├── MenuItemCell.java │ │ │ └── SampleActivity.java │ │ │ ├── multi │ │ │ ├── MultiChoiceActivity.java │ │ │ ├── MultiChoiceCell.java │ │ │ └── MultiChoiceModel.java │ │ │ └── single │ │ │ ├── SingleChoiceActivity.java │ │ │ ├── SingleChoiceCell.java │ │ │ └── SingleChoiceModel.java │ └── res │ │ ├── layout │ │ ├── activity_with_recycler_view.xml │ │ ├── item_base_alpha.xml │ │ ├── item_base_beta.xml │ │ ├── item_base_gamma.xml │ │ ├── item_multi.xml │ │ └── item_single.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── io │ └── techery │ └── sample │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/ 38 | 39 | # Keystore files 40 | *.jks -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CellAdapter 2 | 3 | This library simplifies RecyclerView with multiple view types. 4 | Main points: 5 | 6 | * Single adapter class for all project 7 | * Extend item types in RecyclerView - just register in adapter and extend Cell.class for mapping model with UI (below more details + [`sample`](https://github.com/techery/CellAdapter/tree/master/sample/src/main/java/io/techery/sample)) 8 | * Add any UI listeners to each item type 9 | 10 | No more code like this: 11 | ```java 12 | @Override 13 | public int getItemViewType(int position) { 14 | // Just as an example, return 0 or 2 depending on position 15 | return position % 2 * 2; 16 | } 17 | 18 | @Override 19 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 20 | switch (viewType) { 21 | case 0: return new ViewHolder0(...); 22 | case 2: return new ViewHolder2(...); 23 | ... 24 | } 25 | } 26 | ``` 27 | 28 | ## Usage 29 | 30 | ```java 31 | CellAdapter adapter = new CellAdapter(context); 32 | //feel free to register multiple models and cells (model per cell, so your RecyclerView would represent multiple view types) 33 | adapter.registerCell(Model.class, YourCell.class, new YourCell.Listener(){}); 34 | 35 | List items = new ArrayList(); 36 | items.add(new Model()); 37 | adapter.setItems(items); 38 | adapter.notifyDataSetChanged(); 39 | ``` 40 | where 41 | `Model.class` is POJO and `YourCell.class` is 42 | ```java 43 | @Layout(R.layout.your_cell_view) 44 | public class YourCell extends Cell { 45 | 46 | @Override 47 | protected void syncUiWithItem() { 48 | getItem() // is your Model object 49 | } 50 | 51 | protected void prepareForReuse() { 52 | //optional 53 | } 54 | 55 | protected void clearResources() { 56 | //optional 57 | } 58 | 59 | public interface Listener extends Cell.Listener { 60 | void callbackSample(Model model); 61 | } 62 | } 63 | ``` 64 | Also please find 65 | [`CellAdapter/sample/src/main/java/io/techery/sample/BaseCell.java`](https://github.com/techery/CellAdapter/blob/master/sample/src/main/java/io/techery/sample/BaseCell.java) 66 | there is sample how to implement ButterKnife in Cells. 67 | 68 | ## Download 69 | 70 | The dependency is available via [jCenter](https://bintray.com/techery/android/celladapter). 71 | jCenter is the default Maven repository used by Android Studio. 72 | 73 | #### Gradle 74 | ```groovy 75 | dependencies { 76 | compile 'io.techery:celladapter:1.0.1' 77 | } 78 | ``` 79 | 80 | #### Maven 81 | ```xml 82 | 83 | io.techery 84 | celladapter 85 | 1.0.1 86 | pom 87 | 88 | ``` 89 | 90 | ## License 91 | 92 | The MIT License (MIT) 93 | 94 | Copyright (c) 2016 Techery (http://techery.io/) 95 | 96 | Permission is hereby granted, free of charge, to any person obtaining a copy 97 | of this software and associated documentation files (the "Software"), to deal 98 | in the Software without restriction, including without limitation the rights 99 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 100 | copies of the Software, and to permit persons to whom the Software is 101 | furnished to do so, subject to the following conditions: 102 | 103 | The above copyright notice and this permission notice shall be included in 104 | all copies or substantial portions of the Software. 105 | 106 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 107 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 108 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 109 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 110 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 111 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 112 | THE SOFTWARE. 113 | 114 | ------- 115 | 116 |
Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY
117 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | } 5 | dependencies { 6 | classpath 'com.android.tools.build:gradle:2.2.2' 7 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 8 | //publishing 9 | classpath 'com.novoda:bintray-release:0.4.0' 10 | } 11 | } 12 | 13 | allprojects { 14 | repositories { 15 | jcenter() 16 | } 17 | } 18 | 19 | task clean(type: Delete) { 20 | delete rootProject.buildDir 21 | } -------------------------------------------------------------------------------- /celladapter/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.novoda.bintray-release' 3 | 4 | android { 5 | compileSdkVersion 24 6 | buildToolsVersion "24.0.3" 7 | 8 | defaultConfig { 9 | minSdkVersion 15 10 | targetSdkVersion 24 11 | versionCode 4 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | publish { 26 | userOrg = 'techery' 27 | repoName = 'android' 28 | groupId = 'io.techery' 29 | artifactId = 'celladapter' 30 | publishVersion = '1.0.4' 31 | desc = 'A small library to simplify RecyclerView adapter by using auto mapping item view with model' 32 | licences = ['MIT'] 33 | website = 'https://github.com/techery/CellAdapter' 34 | } 35 | 36 | dependencies { 37 | compile fileTree(dir: 'libs', include: ['*.jar']) 38 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 39 | exclude group: 'com.android.support', module: 'support-annotations' 40 | }) 41 | compile 'com.android.support:recyclerview-v7:24.2.1' 42 | testCompile 'junit:junit:4.12' 43 | } -------------------------------------------------------------------------------- /celladapter/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Development\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /celladapter/src/androidTest/java/io/techery/celladapter/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package io.techery.celladapter; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("io.techery.celladapter.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /celladapter/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /celladapter/src/main/java/io/techery/celladapter/Cell.java: -------------------------------------------------------------------------------- 1 | package io.techery.celladapter; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.View; 5 | 6 | public abstract class Cell> extends RecyclerView.ViewHolder { 7 | 8 | private ITEM item; 9 | private LISTENER listener; 10 | 11 | public Cell(View view) { 12 | super(view); 13 | view.setOnClickListener(new View.OnClickListener() { 14 | @Override 15 | public void onClick(View v) { 16 | if (listener != null) listener.onCellClicked(getItem()); 17 | } 18 | }); 19 | view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() { 20 | @Override 21 | public void onViewAttachedToWindow(View v) { 22 | } 23 | 24 | @Override 25 | public void onViewDetachedFromWindow(View v) { 26 | clearResources(); 27 | v.removeOnAttachStateChangeListener(this); 28 | } 29 | }); 30 | } 31 | 32 | protected final ITEM getItem() { 33 | return item; 34 | } 35 | 36 | protected LISTENER getListener() { 37 | return listener; 38 | } 39 | 40 | protected abstract void syncUiWithItem(); 41 | 42 | protected void prepareForReuse() { 43 | } 44 | 45 | protected void clearResources() { 46 | } 47 | 48 | void setCellDelegate(LISTENER listener) { 49 | this.listener = listener; 50 | } 51 | 52 | void setItem(ITEM item) { 53 | this.item = item; 54 | } 55 | 56 | void fillWithItem(ITEM item) { 57 | setItem(item); 58 | syncUiWithItem(); 59 | } 60 | 61 | public interface Listener { 62 | 63 | void onCellClicked(ITEM item); 64 | } 65 | } -------------------------------------------------------------------------------- /celladapter/src/main/java/io/techery/celladapter/CellAdapter.java: -------------------------------------------------------------------------------- 1 | package io.techery.celladapter; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.util.Log; 7 | import android.util.SparseArray; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | 12 | import java.lang.reflect.Constructor; 13 | import java.util.ArrayList; 14 | import java.util.HashMap; 15 | import java.util.List; 16 | import java.util.Map; 17 | 18 | /** 19 | * @param is yours POJO model 20 | */ 21 | public class CellAdapter extends RecyclerView.Adapter { 22 | 23 | /** 24 | * Map 25 | */ 26 | private final Map> itemCellMap = new HashMap<>(); 27 | private final List viewTypes = new ArrayList<>(); 28 | private final SparseArray typeListenerMapping = new SparseArray<>(); 29 | protected List items = new ArrayList<>(); 30 | 31 | private LayoutInflater layoutInflater; 32 | 33 | public CellAdapter(Context context) { 34 | layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 35 | } 36 | 37 | public void registerCell(Class itemClass, 38 | Class cellClass) { 39 | registerCell(itemClass, cellClass, null); 40 | } 41 | 42 | public void registerCell(Class itemClass, 43 | Class cellClass, 44 | @Nullable Cell.Listener cellListener) { 45 | itemCellMap.put(itemClass, cellClass); 46 | int type = viewTypes.indexOf(itemClass); 47 | if (type == -1) { 48 | viewTypes.add(itemClass); 49 | } 50 | registerListener(itemClass, cellListener); 51 | } 52 | 53 | private void registerListener(Class itemClass, 54 | @Nullable Cell.Listener cellListener) { 55 | int index = viewTypes.indexOf(itemClass); 56 | if (index < 0) 57 | throw new IllegalStateException(itemClass.getSimpleName() + " is not registered as Cell"); 58 | typeListenerMapping.put(index, cellListener); 59 | } 60 | 61 | @Override 62 | public Cell onCreateViewHolder(ViewGroup parent, int viewType) { 63 | Class itemClass = viewTypes.get(viewType); 64 | Class cellClass = itemCellMap.get(itemClass); 65 | Cell cell = buildCell(cellClass, parent); 66 | cell.setCellDelegate(typeListenerMapping.get(viewType)); 67 | return cell; 68 | } 69 | 70 | private Cell buildCell(Class cellClass, ViewGroup parent) { 71 | Layout layoutAnnotation = cellClass.getAnnotation(Layout.class); 72 | View cellView = layoutInflater.inflate(layoutAnnotation.value(), parent, false); 73 | RecyclerView.ViewHolder cellObject = null; 74 | try { 75 | Constructor constructor = cellClass.getConstructor(View.class); 76 | cellObject = constructor.newInstance(cellView); 77 | } catch (Exception e) { 78 | Log.e("CellAdapter", "Can't create cell: " + e.getMessage()); 79 | } 80 | return (Cell) cellObject; 81 | } 82 | 83 | @Override 84 | public void onBindViewHolder(Cell cell, int position) { 85 | ITEM item = getItem(position); 86 | cell.prepareForReuse(); 87 | cell.fillWithItem(item); 88 | } 89 | 90 | @Override 91 | public long getItemId(int position) { 92 | return super.getItemId(position); 93 | } 94 | 95 | public int getClassItemViewType(Class itemClass) { 96 | int index = viewTypes.indexOf(itemClass); 97 | if (index < 0) { 98 | throw new IllegalArgumentException(itemClass.getSimpleName() + " is not registered"); 99 | } 100 | return index; 101 | } 102 | 103 | @Override 104 | public int getItemViewType(int position) { 105 | ITEM ITEM = items.get(position); 106 | Class itemClass = ITEM.getClass(); 107 | return getClassItemViewType(itemClass); 108 | } 109 | 110 | @Override 111 | public int getItemCount() { 112 | return items.size(); 113 | } 114 | 115 | public ITEM getItem(int position) { 116 | return items.get(position); 117 | } 118 | 119 | public List getItems() { 120 | return items; 121 | } 122 | 123 | public void setItems(List items) { 124 | this.items = items; 125 | } 126 | 127 | public void clear() { 128 | items.clear(); 129 | } 130 | } -------------------------------------------------------------------------------- /celladapter/src/main/java/io/techery/celladapter/Layout.java: -------------------------------------------------------------------------------- 1 | package io.techery.celladapter; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.TYPE) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface Layout { 11 | 12 | int value(); 13 | } -------------------------------------------------------------------------------- /celladapter/src/main/java/io/techery/celladapter/select/SelectableCell.java: -------------------------------------------------------------------------------- 1 | package io.techery.celladapter.select; 2 | 3 | import android.view.View; 4 | 5 | import io.techery.celladapter.Cell; 6 | import io.techery.celladapter.select.mode.SelectionManager; 7 | 8 | public abstract class SelectableCell> extends Cell { 9 | 10 | protected SelectionManager selectionManager; 11 | 12 | public SelectableCell(View view) { 13 | super(view); 14 | } 15 | 16 | public void setSelectionManager(SelectionManager selectionManager) { 17 | this.selectionManager = selectionManager; 18 | } 19 | } -------------------------------------------------------------------------------- /celladapter/src/main/java/io/techery/celladapter/select/SelectableCellAdapter.java: -------------------------------------------------------------------------------- 1 | package io.techery.celladapter.select; 2 | 3 | import android.content.Context; 4 | import android.view.ViewGroup; 5 | 6 | import java.util.Collection; 7 | import java.util.HashSet; 8 | import java.util.Set; 9 | 10 | import io.techery.celladapter.Cell; 11 | import io.techery.celladapter.CellAdapter; 12 | import io.techery.celladapter.select.mode.SelectionManager; 13 | 14 | public class SelectableCellAdapter extends CellAdapter { 15 | 16 | private Set selectedPositions; 17 | private SelectionManager selectionManager; 18 | 19 | public SelectableCellAdapter(Context context, SelectionManager selectionManager) { 20 | this(context, new HashSet(), selectionManager); 21 | } 22 | 23 | public SelectableCellAdapter(Context context, Set selectedPositions, SelectionManager selectionManager) { 24 | super(context); 25 | this.selectedPositions = selectedPositions; 26 | this.selectionManager = selectionManager; 27 | this.selectionManager.setAdapter(this); 28 | } 29 | 30 | @Override 31 | public Cell onCreateViewHolder(ViewGroup parent, int viewType) { 32 | Cell cell = super.onCreateViewHolder(parent, viewType); 33 | if (cell instanceof SelectableCell) { 34 | ((SelectableCell) cell).setSelectionManager(selectionManager); 35 | } 36 | return cell; 37 | } 38 | 39 | public void toggleSelection(Integer position) { 40 | if (selectedPositions.contains(position)) selectedPositions.remove(position); 41 | else selectedPositions.add(position); 42 | } 43 | 44 | public void setSelection(Integer position, boolean isSelected) { 45 | if (isSelected) selectedPositions.add(position); 46 | else selectedPositions.remove(position); 47 | } 48 | 49 | public void clearSelections(boolean notify) { 50 | if (notify) { 51 | for (int position : selectedPositions) 52 | notifyItemChanged(position); 53 | } 54 | selectedPositions.clear(); 55 | } 56 | 57 | public int getSelectedItemCount() { 58 | return selectedPositions.size(); 59 | } 60 | 61 | public Collection getSelectedPositions() { 62 | return selectedPositions; 63 | } 64 | } -------------------------------------------------------------------------------- /celladapter/src/main/java/io/techery/celladapter/select/mode/MultiSelectionManager.java: -------------------------------------------------------------------------------- 1 | package io.techery.celladapter.select.mode; 2 | 3 | import java.util.Collection; 4 | import java.util.List; 5 | 6 | public class MultiSelectionManager extends SelectionManager { 7 | 8 | @Override 9 | public void toggleSelection(int position) { 10 | selectableCellAdapter.toggleSelection(position); 11 | selectableCellAdapter.notifyItemChanged(position); 12 | } 13 | 14 | @Override 15 | public void setSelection(int position, boolean isSelected) { 16 | selectableCellAdapter.setSelection(position, isSelected); 17 | selectableCellAdapter.notifyItemChanged(position); 18 | } 19 | 20 | @Override 21 | public boolean isSelected(int position) { 22 | return selectableCellAdapter.getSelectedPositions().contains(position); 23 | } 24 | 25 | public Collection getSelectedPositions() { 26 | return selectableCellAdapter.getSelectedPositions(); 27 | } 28 | 29 | public boolean isAllSelected() { 30 | return selectableCellAdapter.getSelectedItemCount() == selectableCellAdapter.getItemCount(); 31 | } 32 | 33 | public void setSelectedPositions(List selectionPositions) { 34 | selectableCellAdapter.clearSelections(false); 35 | for (Integer position : selectionPositions) { 36 | selectableCellAdapter.toggleSelection(position); 37 | } 38 | selectableCellAdapter.notifyDataSetChanged(); 39 | } 40 | 41 | public void setSelectionForAll(boolean isSelected) { 42 | for (int position = 0; position < selectableCellAdapter.getItemCount(); position++) 43 | setSelection(position, isSelected); 44 | } 45 | } -------------------------------------------------------------------------------- /celladapter/src/main/java/io/techery/celladapter/select/mode/SelectionManager.java: -------------------------------------------------------------------------------- 1 | package io.techery.celladapter.select.mode; 2 | 3 | import io.techery.celladapter.select.SelectableCellAdapter; 4 | 5 | public abstract class SelectionManager { 6 | 7 | protected SelectableCellAdapter selectableCellAdapter; 8 | 9 | public final void setAdapter(SelectableCellAdapter selectableCellAdapter) { 10 | this.selectableCellAdapter = selectableCellAdapter; 11 | } 12 | 13 | public abstract void setSelection(int position, boolean isSelected); 14 | 15 | public abstract void toggleSelection(int position); 16 | 17 | public abstract boolean isSelected(int position); 18 | } -------------------------------------------------------------------------------- /celladapter/src/main/java/io/techery/celladapter/select/mode/SingleSelectionManager.java: -------------------------------------------------------------------------------- 1 | package io.techery.celladapter.select.mode; 2 | 3 | public class SingleSelectionManager extends SelectionManager { 4 | 5 | @Override 6 | public void toggleSelection(int position) { 7 | selectableCellAdapter.clearSelections(true); 8 | selectableCellAdapter.toggleSelection(position); 9 | selectableCellAdapter.notifyItemChanged(position); 10 | } 11 | 12 | @Override 13 | public void setSelection(int position, boolean isSelected) { 14 | selectableCellAdapter.clearSelections(true); 15 | selectableCellAdapter.setSelection(position, isSelected); 16 | selectableCellAdapter.notifyItemChanged(position); 17 | } 18 | 19 | @Override 20 | public boolean isSelected(int position) { 21 | return selectableCellAdapter.getSelectedPositions().contains(position); 22 | } 23 | } -------------------------------------------------------------------------------- /celladapter/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Cell Adapter 3 | -------------------------------------------------------------------------------- /celladapter/src/test/java/io/techery/celladapter/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package io.techery.celladapter; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techery/CellAdapter/7dd6b98eb0d5407b1c435f254384c6e07b1f54c1/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'android-apt' 3 | 4 | android { 5 | compileSdkVersion 24 6 | buildToolsVersion "24.0.3" 7 | 8 | defaultConfig { 9 | applicationId "io.techery.sample" 10 | minSdkVersion 15 11 | targetSdkVersion 24 12 | versionCode 4 13 | versionName "1.0" 14 | 15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | compile fileTree(dir: 'libs', include: ['*.jar']) 27 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 28 | exclude group: 'com.android.support', module: 'support-annotations' 29 | }) 30 | testCompile 'junit:junit:4.12' 31 | 32 | compile project(path: ':celladapter') 33 | 34 | compile 'com.android.support:appcompat-v7:24.2.1' 35 | compile 'com.android.support:recyclerview-v7:24.2.1' 36 | 37 | compile 'com.jakewharton:butterknife:8.4.0' 38 | apt 'com.jakewharton:butterknife-compiler:8.4.0' 39 | } 40 | -------------------------------------------------------------------------------- /sample/images/ic_launcher.svg: -------------------------------------------------------------------------------- 1 | 3 | 5 | 6 | 16 | 23 | 26 | 29 | 32 | 35 | 38 | 41 | 45 | 49 | 52 | 55 | 59 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Development\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | 20 | #CellAdapter 21 | -keepclasseswithmembers public class * extends io.techery.celladapter.** { *; } -------------------------------------------------------------------------------- /sample/src/androidTest/java/io/techery/sample/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("io.techery.sample", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/BaseCell.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample; 2 | 3 | import android.view.View; 4 | 5 | import io.techery.celladapter.Cell; 6 | 7 | import butterknife.ButterKnife; 8 | 9 | public abstract class BaseCell> extends Cell { 10 | 11 | public BaseCell(View view) { 12 | super(view); 13 | ButterKnife.bind(this, view); 14 | } 15 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/BaseCellAdapter.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import io.techery.celladapter.CellAdapter; 7 | 8 | import java.util.List; 9 | 10 | public class BaseCellAdapter extends CellAdapter { 11 | 12 | public BaseCellAdapter(Context context) { 13 | super(context); 14 | } 15 | 16 | public void addItem(ITEM item) { 17 | int position = items.size(); 18 | items.add(item); 19 | notifyItemInserted(position); 20 | } 21 | 22 | public void addItem(int position, ITEM item) { 23 | items.add(position, item); 24 | notifyItemInserted(position); 25 | } 26 | 27 | public void addItems(List items) { 28 | if (items != null) { 29 | int position = this.items.size(); 30 | this.items.addAll(items); 31 | notifyItemRangeInserted(position, items.size()); 32 | } 33 | } 34 | 35 | public void addItems(int position, List items) { 36 | if (items != null) { 37 | this.items.addAll(position, items); 38 | notifyItemRangeInserted(position, items.size()); 39 | } 40 | } 41 | 42 | public void moveItem(int fromPosition, int toPosition) { 43 | if (fromPosition == toPosition) { 44 | return; 45 | } 46 | final ITEM item = items.remove(fromPosition); 47 | items.add(toPosition, item); 48 | notifyItemMoved(fromPosition, toPosition); 49 | } 50 | 51 | public void replaceItem(int position, ITEM item) { 52 | items.set(position, item); 53 | notifyItemChanged(position); 54 | } 55 | 56 | public void remove(ITEM item) { 57 | if (item != null) { 58 | int position = items.indexOf(item); 59 | if (position != -1) remove(position); 60 | notifyItemRemoved(position); 61 | } 62 | } 63 | 64 | public void remove(int position) { 65 | if (items.size() > position) { 66 | items.remove(position); 67 | notifyItemRemoved(position); 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/BaseSelectableCell.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample; 2 | 3 | import android.view.View; 4 | 5 | import butterknife.ButterKnife; 6 | import io.techery.celladapter.Cell; 7 | import io.techery.celladapter.select.SelectableCell; 8 | 9 | public abstract class BaseSelectableCell> extends SelectableCell { 10 | 11 | public BaseSelectableCell(View view) { 12 | super(view); 13 | ButterKnife.bind(this, view); 14 | } 15 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/DividerItemDecoration.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.Rect; 7 | import android.graphics.drawable.Drawable; 8 | import android.support.annotation.DrawableRes; 9 | import android.support.v4.content.res.ResourcesCompat; 10 | import android.support.v7.widget.LinearLayoutManager; 11 | import android.support.v7.widget.RecyclerView; 12 | import android.view.View; 13 | 14 | public class DividerItemDecoration extends RecyclerView.ItemDecoration { 15 | 16 | private Drawable mDivider; 17 | 18 | public DividerItemDecoration(Context context) { 19 | final TypedArray a = context.obtainStyledAttributes(new int [] { android.R.attr.listDivider }); 20 | mDivider = a.getDrawable(0); 21 | a.recycle(); 22 | } 23 | 24 | public DividerItemDecoration(Drawable divider) { mDivider = divider; } 25 | 26 | public DividerItemDecoration (Context context, @DrawableRes int drawableId) { 27 | mDivider = ResourcesCompat.getDrawable(context.getResources(), drawableId, null); 28 | } 29 | 30 | @Override 31 | public void getItemOffsets (Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 32 | super.getItemOffsets(outRect, view, parent, state); 33 | if (mDivider == null) return; 34 | if (parent.getChildAdapterPosition(view) < 1) return; 35 | 36 | if (getOrientation(parent) == LinearLayoutManager.VERTICAL) outRect.top = mDivider.getIntrinsicHeight(); 37 | else outRect.left = mDivider.getIntrinsicWidth(); 38 | } 39 | 40 | @Override 41 | public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { 42 | if (mDivider == null) { super.onDrawOver(c, parent, state); return; } 43 | 44 | if (getOrientation(parent) == LinearLayoutManager.VERTICAL) { 45 | final int left = parent.getPaddingLeft(); 46 | final int right = parent.getWidth() - parent.getPaddingRight(); 47 | final int childCount = parent.getChildCount(); 48 | 49 | for (int i=1; i < childCount; i++) { 50 | final View child = parent.getChildAt(i); 51 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); 52 | final int size = mDivider.getIntrinsicHeight(); 53 | final int top = child.getTop() - params.topMargin; 54 | final int bottom = top + size; 55 | mDivider.setBounds(left, top, right, bottom); 56 | mDivider.draw(c); 57 | } 58 | } else { //horizontal 59 | final int top = parent.getPaddingTop(); 60 | final int bottom = parent.getHeight() - parent.getPaddingBottom(); 61 | final int childCount = parent.getChildCount(); 62 | 63 | for (int i=1; i < childCount; i++) { 64 | final View child = parent.getChildAt(i); 65 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); 66 | final int size = mDivider.getIntrinsicWidth(); 67 | final int left = child.getLeft() - params.leftMargin; 68 | final int right = left + size; 69 | mDivider.setBounds(left, top, right, bottom); 70 | mDivider.draw(c); 71 | } 72 | } 73 | } 74 | 75 | private int getOrientation(RecyclerView parent) { 76 | if (parent.getLayoutManager() instanceof LinearLayoutManager) { 77 | LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager(); 78 | return layoutManager.getOrientation(); 79 | } else throw new IllegalStateException("DividerItemDecoration can only be used with a LinearLayoutManager."); 80 | } 81 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/base/BaseSampleActivity.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.base; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.util.Log; 8 | import android.widget.Toast; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | import io.techery.celladapter.CellAdapter; 14 | import io.techery.sample.DividerItemDecoration; 15 | import io.techery.sample.R; 16 | import io.techery.sample.base.cell.AlphaCell; 17 | import io.techery.sample.base.cell.BetaCell; 18 | import io.techery.sample.base.cell.GammaCell; 19 | import io.techery.sample.base.model.AlphaModel; 20 | import io.techery.sample.base.model.BetaModel; 21 | import io.techery.sample.base.model.GammaModel; 22 | 23 | public class BaseSampleActivity extends AppCompatActivity { 24 | 25 | RecyclerView recyclerView; 26 | CellAdapter adapter; 27 | 28 | @Override 29 | protected void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_with_recycler_view); 32 | 33 | recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 34 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 35 | recyclerView.addItemDecoration(new DividerItemDecoration(this)); 36 | 37 | adapter = new CellAdapter(this); 38 | adapter.registerCell(AlphaModel.class, AlphaCell.class, new AlphaCell.Listener() { 39 | @Override 40 | public void onPressOne(AlphaModel model) { 41 | Toast.makeText(BaseSampleActivity.this, String.format("%s\npress button %d", model.getAlpha(), 1), 42 | Toast.LENGTH_SHORT).show(); 43 | } 44 | 45 | @Override 46 | public void onPressTwo(AlphaModel model) { 47 | Toast.makeText(BaseSampleActivity.this, String.format("%s\npress button %d", model.getAlpha(), 2), 48 | Toast.LENGTH_SHORT).show(); 49 | } 50 | 51 | @Override 52 | public void onCellClicked(AlphaModel model) { 53 | Toast.makeText(BaseSampleActivity.this, String.format(model.getAlpha()), 54 | Toast.LENGTH_SHORT).show(); 55 | } 56 | }); 57 | adapter.registerCell(BetaModel.class, BetaCell.class, new BetaCell.Listener() { 58 | @Override 59 | public void onCellClicked(BetaModel model) { 60 | Toast.makeText(BaseSampleActivity.this, String.format(model.getBeta()), 61 | Toast.LENGTH_SHORT).show(); 62 | } 63 | }); 64 | adapter.registerCell(GammaModel.class, GammaCell.class, new GammaCell.Listener() { 65 | @Override 66 | public void onCellClicked(GammaModel model) { 67 | Log.d("CellAdapter", model.getGamma()); 68 | } 69 | }); 70 | recyclerView.setAdapter(adapter); 71 | 72 | List items = new ArrayList(); 73 | for (int i = 0; i <= 33; i++) { 74 | items.add(new AlphaModel(String.format("AlphaModel %d", i))); 75 | items.add(new BetaModel(String.format("BetaModel %d", i))); 76 | items.add(new GammaModel(String.format("GammaModel %d", i))); 77 | } 78 | 79 | adapter.setItems(items); 80 | adapter.notifyDataSetChanged(); 81 | } 82 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/base/cell/AlphaCell.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.base.cell; 2 | 3 | import android.view.View; 4 | import android.widget.TextView; 5 | 6 | import butterknife.BindView; 7 | import butterknife.OnClick; 8 | import io.techery.celladapter.Cell; 9 | import io.techery.celladapter.Layout; 10 | import io.techery.sample.BaseCell; 11 | import io.techery.sample.R; 12 | import io.techery.sample.base.model.AlphaModel; 13 | 14 | @Layout(R.layout.item_base_alpha) 15 | public class AlphaCell extends BaseCell { 16 | 17 | @BindView(R.id.tv_alpha) TextView textView; 18 | 19 | public AlphaCell(View view) { 20 | super(view); 21 | } 22 | 23 | @Override 24 | protected void syncUiWithItem() { 25 | textView.setText(getItem().getAlpha()); 26 | } 27 | 28 | @Override 29 | public void prepareForReuse() { 30 | } 31 | 32 | @OnClick(R.id.btn_one_press) 33 | public void onPressOne() { 34 | if (getListener() != null) getListener().onPressOne(getItem()); 35 | } 36 | 37 | @OnClick(R.id.btn_two_press) 38 | public void onPressTwo() { 39 | if (getListener() != null) getListener().onPressTwo(getItem()); 40 | } 41 | 42 | public interface Listener extends Cell.Listener { 43 | 44 | void onPressOne(AlphaModel model); 45 | 46 | void onPressTwo(AlphaModel model); 47 | } 48 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/base/cell/BetaCell.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.base.cell; 2 | 3 | import android.view.View; 4 | import android.widget.TextView; 5 | 6 | import butterknife.BindView; 7 | import io.techery.celladapter.Cell; 8 | import io.techery.celladapter.Layout; 9 | import io.techery.sample.BaseCell; 10 | import io.techery.sample.R; 11 | import io.techery.sample.base.model.BetaModel; 12 | 13 | @Layout(R.layout.item_base_beta) 14 | public class BetaCell extends BaseCell { 15 | 16 | @BindView(R.id.tv_beta) 17 | TextView textView; 18 | 19 | public BetaCell(View view) { 20 | super(view); 21 | } 22 | 23 | @Override 24 | protected void syncUiWithItem() { 25 | textView.setText(getItem().getBeta()); 26 | } 27 | 28 | @Override 29 | public void prepareForReuse() { 30 | 31 | } 32 | 33 | public interface Listener extends Cell.Listener { 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/base/cell/GammaCell.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.base.cell; 2 | 3 | import android.view.View; 4 | import android.widget.TextView; 5 | 6 | import butterknife.BindView; 7 | import io.techery.celladapter.Cell; 8 | import io.techery.celladapter.Layout; 9 | import io.techery.sample.BaseCell; 10 | import io.techery.sample.R; 11 | import io.techery.sample.base.model.GammaModel; 12 | 13 | @Layout(R.layout.item_base_gamma) 14 | public class GammaCell extends BaseCell { 15 | 16 | @BindView(R.id.tv_gamma) 17 | TextView textView; 18 | 19 | public GammaCell(View view) { 20 | super(view); 21 | } 22 | 23 | @Override 24 | protected void syncUiWithItem() { 25 | textView.setText(getItem().getGamma()); 26 | } 27 | 28 | @Override 29 | public void prepareForReuse() { 30 | 31 | } 32 | 33 | public interface Listener extends Cell.Listener { 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/base/model/AlphaModel.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.base.model; 2 | 3 | public class AlphaModel { 4 | private String alpha; 5 | 6 | public AlphaModel(String alpha) { 7 | this.alpha = alpha; 8 | } 9 | 10 | public String getAlpha() { 11 | return alpha; 12 | } 13 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/base/model/BetaModel.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.base.model; 2 | 3 | public class BetaModel { 4 | private String beta; 5 | 6 | public BetaModel(String beta) { 7 | this.beta = beta; 8 | } 9 | 10 | public String getBeta() { 11 | return beta; 12 | } 13 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/base/model/GammaModel.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.base.model; 2 | 3 | public class GammaModel { 4 | private String gamma; 5 | 6 | public GammaModel(String gamma) { 7 | this.gamma = gamma; 8 | } 9 | 10 | public String getGamma() { 11 | return gamma; 12 | } 13 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/home/MenuItem.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.home; 2 | 3 | import android.support.annotation.StringRes; 4 | 5 | public class MenuItem { 6 | 7 | @StringRes final int titleId; 8 | final Class clazz; 9 | 10 | public MenuItem(@StringRes int titleId, Class clazz) { 11 | this.titleId = titleId; 12 | this.clazz = clazz; 13 | } 14 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/home/MenuItemCell.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.home; 2 | 3 | import android.view.View; 4 | import android.widget.TextView; 5 | 6 | import butterknife.BindView; 7 | import io.techery.celladapter.Cell; 8 | import io.techery.celladapter.Layout; 9 | import io.techery.sample.BaseCell; 10 | 11 | @Layout(android.R.layout.simple_list_item_1) 12 | public class MenuItemCell extends BaseCell> { 13 | 14 | @BindView(android.R.id.text1) TextView titleTv; 15 | 16 | public MenuItemCell(View view) { 17 | super(view); 18 | } 19 | 20 | @Override 21 | protected void syncUiWithItem() { 22 | titleTv.setText(getItem().titleId); 23 | } 24 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/home/SampleActivity.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.home; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.support.v7.widget.LinearLayoutManager; 8 | import android.support.v7.widget.RecyclerView; 9 | 10 | import java.util.Arrays; 11 | 12 | import io.techery.celladapter.Cell; 13 | import io.techery.celladapter.CellAdapter; 14 | import io.techery.sample.DividerItemDecoration; 15 | import io.techery.sample.R; 16 | import io.techery.sample.base.BaseSampleActivity; 17 | import io.techery.sample.multi.MultiChoiceActivity; 18 | import io.techery.sample.single.SingleChoiceActivity; 19 | 20 | public class SampleActivity extends AppCompatActivity { 21 | 22 | RecyclerView recyclerView; 23 | CellAdapter adapter; 24 | 25 | @Override 26 | protected void onCreate(@Nullable Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_with_recycler_view); 29 | 30 | recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 31 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 32 | recyclerView.addItemDecoration(new DividerItemDecoration(this)); 33 | 34 | adapter = new CellAdapter(this); 35 | adapter.registerCell(MenuItem.class, MenuItemCell.class, new Cell.Listener() { 36 | @Override 37 | public void onCellClicked(MenuItem menuItem) { 38 | startActivity(new Intent(SampleActivity.this, menuItem.clazz)); 39 | } 40 | }); 41 | recyclerView.setAdapter(adapter); 42 | adapter.setItems(Arrays.asList( 43 | new MenuItem(R.string.sample_base, BaseSampleActivity.class), 44 | new MenuItem(R.string.sample_single_choice, SingleChoiceActivity.class), 45 | new MenuItem(R.string.sample_multi_choice, MultiChoiceActivity.class) 46 | )); 47 | adapter.notifyDataSetChanged(); 48 | } 49 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/multi/MultiChoiceActivity.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.multi; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | import io.techery.celladapter.Cell; 13 | import io.techery.celladapter.select.SelectableCellAdapter; 14 | import io.techery.celladapter.select.mode.MultiSelectionManager; 15 | import io.techery.sample.DividerItemDecoration; 16 | import io.techery.sample.R; 17 | 18 | public class MultiChoiceActivity extends AppCompatActivity { 19 | 20 | RecyclerView recyclerView; 21 | SelectableCellAdapter adapter; 22 | 23 | @Override 24 | protected void onCreate(@Nullable Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | setContentView(R.layout.activity_with_recycler_view); 27 | 28 | recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 29 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 30 | recyclerView.addItemDecoration(new DividerItemDecoration(this)); 31 | 32 | MultiSelectionManager multiSelectionManager = new MultiSelectionManager(); 33 | adapter = new SelectableCellAdapter(this, multiSelectionManager); 34 | adapter.registerCell(MultiChoiceModel.class, MultiChoiceCell.class, new Cell.Listener() { 35 | @Override 36 | public void onCellClicked(MultiChoiceModel multiChoiceModel) { 37 | 38 | } 39 | }); 40 | recyclerView.setAdapter(adapter); 41 | 42 | List items = new ArrayList(); 43 | for (int i = 0; i <= 33; i++) { 44 | items.add(new MultiChoiceModel(String.format("Multi select %d", i))); 45 | } 46 | 47 | adapter.setItems(items); 48 | adapter.notifyDataSetChanged(); 49 | } 50 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/multi/MultiChoiceCell.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.multi; 2 | 3 | import android.view.View; 4 | import android.widget.CheckBox; 5 | 6 | import butterknife.BindView; 7 | import butterknife.OnClick; 8 | import io.techery.celladapter.Cell; 9 | import io.techery.celladapter.Layout; 10 | import io.techery.sample.BaseSelectableCell; 11 | import io.techery.sample.R; 12 | 13 | @Layout(R.layout.item_multi) 14 | public class MultiChoiceCell extends BaseSelectableCell> { 15 | 16 | @BindView(R.id.cb_multi) CheckBox checkBox; 17 | 18 | public MultiChoiceCell(View view) { 19 | super(view); 20 | } 21 | 22 | @Override 23 | protected void syncUiWithItem() { 24 | checkBox.setText(getItem().multiTitle); 25 | checkBox.setChecked(selectionManager.isSelected(getAdapterPosition())); 26 | } 27 | 28 | @OnClick(R.id.cb_multi) 29 | void checkBoxClicked() { 30 | selectionManager.toggleSelection(getAdapterPosition()); 31 | getListener().onCellClicked(getItem()); 32 | } 33 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/multi/MultiChoiceModel.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.multi; 2 | 3 | public class MultiChoiceModel { 4 | 5 | final String multiTitle; 6 | 7 | public MultiChoiceModel(String multiTitle) { 8 | this.multiTitle = multiTitle; 9 | } 10 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/single/SingleChoiceActivity.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.single; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | import io.techery.celladapter.Cell; 13 | import io.techery.celladapter.select.SelectableCellAdapter; 14 | import io.techery.celladapter.select.mode.SingleSelectionManager; 15 | import io.techery.sample.DividerItemDecoration; 16 | import io.techery.sample.R; 17 | 18 | public class SingleChoiceActivity extends AppCompatActivity { 19 | 20 | RecyclerView recyclerView; 21 | SelectableCellAdapter adapter; 22 | 23 | @Override 24 | protected void onCreate(@Nullable Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | setContentView(R.layout.activity_with_recycler_view); 27 | 28 | recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 29 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 30 | recyclerView.addItemDecoration(new DividerItemDecoration(this)); 31 | 32 | SingleSelectionManager singleSelectionManager = new SingleSelectionManager(); 33 | adapter = new SelectableCellAdapter(this, singleSelectionManager); 34 | adapter.registerCell(SingleChoiceModel.class, SingleChoiceCell.class, new Cell.Listener() { 35 | @Override 36 | public void onCellClicked(SingleChoiceModel singleChoiceModel) { 37 | 38 | } 39 | }); 40 | recyclerView.setAdapter(adapter); 41 | 42 | List items = new ArrayList(); 43 | for (int i = 0; i <= 33; i++) { 44 | items.add(new SingleChoiceModel(String.format("Single select %d", i))); 45 | } 46 | 47 | adapter.setItems(items); 48 | adapter.notifyDataSetChanged(); 49 | } 50 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/single/SingleChoiceCell.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.single; 2 | 3 | import android.view.View; 4 | import android.widget.RadioButton; 5 | 6 | import butterknife.BindView; 7 | import butterknife.OnClick; 8 | import io.techery.celladapter.Cell; 9 | import io.techery.celladapter.Layout; 10 | import io.techery.celladapter.select.mode.SelectionManager; 11 | import io.techery.sample.BaseSelectableCell; 12 | import io.techery.sample.R; 13 | 14 | @Layout(R.layout.item_single) 15 | public class SingleChoiceCell extends BaseSelectableCell> { 16 | 17 | @BindView(R.id.rb_single) RadioButton radioButton; 18 | 19 | public SingleChoiceCell(View view) { 20 | super(view); 21 | } 22 | 23 | @Override 24 | protected void syncUiWithItem() { 25 | radioButton.setText(getItem().singleTitle); 26 | radioButton.setChecked(selectionManager.isSelected(getAdapterPosition())); 27 | } 28 | 29 | @Override 30 | public void setSelectionManager(SelectionManager selectionManager) { 31 | this.selectionManager = selectionManager; 32 | } 33 | 34 | @OnClick(R.id.rb_single) 35 | void radioButtonClicked() { 36 | selectionManager.toggleSelection(getAdapterPosition()); 37 | getListener().onCellClicked(getItem()); 38 | } 39 | } -------------------------------------------------------------------------------- /sample/src/main/java/io/techery/sample/single/SingleChoiceModel.java: -------------------------------------------------------------------------------- 1 | package io.techery.sample.single; 2 | 3 | public class SingleChoiceModel { 4 | 5 | final String singleTitle; 6 | 7 | public SingleChoiceModel(String singleTitle) { 8 | this.singleTitle = singleTitle; 9 | } 10 | } -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_with_recycler_view.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/item_base_alpha.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 19 |