forked from lucasr/twoway-view
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sample] Factor out adapter into separate LayoutAdapter class
- Loading branch information
Showing
2 changed files
with
142 additions
and
117 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
sample/src/main/java/org/lucasr/twowayview/sample/LayoutAdapter.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright (C) 2014 Lucas Rocha | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.lucasr.twowayview.sample; | ||
|
||
import android.content.Context; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
|
||
import org.lucasr.twowayview.TwoWayLayoutManager; | ||
import org.lucasr.twowayview.TwoWayView; | ||
import org.lucasr.twowayview.widget.SpannableGridLayoutManager; | ||
import org.lucasr.twowayview.widget.StaggeredGridLayoutManager; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LayoutAdapter extends RecyclerView.Adapter<LayoutAdapter.SimpleViewHolder> { | ||
private static final int COUNT = 100; | ||
|
||
private final Context mContext; | ||
private final TwoWayView mRecyclerView; | ||
private final List<Integer> mItems; | ||
private final int mLayoutId; | ||
private int mCurrentItemId = 0; | ||
|
||
public static class SimpleViewHolder extends RecyclerView.ViewHolder { | ||
public final TextView title; | ||
|
||
public SimpleViewHolder(View view) { | ||
super(view); | ||
title = (TextView) view.findViewById(R.id.title); | ||
} | ||
} | ||
|
||
public LayoutAdapter(Context context, TwoWayView recyclerView, int layoutId) { | ||
mContext = context; | ||
mItems = new ArrayList<Integer>(COUNT); | ||
for (int i = 0; i < COUNT; i++) { | ||
addItem(i); | ||
} | ||
|
||
mRecyclerView = recyclerView; | ||
mLayoutId = layoutId; | ||
} | ||
|
||
public void addItem(int position) { | ||
final int id = ++mCurrentItemId; | ||
mItems.add(position, id); | ||
notifyItemInserted(position); | ||
} | ||
|
||
public void removeItem(int position) { | ||
mItems.remove(position); | ||
notifyItemRemoved(position); | ||
} | ||
|
||
@Override | ||
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | ||
final View view = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false); | ||
return new SimpleViewHolder(view); | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(SimpleViewHolder holder, int position) { | ||
holder.title.setText(mItems.get(position).toString()); | ||
|
||
boolean isVertical = (mRecyclerView.getOrientation() == TwoWayLayoutManager.Orientation.VERTICAL); | ||
final View itemView = holder.itemView; | ||
|
||
if (mLayoutId == R.layout.layout_staggered_grid) { | ||
final int id; | ||
if (position % 3 == 0) { | ||
id = R.dimen.staggered_child_medium; | ||
} else if (position % 5 == 0) { | ||
id = R.dimen.staggered_child_large; | ||
} else if (position % 7 == 0) { | ||
id = R.dimen.staggered_child_xlarge; | ||
} else { | ||
id = R.dimen.staggered_child_small; | ||
} | ||
|
||
final int span; | ||
if (position == 2) { | ||
span = 2; | ||
} else { | ||
span = 1; | ||
} | ||
|
||
final int size = mContext.getResources().getDimensionPixelSize(id); | ||
|
||
final StaggeredGridLayoutManager.LayoutParams lp = | ||
(StaggeredGridLayoutManager.LayoutParams) itemView.getLayoutParams(); | ||
if (!isVertical && lp.width != id) { | ||
lp.span = span; | ||
lp.width = size; | ||
itemView.setLayoutParams(lp); | ||
} else if (isVertical && lp.height != id) { | ||
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 = (position == 0 || position == 3 ? 2 : 1); | ||
final int span2 = (position == 0 ? 2 : (position == 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); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return mItems.size(); | ||
} | ||
} |
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