Skip to content

Commit a6e4715

Browse files
committed
Update sample app with grid layout toggle button
1 parent 50f259b commit a6e4715

File tree

7 files changed

+46
-7
lines changed

7 files changed

+46
-7
lines changed

sample/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
applicationId "it.xabaras.android.recyclerview.swipedecorator.sample"
77
minSdkVersion 15
88
targetSdkVersion 28
9-
versionCode 1
10-
versionName "1.0"
9+
versionCode 2
10+
versionName "1.1"
1111
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1212
}
1313
buildTypes {

sample/src/main/java/it/xabaras/android/recyclerview/swipedecorator/sample/MainActivity.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.support.v7.app.AppCompatActivity;
88
import android.os.Bundle;
99
import android.support.v7.widget.DividerItemDecoration;
10+
import android.support.v7.widget.GridLayoutManager;
1011
import android.support.v7.widget.LinearLayoutManager;
1112
import android.support.v7.widget.RecyclerView;
1213
import android.support.v7.widget.helper.ItemTouchHelper;
@@ -21,13 +22,16 @@
2122
public class MainActivity extends AppCompatActivity {
2223

2324
private SampleRecyclerViewAdapter mAdapter;
25+
private MenuItem actionToggleLayout;
26+
private boolean isLinear = true;
27+
private RecyclerView recyclerView;
2428

2529
@Override
2630
protected void onCreate(Bundle savedInstanceState) {
2731
super.onCreate(savedInstanceState);
2832
setContentView(R.layout.activity_main);
2933

30-
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
34+
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
3135
// Set a layout manager
3236
recyclerView.setLayoutManager(new LinearLayoutManager(this));
3337
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
@@ -89,15 +93,35 @@ public void onChildDraw (Canvas c, RecyclerView recyclerView, RecyclerView.ViewH
8993
public boolean onCreateOptionsMenu(Menu menu) {
9094
MenuInflater inflater = getMenuInflater();
9195
inflater.inflate(R.menu.main, menu);
96+
97+
actionToggleLayout = menu.findItem(R.id.actionToggleLayout);
98+
9299
return true;
93100
}
94101

95102
@Override
96103
public boolean onOptionsItemSelected(MenuItem item) {
97104
try {
98-
if ( item.getItemId() == R.id.actionRefresh ) {
99-
mAdapter.reloadItems();
100-
mAdapter.notifyDataSetChanged();
105+
switch (item.getItemId()) {
106+
case R.id.actionRefresh: {
107+
mAdapter.reloadItems();
108+
mAdapter.notifyDataSetChanged();
109+
break;
110+
}
111+
case R.id.actionToggleLayout: {
112+
if ( isLinear ) {
113+
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
114+
actionToggleLayout.setIcon(R.drawable.ic_list_white_24dp);
115+
isLinear = false;
116+
} else {
117+
recyclerView.setLayoutManager(new LinearLayoutManager(this));
118+
actionToggleLayout.setIcon(R.drawable.ic_grid_on_white_24dp);
119+
isLinear = true;
120+
}
121+
break;
122+
}
123+
default:
124+
break;
101125
}
102126
} catch(Exception e) {
103127
Log.e("MainActivity", e.getMessage());
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#FFFFFF"
2+
android:viewportHeight="24.0" android:viewportWidth="24.0"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="#FF000000" android:pathData="M20,2L4,2c-1.1,0 -2,0.9 -2,2v16c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM8,20L4,20v-4h4v4zM8,14L4,14v-4h4v4zM8,8L4,8L4,4h4v4zM14,20h-4v-4h4v4zM14,14h-4v-4h4v4zM14,8h-4L10,4h4v4zM20,20h-4v-4h4v4zM20,14h-4v-4h4v4zM20,8h-4L16,4h4v4z"/>
5+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#FFFFFF"
2+
android:viewportHeight="24.0" android:viewportWidth="24.0"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="#FF000000" android:pathData="M3,13h2v-2L3,11v2zM3,17h2v-2L3,15v2zM3,9h2L5,7L3,7v2zM7,13h14v-2L7,11v2zM7,17h14v-2L7,15v2zM7,7v2h14L21,7L7,7z"/>
5+
</vector>

sample/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
43
xmlns:tools="http://schemas.android.com/tools"
54
android:layout_width="match_parent"
65
android:layout_height="match_parent"

sample/src/main/res/menu/main.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<menu xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto">
4+
<item
5+
android:id="@+id/actionToggleLayout"
6+
android:title="@string/action_toggle"
7+
app:showAsAction="always"
8+
android:icon="@drawable/ic_grid_on_white_24dp"/>
49
<item
510
android:id="@+id/actionRefresh"
611
android:title="@string/action_refresh"

sample/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<string name="action_archive">Archive</string>
44
<string name="action_delete">Delete</string>
55
<string name="action_refresh">Refresh</string>
6+
<string name="action_toggle">Toggle Layout Manager</string>
67
</resources>

0 commit comments

Comments
 (0)