Skip to content

Commit 2b17a2b

Browse files
committed
💯
0 parents  commit 2b17a2b

44 files changed

Lines changed: 1168 additions & 0 deletions

Some content is hidden

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

.gitignore

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Created by https://www.gitignore.io
2+
3+
### Android ###
4+
5+
# Files for the Dalvik VM
6+
*.dex
7+
8+
# Java class files
9+
*.class
10+
11+
# Generated files
12+
bin/
13+
gen/
14+
15+
# Gradle files
16+
.gradle/
17+
build/
18+
19+
# Local configuration file (sdk path, etc)
20+
local.properties
21+
22+
# Proguard folder generated by Eclipse
23+
proguard/
24+
25+
# Log Files
26+
*.log
27+
28+
29+
### Intellij ###
30+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
31+
32+
*.iml
33+
34+
## Directory-based project format:
35+
.idea/
36+
# if you remove the above rule, at least ignore the following:
37+
38+
# User-specific stuff:
39+
# .idea/workspace.xml
40+
# .idea/tasks.xml
41+
# .idea/dictionaries
42+
43+
# Sensitive or high-churn files:
44+
# .idea/dataSources.ids
45+
# .idea/dataSources.xml
46+
# .idea/sqlDataSources.xml
47+
# .idea/dynamic.xml
48+
# .idea/uiDesigner.xml
49+
50+
# Gradle:
51+
# .idea/gradle.xml
52+
# .idea/libraries
53+
54+
# Mongo Explorer plugin:
55+
# .idea/mongoSettings.xml
56+
57+
## File-based project format:
58+
*.ipr
59+
*.iws
60+
61+
## Plugin-specific files:
62+
63+
# IntelliJ
64+
out/
65+
66+
# mpeltonen/sbt-idea plugin
67+
.idea_modules/
68+
69+
# JIRA plugin
70+
atlassian-ide-plugin.xml
71+
72+
# Crashlytics plugin (for Android Studio and IntelliJ)
73+
com_crashlytics_export_strings.xml
74+
75+
# Ignore Gradle GUI config
76+
gradle-app.setting
77+
78+
# Mobile Tools for Java (J2ME)
79+
.mtj.tmp/
80+
81+
# Package Files #
82+
*.war
83+
*.ear
84+
85+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
86+
hs_err_pid*
87+
88+
*.DS_Store

