Skip to content

Commit 9af9219

Browse files
committed
Fix the FlowLayout sample
1 parent 41ecd77 commit 9af9219

11 files changed

Lines changed: 233 additions & 52 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ This library is useful for displaying data sets that are repeating, but do not n
3434
# But Why RecyclerView Adapter?
3535
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.
3636

37+
# Currently Created AdapterLayouts
38+
- AdapterLinearLayout
39+
- AdapterFlowLayout (currently in the sample app, can be copied out)
40+
3741
# 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.
42+
It is simple to create your own `ViewGroup` backed by a `RecyclerView.Adapter`. See `AdapterFlowLayout` in the sample app and `AdapterLayoutDelegate` in the library for an example of how to create one.
3943

4044
License
4145
--------

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies {
2323
compile fileTree(include: ['*.jar'], dir: 'libs')
2424
testCompile 'junit:junit:4.12'
2525
compile 'com.android.support:appcompat-v7:23.1.1'
26+
compile 'com.android.support:design:23.1.1'
2627
compile 'com.jakewharton:butterknife:7.0.1'
2728
compile "com.wefika:flowlayout:0.4.1"
2829
compile project(':adapterlayout')

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
android:label="@string/app_name"
99
android:supportsRtl="true"
1010
android:theme="@style/AppTheme">
11-
<activity android:name="com.commit451.adapterlayout.sample.MainActivity">
11+
<activity android:name=".MainActivity">
1212
<intent-filter>
1313
<action android:name="android.intent.action.MAIN" />
1414

1515
<category android:name="android.intent.category.LAUNCHER" />
1616
</intent-filter>
1717
</activity>
18+
19+
<activity android:name=".CustomAdapterLayoutActivity"/>
1820
</application>
1921

2022
</manifest>

app/src/main/java/com/commit451/adapterlayout/sample/AdapterFlowLayout.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package com.commit451.adapterlayout.sample;
22

3-
import android.annotation.TargetApi;
43
import android.content.Context;
54
import android.support.annotation.Nullable;
65
import android.support.v7.widget.RecyclerView;
76
import android.util.AttributeSet;
87
import android.widget.Adapter;
9-
import android.widget.LinearLayout;
108

119
import com.commit451.adapterlayout.AdapterLayoutDelegate;
10+
import com.wefika.flowlayout.FlowLayout;
1211

1312
/**
14-
* LinearLayout with {@link Adapter} support. See {@link AdapterLayoutDelegate} for
15-
* the good bits, and follow the convention here to create your own {@link android.support.v7.widget.RecyclerView.Adapter}
16-
* backed {@link android.view.ViewGroup}
13+
* {@link com.wefika.flowlayout.FlowLayout} with {@link Adapter} support.
1714
*/
18-
public class AdapterFlowLayout extends LinearLayout {
15+
public class AdapterFlowLayout extends FlowLayout {
1916

2017
private AdapterLayoutDelegate mAdapterLayoutDelegate;
2118

@@ -31,11 +28,6 @@ public AdapterFlowLayout(Context context, AttributeSet attrs, int defStyleAttr)
3128
super(context, attrs, defStyleAttr);
3229
}
3330

34-
@TargetApi(21)
35-
public AdapterFlowLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
36-
super(context, attrs, defStyleAttr, defStyleRes);
37-
}
38-
3931
public void setAdapter(RecyclerView.Adapter adapter) {
4032
if (mAdapterLayoutDelegate == null) {
4133
mAdapterLayoutDelegate = new AdapterLayoutDelegate(this);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.commit451.adapterlayout.sample;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.support.v7.widget.Toolbar;
6+
import android.view.View;
7+
import android.widget.Toast;
8+
9+
import java.util.ArrayList;
10+
11+
import butterknife.Bind;
12+
import butterknife.ButterKnife;
13+
import butterknife.OnClick;
14+
15+
public class CustomAdapterLayoutActivity extends AppCompatActivity {
16+
17+
@Bind(R.id.toolbar) Toolbar mToolbar;
18+
@Bind(R.id.adapter_layout) AdapterFlowLayout mAdapterLayout;
19+
CheeseAdapter mAdapter;
20+
21+
@OnClick(R.id.add_cheese)
22+
void onAddCheeseClicked() {
23+
mAdapter.add(Cheeses.getRandomCheese());
24+
}
25+
26+
@OnClick(R.id.remove_cheese)
27+
void onRemoveCheeseClicked() {
28+
mAdapter.removeLast();
29+
}
30+
31+
@OnClick(R.id.remove_middle)
32+
void onRemoveMiddleClicked() {
33+
mAdapter.removeMiddle();
34+
}
35+
36+
@OnClick(R.id.new_adapter)
37+
void onNewAdapterClicked() {
38+
mAdapterLayout.setAdapter(null);
39+
mAdapter = new CheeseAdapter(mListener);
40+
mAdapterLayout.setAdapter(mAdapter);
41+
}
42+
43+
private CheeseAdapter.Listener mListener = new CheeseAdapter.Listener() {
44+
@Override
45+
public void onItemClicked(Cheese cheese) {
46+
Toast.makeText(CustomAdapterLayoutActivity.this, cheese.getName() + " clicked", Toast.LENGTH_SHORT)
47+
.show();
48+
}
49+
};
50+
51+
@Override
52+
protected void onCreate(Bundle savedInstanceState) {
53+
super.onCreate(savedInstanceState);
54+
setContentView(R.layout.activity_custom_adapter_layout);
55+
ButterKnife.bind(this);
56+
mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_24dp);
57+
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
58+
@Override
59+
public void onClick(View v) {
60+
onBackPressed();
61+
}
62+
});
63+
mAdapter = new CheeseAdapter(mListener);
64+
mAdapterLayout.setAdapter(mAdapter);
65+
loadCheeses();
66+
}
67+
68+
private void loadCheeses() {
69+
ArrayList<Cheese> cheeses = new ArrayList<>();
70+
for (int i=0; i<5; i++) {
71+
cheeses.add(Cheeses.getRandomCheese());
72+
}
73+
mAdapter.setData(cheeses);
74+
}
75+
}

