Skip to content

Commit

Permalink
add Item decorator for similar margins in greed layout manager and ad…
Browse files Browse the repository at this point in the history
…d examples
  • Loading branch information
Max Rovkin committed Feb 12, 2015
1 parent 087759c commit 1b323a1
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<orderEntry type="library" exported="" name="appcompat-v7-21.0.3" level="project" />
<orderEntry type="library" exported="" name="support-annotations-21.0.3" level="project" />
<orderEntry type="library" exported="" name="support-v4-21.0.3" level="project" />
<orderEntry type="library" exported="" name="cardview-v7-21.0.3" level="project" />
<orderEntry type="module" module-name="paralaxheader" exported="" />
</component>
</module>
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:name=".AdapterActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions app/src/main/res/layout/item.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="2dp"
>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:gravity="center_vertical"
android:layout_gravity="center"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 1b323a1

Please sign in to comment.