Skip to content

Commit f933db6

Browse files
committed
Finish grid sample
1 parent 72e3626 commit f933db6

File tree

6 files changed

+115
-2
lines changed

6 files changed

+115
-2
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
<category android:name="android.intent.category.LAUNCHER" />
1515
</intent-filter>
1616
</activity>
17+
18+
<activity android:name=".GridActivity" />
1719
</application>
1820

1921
</manifest>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.github.rubensousa.recyclerviewsnap;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.support.v7.widget.GridLayoutManager;
6+
import android.support.v7.widget.RecyclerView;
7+
import android.view.Gravity;
8+
9+
import com.github.rubensousa.gravitysnaphelper.GravitySnapHelper;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
15+
public class GridActivity extends AppCompatActivity {
16+
17+
private RecyclerView mRecyclerView;
18+
19+
@Override
20+
protected void onCreate(Bundle savedInstanceState) {
21+
super.onCreate(savedInstanceState);
22+
setContentView(R.layout.activity_grid);
23+
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
24+
25+
Adapter adapter = new Adapter(false, false, getApps());
26+
27+
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
28+
mRecyclerView.setHasFixedSize(true);
29+
mRecyclerView.setAdapter(adapter);
30+
new GravitySnapHelper(Gravity.TOP).attachToRecyclerView(mRecyclerView);
31+
}
32+
33+
private List<App> getApps() {
34+
List<App> apps = new ArrayList<>();
35+
apps.add(new App("Google+", R.drawable.ic_google_48dp, 4.6f));
36+
apps.add(new App("Gmail", R.drawable.ic_gmail_48dp, 4.8f));
37+
apps.add(new App("Inbox", R.drawable.ic_inbox_48dp, 4.5f));
38+
apps.add(new App("Google Keep", R.drawable.ic_keep_48dp, 4.2f));
39+
apps.add(new App("Google Drive", R.drawable.ic_drive_48dp, 4.6f));
40+
apps.add(new App("Hangouts", R.drawable.ic_hangouts_48dp, 3.9f));
41+
apps.add(new App("Google Photos", R.drawable.ic_photos_48dp, 4.6f));
42+
apps.add(new App("Messenger", R.drawable.ic_messenger_48dp, 4.2f));
43+
apps.add(new App("Sheets", R.drawable.ic_sheets_48dp, 4.2f));
44+
apps.add(new App("Slides", R.drawable.ic_slides_48dp, 4.2f));
45+
apps.add(new App("Docs", R.drawable.ic_docs_48dp, 4.2f));
46+
apps.add(new App("Google+", R.drawable.ic_google_48dp, 4.6f));
47+
apps.add(new App("Gmail", R.drawable.ic_gmail_48dp, 4.8f));
48+
apps.add(new App("Inbox", R.drawable.ic_inbox_48dp, 4.5f));
49+
apps.add(new App("Google Keep", R.drawable.ic_keep_48dp, 4.2f));
50+
apps.add(new App("Google Drive", R.drawable.ic_drive_48dp, 4.6f));
51+
apps.add(new App("Hangouts", R.drawable.ic_hangouts_48dp, 3.9f));
52+
apps.add(new App("Google Photos", R.drawable.ic_photos_48dp, 4.6f));
53+
apps.add(new App("Messenger", R.drawable.ic_messenger_48dp, 4.2f));
54+
apps.add(new App("Sheets", R.drawable.ic_sheets_48dp, 4.2f));
55+
apps.add(new App("Slides", R.drawable.ic_slides_48dp, 4.2f));
56+
apps.add(new App("Docs", R.drawable.ic_docs_48dp, 4.2f));
57+
return apps;
58+
}
59+
}

app/src/main/java/com/github/rubensousa/recyclerviewsnap/MainActivity.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.github.rubensousa.recyclerviewsnap;
22

3+
import android.content.Intent;
34
import android.support.v7.app.AppCompatActivity;
45
import android.os.Bundle;
6+
import android.support.v7.widget.GridLayoutManager;
57
import android.support.v7.widget.LinearLayoutManager;
68
import android.support.v7.widget.RecyclerView;
79
import android.support.v7.widget.Toolbar;
810
import android.view.Gravity;
911
import android.view.MenuItem;
1012