app/src/main/java/com/commit451/adapterlayout/sample/MainActivity.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.commit451.adapterlayout.sample;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
45
import android.support.v7.app.AppCompatActivity;
6+
import android.support.v7.widget.Toolbar;
7+
import android.view.MenuItem;
58
import android.widget.Toast;
69

710
import com.commit451.adapterlayout.AdapterLinearLayout;
@@ -12,6 +15,7 @@
1215

1316
public class MainActivity extends AppCompatActivity {
1417

18+
@Bind(R.id.toolbar) Toolbar mToolbar;
1519
@Bind(R.id.adapter_layout) AdapterLinearLayout mAdapterLinearLayout;
1620
CheeseAdapter mAdapter;
1721

@@ -50,6 +54,19 @@ protected void onCreate(Bundle savedInstanceState) {
5054
super.onCreate(savedInstanceState);
5155
setContentView(R.layout.activity_main);
5256
ButterKnife.bind(this);
57+
mToolbar.setTitle(R.string.app_name);
58+
mToolbar.inflateMenu(R.menu.main);
59+
mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
60+
@Override
61+
public boolean onMenuItemClick(MenuItem item) {
62+
switch (item.getItemId()) {
63+
case R.id.action_custom_adapter_layout:
64+
startActivity(new Intent(MainActivity.this, CustomAdapterLayoutActivity.class));
65+
return true;
66+
}
67+
return false;
68+
}
69+
});
5370
mAdapter = new CheeseAdapter(mListener);
5471
mAdapterLinearLayout.setAdapter(mAdapter);
5572
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FFFFFFFF"
8+
android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/>
9+
</vector>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
6+
7+
<android.support.v7.widget.Toolbar
8+
android:id="@+id/toolbar"
9+
android:layout_width="match_parent"
10+
android:layout_height="?attr/actionBarSize"
11+
android:background="?attr/colorPrimary" />
12+
13+
<ScrollView
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content">
16+
17+
<LinearLayout
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content"
20+
android:orientation="vertical">
21+
22+
<com.commit451.adapterlayout.sample.AdapterFlowLayout
23+
android:id="@+id/adapter_layout"
24+
android:layout_width="wrap_content"
25+
android:layout_height="wrap_content" />
26+
27+
<com.wefika.flowlayout.FlowLayout
28+
android:layout_width="match_parent"
29+
android:layout_height="wrap_content">
30+
31+
<Button
32+
android:id="@+id/add_cheese"
33+
android:layout_width="wrap_content"
34+
android:layout_height="wrap_content"
35+
android:text="Add" />
36+
37+
<Button
38+
android:id="@+id/remove_cheese"
39+
android:layout_width="wrap_content"
40+
android:layout_height="wrap_content"
41+
android:text="Remove Last" />
42+
43+
<Button
44+
android:id="@+id/remove_middle"
45+
android:layout_width="wrap_content"
46+
android:layout_height="wrap_content"
47+
android:text="Remove Middle" />
48+
49+
<Button
50+
android:id="@+id/new_adapter"
51+
android:layout_width="wrap_content"
52+
android:layout_height="wrap_content"
53+
android:text="New Adapter" />
54+
55+
</com.wefika.flowlayout.FlowLayout>
56+
</LinearLayout>
57+
</ScrollView>
58+
59+
</LinearLayout>
Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,60 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
4-
android:layout_height="match_parent">
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
56

