Skip to content

Commit

Permalink
Finish grid sample
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensousa committed Jun 17, 2017
1 parent 72e3626 commit f933db6
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".GridActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.github.rubensousa.recyclerviewsnap;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;

import com.github.rubensousa.gravitysnaphelper.GravitySnapHelper;

import java.util.ArrayList;
import java.util.List;


public class GridActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

Adapter adapter = new Adapter(false, false, getApps());

mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(adapter);
new GravitySnapHelper(Gravity.TOP).attachToRecyclerView(mRecyclerView);
}

private List<App> getApps() {
List<App> apps = new ArrayList<>();
apps.add(new App("Google+", R.drawable.ic_google_48dp, 4.6f));
apps.add(new App("Gmail", R.drawable.ic_gmail_48dp, 4.8f));
apps.add(new App("Inbox", R.drawable.ic_inbox_48dp, 4.5f));
apps.add(new App("Google Keep", R.drawable.ic_keep_48dp, 4.2f));
apps.add(new App("Google Drive", R.drawable.ic_drive_48dp, 4.6f));
apps.add(new App("Hangouts", R.drawable.ic_hangouts_48dp, 3.9f));
apps.add(new App("Google Photos", R.drawable.ic_photos_48dp, 4.6f));
apps.add(new App("Messenger", R.drawable.ic_messenger_48dp, 4.2f));
apps.add(new App("Sheets", R.drawable.ic_sheets_48dp, 4.2f));
apps.add(new App("Slides", R.drawable.ic_slides_48dp, 4.2f));
apps.add(new App("Docs", R.drawable.ic_docs_48dp, 4.2f));
apps.add(new App("Google+", R.drawable.ic_google_48dp, 4.6f));
apps.add(new App("Gmail", R.drawable.ic_gmail_48dp, 4.8f));
apps.add(new App("Inbox", R.drawable.ic_inbox_48dp, 4.5f));
apps.add(new App("Google Keep", R.drawable.ic_keep_48dp, 4.2f));
apps.add(new App("Google Drive", R.drawable.ic_drive_48dp, 4.6f));
apps.add(new App("Hangouts", R.drawable.ic_hangouts_48dp, 3.9f));
apps.add(new App("Google Photos", R.drawable.ic_photos_48dp, 4.6f));
apps.add(new App("Messenger", R.drawable.ic_messenger_48dp, 4.2f));
apps.add(new App("Sheets", R.drawable.ic_sheets_48dp, 4.2f));
apps.add(new App("Slides", R.drawable.ic_slides_48dp, 4.2f));
apps.add(new App("Docs", R.drawable.ic_docs_48dp, 4.2f));
return apps;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.github.rubensousa.recyclerviewsnap;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;

import com.github.rubensousa.gravitysnaphelper.GravitySnapHelper;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -76,6 +80,17 @@ private List<App> getApps() {
apps.add(new App("Sheets", R.drawable.ic_sheets_48dp, 4.2f));
apps.add(new App("Slides", R.drawable.ic_slides_48dp, 4.2f));
apps.add(new App("Docs", R.drawable.ic_docs_48dp, 4.2f));
apps.add(new App("Google+", R.drawable.ic_google_48dp, 4.6f));
apps.add(new App("Gmail", R.drawable.ic_gmail_48dp, 4.8f));
apps.add(new App("Inbox", R.drawable.ic_inbox_48dp, 4.5f));
apps.add(new App("Google Keep", R.drawable.ic_keep_48dp, 4.2f));
apps.add(new App("Google Drive", R.drawable.ic_drive_48dp, 4.6f));
apps.add(new App("Hangouts", R.drawable.ic_hangouts_48dp, 3.9f));
apps.add(new App("Google Photos", R.drawable.ic_photos_48dp, 4.6f));
apps.add(new App("Messenger", R.drawable.ic_messenger_48dp, 4.2f));
apps.add(new App("Sheets", R.drawable.ic_sheets_48dp, 4.2f));
apps.add(new App("Slides", R.drawable.ic_slides_48dp, 4.2f));
apps.add(new App("Docs", R.drawable.ic_docs_48dp, 4.2f));
return apps;
}

Expand All @@ -85,6 +100,8 @@ public boolean onMenuItemClick(MenuItem item) {
mHorizontal = !mHorizontal;
setupAdapter();
item.setTitle(mHorizontal ? "Vertical" : "Horizontal");
} else if (item.getItemId() == R.id.grid) {
startActivity(new Intent(this, GridActivity.class));
}
return false;
}
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/res/layout/activity_grid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">


<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />


<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@string/app_name" />

</android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>
5 changes: 5 additions & 0 deletions app/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
android:id="@+id/layoutType"
android:title="Vertical"
app:showAsAction="always"></item>

<item
android:id="@+id/grid"
android:title="Grid"
app:showAsAction="always"></item>
</menu>
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private View findStartView(RecyclerView.LayoutManager layoutManager,
// to avoid having the last item not completely visible.
boolean endOfList = ((LinearLayoutManager) layoutManager)
.findLastCompletelyVisibleItemPosition()
>= layoutManager.getItemCount() - offset;
== layoutManager.getItemCount() - 1;

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

if (visibleWidth > 0.5f && !startOfList) {
return child;
Expand Down

0 comments on commit f933db6

Please sign in to comment.