From 1b323a12860124d8ea027ccba2d40c559ea5ee6c Mon Sep 17 00:00:00 2001 From: Max Rovkin Date: Thu, 12 Feb 2015 17:00:13 +0500 Subject: [PATCH] add Item decorator for similar margins in greed layout manager and add examples --- app/app.iml | 1 + app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 2 +- ...MainActivity.java => AdapterActivity.java} | 27 ++++-- .../ExampleAdapter.java | 2 +- app/src/main/res/layout/item.xml | 18 ++-- .../paralaxheader/SpacesItemDecoration.java | 82 +++++++++++++++++++ 7 files changed, 117 insertions(+), 16 deletions(-) rename app/src/main/java/pro/useit/paralaxrecycleradapter/{MainActivity.java => AdapterActivity.java} (67%) create mode 100644 paralaxheader/src/main/java/pro/useit/paralaxheader/SpacesItemDecoration.java diff --git a/app/app.iml b/app/app.iml index 1f5d82f..55306c8 100644 --- a/app/app.iml +++ b/app/app.iml @@ -88,6 +88,7 @@ + diff --git a/app/build.gradle b/app/build.gradle index d05b4e9..dc527ce 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -23,5 +23,6 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile project(':paralaxheader') compile 'com.android.support:appcompat-v7:21.0.3' + compile 'com.android.support:cardview-v7:21.0.3' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 967abce..85cfa9f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,7 +7,7 @@ android:label="@string/app_name" android:theme="@style/AppTheme" > diff --git a/app/src/main/java/pro/useit/paralaxrecycleradapter/MainActivity.java b/app/src/main/java/pro/useit/paralaxrecycleradapter/AdapterActivity.java similarity index 67% rename from app/src/main/java/pro/useit/paralaxrecycleradapter/MainActivity.java rename to app/src/main/java/pro/useit/paralaxrecycleradapter/AdapterActivity.java index 2a02799..9344c88 100644 --- a/app/src/main/java/pro/useit/paralaxrecycleradapter/MainActivity.java +++ b/app/src/main/java/pro/useit/paralaxrecycleradapter/AdapterActivity.java @@ -6,16 +6,17 @@ import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.support.v7.widget.Toolbar; +import pro.useit.paralaxheader.HeaderGridLayoutManager; import pro.useit.paralaxheader.ParallaxRecyclerAdapter; +import pro.useit.paralaxheader.SpacesItemDecoration; -public class MainActivity extends ActionBarActivity implements ParallaxRecyclerAdapter.OnParallaxEventListener +public class AdapterActivity extends ActionBarActivity implements ParallaxRecyclerAdapter.OnParallaxEventListener { private Toolbar toolbar; private ExampleAdapter adapter; - private boolean abShowed = true; - private int top = 0; + private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) @@ -24,17 +25,29 @@ protected void onCreate(Bundle savedInstanceState) setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); - final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); + recyclerView = (RecyclerView) findViewById(R.id.recyclerView); recyclerView.setHasFixedSize(false); - final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setItemAnimator(new DefaultItemAnimator()); + adapter = new ExampleAdapter(this, recyclerView); adapter.setParallaxListener(this); + + final RecyclerView.LayoutManager layoutManager = getLayoutManager(); + recyclerView.setLayoutManager(layoutManager); + recyclerView.setItemAnimator(new DefaultItemAnimator()); + recyclerView.setAdapter(adapter); onParallaxScroll(0, 0); } + private RecyclerView.LayoutManager getLayoutManager() + { + int spanCount = 3; + final SpacesItemDecoration itemDecoration = new SpacesItemDecoration(10, spanCount); + itemDecoration.setIgnoreFirst(adapter.isEnableHeader()); + recyclerView.addItemDecoration(itemDecoration); + return new HeaderGridLayoutManager(adapter, spanCount, StaggeredGridLayoutManager.VERTICAL, false); + } + @Override public void onParallaxScroll(final float percentage, final float offset) { diff --git a/app/src/main/java/pro/useit/paralaxrecycleradapter/ExampleAdapter.java b/app/src/main/java/pro/useit/paralaxrecycleradapter/ExampleAdapter.java index 14891f6..f6aa967 100644 --- a/app/src/main/java/pro/useit/paralaxrecycleradapter/ExampleAdapter.java +++ b/app/src/main/java/pro/useit/paralaxrecycleradapter/ExampleAdapter.java @@ -56,7 +56,7 @@ protected HeaderHolder onCreateHeaderViewHolder(final ViewGroup parent) @Override protected void onBindMainViewHolder(final ViewHolder holder, final int position) { - holder.textView.setText("item The integral is an important concept in mathematics. Integration is one of the two main operations in calculus, with its inverse, differentiation, being the other. Given a function f of a real variable x and an interval [a, b] of the real line, the definite integral" + (position + 1)); + holder.textView.setText("item " + (position + 1)); } @Override diff --git a/app/src/main/res/layout/item.xml b/app/src/main/res/layout/item.xml index a32f1d0..0a5a92c 100644 --- a/app/src/main/res/layout/item.xml +++ b/app/src/main/res/layout/item.xml @@ -1,13 +1,17 @@ - + - \ No newline at end of file + \ No newline at end of file diff --git a/paralaxheader/src/main/java/pro/useit/paralaxheader/SpacesItemDecoration.java b/paralaxheader/src/main/java/pro/useit/paralaxheader/SpacesItemDecoration.java new file mode 100644 index 0000000..bdbc3d4 --- /dev/null +++ b/paralaxheader/src/main/java/pro/useit/paralaxheader/SpacesItemDecoration.java @@ -0,0 +1,82 @@ +package pro.useit.paralaxheader; + +import android.graphics.Rect; +import android.support.v7.widget.RecyclerView; +import android.view.View; + +/** + * Created UseIT for AdMe + * User: maxrovkin + * Date: 12.02.15 + * Time: 14:31 + */ +public class SpacesItemDecoration extends RecyclerView.ItemDecoration +{ + private int space; + private int spanCount = 1; + private boolean ignoreFirst = false; + private int diffSize = 1; + + public SpacesItemDecoration(int space) + { + this.space = space; + } + + public SpacesItemDecoration(final int space, final int spanCount) + { + this.space = space; + this.spanCount = spanCount; + } + + @Override + public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) + { + int targetPosition = parent.getChildPosition(view); + if (ignoreFirst && targetPosition == 0) + return; + + + int positionInColumn = getPositionInColumn(targetPosition + diffSize); + outRect.left = getLeftSpace(positionInColumn); + outRect.right = getRightSpace(positionInColumn); + + //outRect.bottom = space; + outRect.top = space; + } + + public void setIgnoreFirst(final boolean ignoreFirst) + { + this.ignoreFirst = ignoreFirst; + diffSize = ignoreFirst ? 2 : 1; + } + + private int getPositionInColumn(int targetPosition) + { + int mod = targetPosition % spanCount; + if (mod == 0) + return spanCount; + + return mod; + } + + private int getLeftSpace(int positionInColumn) + { + if (positionInColumn == 1) + return space; + + return space / 2; + } + + private int getRightSpace(int positionInColumn) + { + if (positionInColumn == spanCount) + return space; + + return space / 2; + } + + public void setSpanCount(final int spanCount) + { + this.spanCount = spanCount; + } +} \ No newline at end of file