6-
<LinearLayout
7+
<android.support.v7.widget.Toolbar
8+
android:id="@+id/toolbar"
79
android:layout_width="match_parent"
8-
android:layout_height="wrap_content"
9-
android:orientation="vertical">
10+
android:layout_height="?attr/actionBarSize"
11+
android:background="?attr/colorPrimary" />
1012

11-
<com.commit451.adapterlayout.AdapterLinearLayout
12-
android:id="@+id/adapter_layout"
13-
android:layout_width="wrap_content"
14-
android:layout_height="wrap_content"
15-
android:orientation="vertical"/>
13+
<ScrollView
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content">
1616

17-
<com.wefika.flowlayout.FlowLayout
17+
<LinearLayout
1818
android:layout_width="match_parent"
19-
android:layout_height="wrap_content">
20-
21-
<Button
22-
android:id="@+id/add_cheese"
23-
android:layout_width="wrap_content"
24-
android:layout_height="wrap_content"
25-
android:text="Add" />
26-
27-
<Button
28-
android:id="@+id/remove_cheese"
29-
android:layout_width="wrap_content"
30-
android:layout_height="wrap_content"
31-
android:text="Remove Last" />
32-
33-
<Button
34-
android:id="@+id/remove_middle"
35-
android:layout_width="wrap_content"
36-
android:layout_height="wrap_content"
37-
android:text="Remove Middle" />
19+
android:layout_height="wrap_content"
20+
android:orientation="vertical">
3821

39-
<Button
40-
android:id="@+id/new_adapter"
22+
<com.commit451.adapterlayout.AdapterLinearLayout
23+
android:id="@+id/adapter_layout"
4124
android:layout_width="wrap_content"
4225
android:layout_height="wrap_content"
43-
android:text="New Adapter" />
44-
45-
</com.wefika.flowlayout.FlowLayout>
46-
</LinearLayout>
47-
48-
</ScrollView>
26+
android:orientation="vertical" />
27+
28+
<com.wefika.flowlayout.FlowLayout
29+
android:layout_width="match_parent"
30+
android:layout_height="wrap_content">
31+
32+
<Button
33+
android:id="@+id/add_cheese"
34+
android:layout_width="wrap_content"
35+
android:layout_height="wrap_content"
36+
android:text="Add" />
37+
38+
<Button
39+
android:id="@+id/remove_cheese"
40+
android:layout_width="wrap_content"
41+
android:layout_height="wrap_content"
42+
android:text="Remove Last" />
43+
44+
<Button
45+
android:id="@+id/remove_middle"
46+
android:layout_width="wrap_content"
47+
android:layout_height="wrap_content"
48+
android:text="Remove Middle" />
49+
50+
<Button
51+
android:id="@+id/new_adapter"
52+
android:layout_width="wrap_content"
53+
android:layout_height="wrap_content"
54+
android:text="New Adapter" />
55+
56+
</com.wefika.flowlayout.FlowLayout>
57+
</LinearLayout>
58+
</ScrollView>
59+
60+
</LinearLayout>

app/src/main/res/menu/main.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto">
4+
5+
<item
6+
android:id="@+id/action_custom_adapter_layout"
7+
app:showAsAction="never"
8+
android:title="Custom AdapterLayout" />
9+
10+
</menu>

0 commit comments

Comments
 (0)