/*
* 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 com.owen.tvrecyclerview.example;
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.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class LayoutAdapter2 extends RecyclerView.Adapter<LayoutAdapter2.SimpleViewHolder> {
private static final int COUNT = 50;
private final Context mContext;
private final IRecyclerView 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 LayoutAdapter2(Context context, IRecyclerView 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, final int position) {
holder.title.setText(mItems.get(position).toString());
if(mLayoutId == R.layout.layout_grid2) {
android.support.v7.widget.GridLayoutManager.LayoutParams params = (android.support.v7.widget.GridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
params.width = 200;
params.height = LinearLayout.LayoutParams.MATCH_PARENT;
holder.itemView.setLayoutParams(params);
}
}
@Override
public int getItemCount() {
return mItems.size();
}
public interface OnItemSelectedListenner {
void onSelected(View view, int positin);
}
}