13+
import com.github.rubensousa.gravitysnaphelper.GravitySnapHelper;
14+
1115
import java.util.ArrayList;
1216
import java.util.List;
1317

@@ -76,6 +80,17 @@ private List<App> getApps() {
7680
apps.add(new App("Sheets", R.drawable.ic_sheets_48dp, 4.2f));
7781
apps.add(new App("Slides", R.drawable.ic_slides_48dp, 4.2f));
7882
apps.add(new App("Docs", R.drawable.ic_docs_48dp, 4.2f));
83+
apps.add(new App("Google+", R.drawable.ic_google_48dp, 4.6f));
84+
apps.add(new App("Gmail", R.drawable.ic_gmail_48dp, 4.8f));
85+
apps.add(new App("Inbox", R.drawable.ic_inbox_48dp, 4.5f));
86+
apps.add(new App("Google Keep", R.drawable.ic_keep_48dp, 4.2f));
87+
apps.add(new App("Google Drive", R.drawable.ic_drive_48dp, 4.6f));
88+
apps.add(new App("Hangouts", R.drawable.ic_hangouts_48dp, 3.9f));
89+
apps.add(new App("Google Photos", R.drawable.ic_photos_48dp, 4.6f));
90+
apps.add(new App("Messenger", R.drawable.ic_messenger_48dp, 4.2f));
91+
apps.add(new App("Sheets", R.drawable.ic_sheets_48dp, 4.2f));
92+
apps.add(new App("Slides", R.drawable.ic_slides_48dp, 4.2f));
93+
apps.add(new App("Docs", R.drawable.ic_docs_48dp, 4.2f));
7994
return apps;
8095
}
8196

@@ -85,6 +100,8 @@ public boolean onMenuItemClick(MenuItem item) {
85100
mHorizontal = !mHorizontal;
86101
setupAdapter();
87102
item.setTitle(mHorizontal ? "Vertical" : "Horizontal");
103+
} else if (item.getItemId() == R.id.grid) {
104+
startActivity(new Intent(this, GridActivity.class));
88105
}
89106
return false;
90107
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:fitsSystemWindows="true">
7+
8+
9+
<android.support.v7.widget.RecyclerView
10+
android:id="@+id/recyclerView"
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
14+
15+
16+
<android.support.design.widget.AppBarLayout
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:theme="@style/AppTheme.AppBarOverlay">
20+
21+
<android.support.v7.widget.Toolbar
22+
android:id="@+id/toolbar"
23+
android:layout_width="match_parent"
24+
android:layout_height="?actionBarSize"
25+
app:popupTheme="@style/AppTheme.PopupOverlay"
26+
app:title="@string/app_name" />
27+
28+
</android.support.design.widget.AppBarLayout>
29+
30+
</android.support.design.widget.CoordinatorLayout>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@
55
android:id="@+id/layoutType"
66
android:title="Vertical"
77
app:showAsAction="always"></item>
8+
9+
<item
10+
android:id="@+id/grid"
11+
android:title="Grid"
12+
app:showAsAction="always"></item>
813
</menu>

gravitysnaphelper/src/main/java/com/github/rubensousa/gravitysnaphelper/GravityDelegate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private View findStartView(RecyclerView.LayoutManager layoutManager,
180180
// to avoid having the last item not completely visible.
181181
boolean endOfList = ((LinearLayoutManager) layoutManager)
182182
.findLastCompletelyVisibleItemPosition()
183-
>= layoutManager.getItemCount() - offset;
183+
== layoutManager.getItemCount() - 1;
184184

185185
if (visibleWidth > 0.5f && !endOfList) {
186186
return child;
@@ -228,7 +228,7 @@ private View findEndView(RecyclerView.LayoutManager layoutManager,
228228
// If we're at the start of the list, we shouldn't snap
229229
// to avoid having the first item not completely visible.
230230
boolean startOfList = ((LinearLayoutManager) layoutManager)
231-
.findFirstCompletelyVisibleItemPosition() <= offset - 1;
231+
.findFirstCompletelyVisibleItemPosition() == 0;
232232

233233
if (visibleWidth > 0.5f && !startOfList) {
234234
return child;

0 commit comments

Comments
 (0)