Skip to content
This repository was archived by the owner on Dec 1, 2017. It is now read-only.

set a custom secondary cell dimension from elsewhere #240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion layouts/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ android {

dependencies {
compile project(':core')
compile 'com.android.support:recyclerview-v7:21.0.0'
compile 'com.android.support:recyclerview-v7:22.1.0'
}

apply from: "${rootDir}/gradle/scripts/gradle-mvn-push.gradle"
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ private enum UpdateOp {

private Lanes mLanes;
private Lanes mLanesToRestore;
// external attributes
private int mCustomWidth;
private int mCustomHeight;

private ItemEntries mItemEntries;
private ItemEntries mItemEntriesToRestore;
Expand All @@ -148,6 +151,8 @@ public BaseLayoutManager(Context context, AttributeSet attrs) {

public BaseLayoutManager(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO setCustomWidth()
// TODO setCustomHeight()
}

public BaseLayoutManager(Orientation orientation) {
Expand Down Expand Up @@ -259,6 +264,7 @@ private boolean canUseLanes(Lanes lanes) {

return (lanes.getOrientation() == getOrientation() &&
lanes.getCount() == laneCount &&
lanes.getSecondaryDimension() == getSecondaryDimension() &&
lanes.getLaneSize() == laneSize);
}

Expand All @@ -269,7 +275,7 @@ private boolean ensureLayoutState() {
}

final Lanes oldLanes = mLanes;
mLanes = new Lanes(this, laneCount);
mLanes = new Lanes(this, laneCount, getSecondaryDimension());

requestMoveLayout();

Expand Down Expand Up @@ -421,6 +427,7 @@ public Parcelable onSaveInstanceState() {

state.orientation = getOrientation();
state.laneSize = (mLanes != null ? mLanes.getLaneSize() : 0);
state.secondaryDimension = (mLanes != null ? mLanes.getSecondaryDimension() : 0);
state.itemEntries = mItemEntries;

return state;
Expand All @@ -431,7 +438,7 @@ public void onRestoreInstanceState(Parcelable state) {
final LanedSavedState ss = (LanedSavedState) state;

if (ss.lanes != null && ss.laneSize > 0) {
mLanesToRestore = new Lanes(this, ss.orientation, ss.lanes, ss.laneSize);
mLanesToRestore = new Lanes(this, ss.orientation, ss.lanes, ss.laneSize, ss.secondaryDimension);
mItemEntriesToRestore = ss.itemEntries;
}

Expand All @@ -452,7 +459,7 @@ private int getWidthUsed(View child) {
return 0;
}

final int size = getLanes().getLaneSize() * getLaneSpanForChild(child);
final int size = getLanes().getLaneWidth() * getLaneSpanForChild(child);
return getWidth() - getPaddingLeft() - getPaddingRight() - size;
}

Expand All @@ -461,7 +468,7 @@ private int getHeightUsed(View child) {
return 0;
}

final int size = getLanes().getLaneSize() * getLaneSpanForChild(child);
final int size = getLanes().getLaneHeight() * getLaneSpanForChild(child);
return getHeight() - getPaddingTop() - getPaddingBottom() - size;
}

Expand Down Expand Up @@ -566,10 +573,32 @@ public LayoutParams generateLayoutParams(Context c, AttributeSet attrs) {
abstract void getLaneForPosition(LaneInfo outInfo, int position, Direction direction);
abstract void moveLayoutToPosition(int position, int offset, Recycler recycler, State state);


public void setCustomWidth(int mCustomWidth) {
this.mCustomWidth = mCustomWidth;
}

public void setCustomHeight(int mCustomHeight) {
this.mCustomHeight = mCustomHeight;
}


public int getSecondaryDimension() {
switch (getOrientation()){
case HORIZONTAL:
return mCustomHeight;
case VERTICAL:
return mCustomWidth;
default:
return 0; // not use at all
}
}

protected static class LanedSavedState extends SavedState {
private Orientation orientation;
private Rect[] lanes;
private int laneSize;
private int secondaryDimension;
private ItemEntries itemEntries;

protected LanedSavedState(Parcelable superState) {
Expand All @@ -581,6 +610,7 @@ private LanedSavedState(Parcel in) {

orientation = Orientation.values()[in.readInt()];
laneSize = in.readInt();
secondaryDimension = in.readInt();

final int laneCount = in.readInt();
if (laneCount > 0) {
Expand Down Expand Up @@ -608,6 +638,7 @@ public void writeToParcel(Parcel out, int flags) {

out.writeInt(orientation.ordinal());
out.writeInt(laneSize);
out.writeInt(secondaryDimension);

final int laneCount = (lanes != null ? lanes.length : 0);
out.writeInt(laneCount);
Expand Down
28 changes: 28 additions & 0 deletions layouts/src/main/java/org/lucasr/twowayview/widget/Lanes.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ class Lanes {
private Integer mInnerStart;
private Integer mInnerEnd;

private final int mSecondaryDimension;

public int getLaneWidth() {
return mIsVertical || mSecondaryDimension == 0 ? mLaneSize : mSecondaryDimension;
}

public int getLaneHeight() {
return !mIsVertical || mSecondaryDimension == 0 ? mLaneSize : mSecondaryDimension;
}

public int getSecondaryDimension() {
return mSecondaryDimension;
}

public static class LaneInfo {
public int startLane;
public int anchorLane;
Expand All @@ -56,10 +70,17 @@ public void setUndefined() {
}

public Lanes(BaseLayoutManager layout, Orientation orientation, Rect[] lanes, int laneSize) {
this(layout, orientation, lanes, laneSize, 0);
}

public Lanes(BaseLayoutManager layout, Orientation orientation, Rect[] lanes, int laneSize,
int secondaryDimension
) {
mLayout = layout;
mIsVertical = (orientation == Orientation.VERTICAL);
mLanes = lanes;
mLaneSize = laneSize;
mSecondaryDimension = secondaryDimension;

mSavedLanes = new Rect[mLanes.length];
for (int i = 0; i < mLanes.length; i++) {
Expand All @@ -68,6 +89,12 @@ public Lanes(BaseLayoutManager layout, Orientation orientation, Rect[] lanes, in
}

public Lanes(BaseLayoutManager layout, int laneCount) {
this(layout, laneCount, 0);
}

public Lanes(BaseLayoutManager layout, int laneCount,
int secondaryDimension
) {
mLayout = layout;
mIsVertical = layout.isVertical();

Expand All @@ -79,6 +106,7 @@ public Lanes(BaseLayoutManager layout, int laneCount) {
}

mLaneSize = calculateLaneSize(layout, laneCount);
mSecondaryDimension = secondaryDimension;

final int paddingLeft = layout.getPaddingLeft();
final int paddingTop = layout.getPaddingTop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ public SpannableGridLayoutManager(Orientation orientation, int numColumns, int n
}

private int getChildWidth(int colSpan) {
return getLanes().getLaneSize() * colSpan;
return getLanes().getLaneWidth() * colSpan;
}

private int getChildHeight(int rowSpan) {
return getLanes().getLaneSize() * rowSpan;
return getLanes().getLaneHeight() * rowSpan;
}

private static int getLaneSpan(LayoutParams lp, boolean isVertical) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.lucasr.twowayview.sample;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
Expand All @@ -35,6 +36,7 @@
import org.lucasr.twowayview.ItemClickSupport;
import org.lucasr.twowayview.ItemClickSupport.OnItemClickListener;
import org.lucasr.twowayview.ItemClickSupport.OnItemLongClickListener;
import org.lucasr.twowayview.widget.BaseLayoutManager;
import org.lucasr.twowayview.widget.DividerItemDecoration;
import org.lucasr.twowayview.widget.TwoWayView;

Expand Down Expand Up @@ -84,6 +86,14 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLongClickable(true);

RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
Resources res = getResources();
if (layoutManager instanceof BaseLayoutManager) {
BaseLayoutManager manager = (BaseLayoutManager) layoutManager;
manager.setCustomWidth((int) res.getDimension(R.dimen.abc_text_size_display_2_material));
manager.setCustomHeight((int) res.getDimension(R.dimen.abc_text_size_display_1_material));
};

mPositionText = (TextView) view.getRootView().findViewById(R.id.position);
mCountText = (TextView) view.getRootView().findViewById(R.id.count);

Expand Down Expand Up @@ -122,7 +132,7 @@ public void onScrolled(RecyclerView recyclerView, int i, int i2) {
}
});

final Drawable divider = getResources().getDrawable(R.drawable.divider);
final Drawable divider = res.getDrawable(R.drawable.divider);
mRecyclerView.addItemDecoration(new DividerItemDecoration(divider));

mRecyclerView.setAdapter(new LayoutAdapter(activity, mRecyclerView, mLayoutId));
Expand Down