.travis.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: android
2+
android:
3+
components:
4+
- platform-tools
5+
- tools
6+
7+
# The BuildTools version used by your project
8+
- build-tools-23.0.2
9+
10+
# The SDK version used to compile your project
11+
- android-23
12+
13+
# Additional components
14+
- extra-android-m2repository
15+
16+
before_script:
17+
- chmod +x gradlew
18+
19+
script: "./gradlew build"

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# AdapterLayout
2+
ViewGroup backed by RecyclerView.Adapter = magic
3+
4+
[![Build Status](https://travis-ci.org/Commit451/AdapterLayout.svg?branch=master)](https://travis-ci.org/Commit451/AdapterLayout)
5+
6+
# Gradle Dependency
7+
Easily reference the library in your Android projects using this dependency in your module's `build.gradle` file:
8+
9+
```Gradle
10+
dependencies {
11+
compile 'com.commit451:adapterlayout:1.0.0'
12+
}
13+
```
14+
15+
# Usage
16+
See the sample project for a full sample.
17+
```java
18+
//CheeseAdapter is a RecyclerView adapter
19+
CheeseAdapter cheeseAdapter = new CheeseAdapter();
20+
adapterLinearLayout adapterLinearLayout =
21+
adapterLinearLayout.setAdapter(cheeseAdapter);
22+
```
23+
Later, when you get your data set:
24+
```java
25+
ArrayList<Cheese> cheeses = loadCheeses();
26+
//addAll is a method in our custom RecyclerView.Adapter
27+
cheeseAdapter.addAll(cheeses);
28+
```
29+
At this point, the data will be added to the `LinearLayout` so long as your `RecyclerView.Adapter` works correctly. `onCreateViewHolder` and `onBindViewHolder` will be called as needed.
30+
31+
# But... Why?
32+
This library is useful for displaying data sets that are repeating, but do not need to be recycled as they would be in a `RecyclerView`. This could be the case if you wanted to display a list of items within a `ScrollView`, or wanted to use a custom `ViewGroup` that did not extend `RecyclerView`.
33+
34+
# But Why RecyclerView Adapter?
35+
Most developers should be using `RecyclerView` instead of `ListView` and should be familiar with creating a `RecyclerView.Adapter`. `RecyclerView.Adapter` also enforces the use of `ViewHolder`s which is better for performance. RecyclerView's adapter also allows for better responses to data structure changes via `notifyItemInserted`, `notifyItemRemoved` etc.
36+
37+
# Creating Your Own AdapterLayout
38+
It is simple to create your own `ViewGroup` backed by a `RecyclerView.Adapter`. See `AdapterLinearLayout` and `AdapterLayoutDelegate` for an example of how to create one.
39+
40+
License
41+
--------
42+
43+
Copyright 2016 Commit 451
44+
45+
Licensed under the Apache License, Version 2.0 (the "License");
46+
you may not use this file except in compliance with the License.
47+
You may obtain a copy of the License at
48+
49+
http://www.apache.org/licenses/LICENSE-2.0
50+
51+
Unless required by applicable law or agreed to in writing, software
52+
distributed under the License is distributed on an "AS IS" BASIS,
53+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
54+
See the License for the specific language governing permissions and
55+
limitations under the License.

adapterlayout/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

adapterlayout/build.gradle

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 23
5+
buildToolsVersion "23.0.2"
6+
7+
defaultConfig {
8+
minSdkVersion 15
9+
targetSdkVersion 23
10+
versionCode 1
11+
versionName "1.0"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
compile fileTree(include: ['*.jar'], dir: 'libs')
23+
compile 'com.android.support:recyclerview-v7:23.1.1'
24+
}

adapterlayout/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in C:\Users\Jawn\Develop\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+
#}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<manifest package="com.commit451.adapterlayout">
2+
3+
<application />
4+
5+
</manifest>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.commit451.adapterlayout;
2+
3+
import android.support.v7.widget.RecyclerView;
4+
import android.view.View;
5+
import android.view.ViewGroup;
6+
7+
8+
/**
9+
* Does all the hard to work to map a {@link android.support.v7.widget.RecyclerView.Adapter} to a
10+
* {@link ViewGroup}. See {@link AdapterLinearLayout} for an example on how to create your own
11+
*/
12+
public class AdapterLayoutDelegate {
13+
14+
private RecyclerView.Adapter mAdapter;
15+
private ViewGroup mViewGroup;
16+
17+
/**
18+
* Checks for if the data changes
19+
*/
20+
private RecyclerView.AdapterDataObserver mObserver = new RecyclerView.AdapterDataObserver() {
21+
@Override
22+
public void onChanged() {
23+
super.onChanged();
24+
updateViews();
25+
}
26+
};
27+
28+
/**
29+
* Create a new delegate, acting as the bridge between the adapter and the ViewGroup
30+
* @param viewGroup the ViewGroup which will have views added and removed from
31+
*/
32+
public AdapterLayoutDelegate(ViewGroup viewGroup) {
33+
mViewGroup = viewGroup;
34+
}
35+
36+
/**
37+
* Set the adapter which will add and remove views from this layout
38+
* @param adapter the adapter
39+
*/
40+
public void setAdapter(RecyclerView.Adapter adapter) {
41+
if (mAdapter != null) {
42+
try {
43+
mAdapter.unregisterAdapterDataObserver(mObserver);
44+
} catch (Exception ignored) {}
45+
}
46+
47+
mAdapter = adapter;
48+
mAdapter.registerAdapterDataObserver(mObserver);
49+
updateViews();
50+
}
51+
52+
/**
53+
* Updates all the views to match the dataset changing
54+
*/
55+
private void updateViews() {
56+
for (int i = 0; i < mAdapter.getItemCount() || i < mViewGroup.getChildCount(); i++) {
57+
58+
//Within the bounds of the dataset
59+
if (i < mAdapter.getItemCount()) {
60+
int viewType = mAdapter.getItemViewType(i);
61+
//This means the view could already exist
62+
if (i < mViewGroup.getChildCount()) {
63+
View child = mViewGroup.getChildAt(i);
64+
Integer savedViewType = (Integer) child.getTag(R.id.adapter_layout_list_view_type);
65+
RecyclerView.ViewHolder savedViewHolder = (RecyclerView.ViewHolder) child.getTag(R.id.adapter_layout_list_holder);
66+
67+
if (savedViewType != null && savedViewType == viewType && savedViewHolder != null) {
68+
mAdapter.onBindViewHolder(savedViewHolder, i);
69+
} else {
70+
RecyclerView.ViewHolder viewHolder = mAdapter.onCreateViewHolder(mViewGroup, viewType);
71+
viewHolder.itemView.setTag(R.id.adapter_layout_list_holder, viewHolder);
72+
viewHolder.itemView.setTag(R.id.adapter_layout_list_view_type, viewType);
73+
mAdapter.onBindViewHolder(viewHolder, i);
74+
mViewGroup.addView(viewHolder.itemView, i);
75+
mViewGroup.removeView(child);
76+
}
77+
} else {
78+
//Creating a brand new view
79+
RecyclerView.ViewHolder viewHolder = mAdapter.onCreateViewHolder(mViewGroup, viewType);
80+
viewHolder.itemView.setTag(R.id.adapter_layout_list_holder, viewHolder);
81+
viewHolder.itemView.setTag(R.id.adapter_layout_list_view_type, viewType);
82+
mAdapter.onBindViewHolder(viewHolder, i);
83+
mViewGroup.addView(viewHolder.itemView);
84+
}
85+
} else {
86+
//Outside the bounds of the dataset, so remove it
87+
if (i < mViewGroup.getChildCount()) {
88+
View child = mViewGroup.getChildAt(i);
89+
mViewGroup.removeView(child);
90+
}
91+
}
92+
}
93+
}
94+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.commit451.adapterlayout;
2+
3+
import android.annotation.TargetApi;
4+
import android.content.Context;
5+
import android.support.v7.widget.RecyclerView;
6+
import android.util.AttributeSet;
7+
import android.widget.Adapter;
8+
import android.widget.LinearLayout;
9+
10+
/**
11+
* LinearLayout with {@link Adapter} support. See {@link AdapterLayoutDelegate} for
12+
* the good bits, and follow the convention here to create your own {@link android.support.v7.widget.RecyclerView.Adapter}
13+
* backed {@link android.view.ViewGroup}
14+
*/
15+
public class AdapterLinearLayout extends LinearLayout {
16+
17+
private AdapterLayoutDelegate mAdapterLayoutDelegate;
18+
19+
public AdapterLinearLayout(Context context) {
20+
super(context);
21+
init();
22+
}
23+
24+
public AdapterLinearLayout(Context context, AttributeSet attrs) {
25+
super(context, attrs);
26+
init();
27+
}
28+
29+
public AdapterLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
30+
super(context, attrs, defStyleAttr);
31+
init();
32+
}
33+
34+
@TargetApi(21)
35+
public AdapterLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
36+
super(context, attrs, defStyleAttr, defStyleRes);
37+
init();
38+
}
39+
40+
private void init() {
41+
mAdapterLayoutDelegate = new AdapterLayoutDelegate(this);
42+
}
43+
44+
public void setAdapter(RecyclerView.Adapter adapter) {
45+
mAdapterLayoutDelegate.setAdapter(adapter);
46+
}
47+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<item name="adapter_layout_list_holder" type="id" />
4+
<item name="adapter_layout_list_view_type" type="id"/>
5+
</resources>

0 commit comments

Comments
 (0)