Created
December 1, 2014 09:37
-
-
Save kennyk/1bd8143831ef9c5c97eb to your computer and use it in GitHub Desktop.
org.lucasr.twowayview.sample.LayoutAdapter#onBindViewHolder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onBindViewHolder(final SimpleViewHolder holder, int position) { | |
holder.title.setText(mItems.get(position).toString()); | |
boolean isVertical = (mRecyclerView.getOrientation() == TwoWayLayoutManager.Orientation.VERTICAL); | |
final View itemView = holder.itemView; | |
final int itemId = mItems.get(position); | |
if (mLayoutId == R.layout.layout_staggered_grid) { | |
final int dimenId; | |
if (itemId % 3 == 0) { | |
dimenId = R.dimen.staggered_child_medium; | |
} else if (itemId % 5 == 0) { | |
dimenId = R.dimen.staggered_child_large; | |
} else if (itemId % 7 == 0) { | |
dimenId = R.dimen.staggered_child_xlarge; | |
} else { | |
dimenId = R.dimen.staggered_child_small; | |
} | |
final int span; | |
if (itemId == 2) { | |
span = 2; | |
} else { | |
span = 1; | |
} | |
final int size = mContext.getResources().getDimensionPixelSize(dimenId); | |
final StaggeredGridLayoutManager.LayoutParams lp = | |
(StaggeredGridLayoutManager.LayoutParams) itemView.getLayoutParams(); | |
if (!isVertical) { | |
lp.span = span; | |
lp.width = size; | |
itemView.setLayoutParams(lp); | |
} else { | |
lp.span = span; | |
lp.height = size; | |
itemView.setLayoutParams(lp); | |
} | |
} else if (mLayoutId == R.layout.layout_spannable_grid) { | |
final SpannableGridLayoutManager.LayoutParams lp = | |
(SpannableGridLayoutManager.LayoutParams) itemView.getLayoutParams(); | |
final int span1 = (itemId == 0 || itemId == 3 ? 2 : 1); | |
final int span2 = (itemId == 0 ? 2 : (itemId == 3 ? 3 : 1)); | |
final int colSpan = (isVertical ? span2 : span1); | |
final int rowSpan = (isVertical ? span1 : span2); | |
if (lp.rowSpan != rowSpan || lp.colSpan != colSpan) { | |
lp.rowSpan = rowSpan; | |
lp.colSpan = colSpan; | |
itemView.setLayoutParams(lp); | |
} | |
} | |
//Adding this breaks the layout | |
holder.title.postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
holder.title.requestLayout(); | |
} | |
}, | |
150); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment