1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.supportv7.widget; 18 19 import android.graphics.Color; 20 import android.support.v7.widget.DividerItemDecoration; 21 import android.support.v7.widget.LinearLayoutManager; 22 import android.support.v7.widget.RecyclerView; 23 import android.view.LayoutInflater; 24 import android.view.ViewGroup; 25 import android.widget.TextView; 26 27 import com.example.android.supportv7.Cheeses; 28 import com.example.android.supportv7.R; 29 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 33 /** 34 * A sample nested RecyclerView activity. 35 */ 36 public class NestedRecyclerViewActivity extends BaseLayoutManagerActivity<LinearLayoutManager> { 37 @Override 38 protected LinearLayoutManager createLayoutManager() { 39 return new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); 40 } 41 42 @Override 43 protected void onRecyclerViewInit(RecyclerView recyclerView) { 44 recyclerView.addItemDecoration( 45 new DividerItemDecoration(this, mLayoutManager.getOrientation())); 46 } 47 48 static class InnerAdapter extends RecyclerView.Adapter<InnerAdapter.ViewHolder> { 49 public static class ViewHolder extends RecyclerView.ViewHolder { 50 public TextView mTextView; 51 52 public ViewHolder(TextView itemView) { 53 super(itemView); 54 mTextView = itemView; 55 } 56 } 57 58 private String[] mData; 59 60 public InnerAdapter(String[] data) { 61 mData = data; 62 } 63 64 @Override 65 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 66 LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 67 TextView textView = (TextView) inflater.inflate(R.layout.nested_item, parent, false); 68 textView.setMinimumWidth(300); 69 textView.setMinimumHeight(300); 70 return new ViewHolder(textView); 71 } 72 73 @Override 74 public void onBindViewHolder(ViewHolder holder, int position) { 75 holder.mTextView.setText(mData[position]); 76 boolean even = position % 2 == 0; 77 holder.mTextView.setBackgroundColor(even ? Color.TRANSPARENT : 0x2fffffff); 78 } 79 80 @Override 81 public int getItemCount() { 82 return mData.length; 83 } 84 } 85 86 static class OuterAdapter extends RecyclerView.Adapter<OuterAdapter.ViewHolder> { 87 public static class ViewHolder extends RecyclerView.ViewHolder { 88 private final RecyclerView mRecyclerView; 89 public ViewHolder(RecyclerView itemView) { 90 super(itemView); 91 mRecyclerView = itemView; 92 } 93 } 94 95 ArrayList<InnerAdapter> mAdapters = new ArrayList<>(); 96 RecyclerView.RecycledViewPool mSharedPool = new RecyclerView.RecycledViewPool(); 97 98 public OuterAdapter(String[] data) { 99 int currentCharIndex = 0; 100 char currentChar = data[0].charAt(0); 101 for (int i = 1; i <= data.length; i++) { 102 if (i == data.length || data[i].charAt(0) != currentChar) { 103 mAdapters.add(new InnerAdapter( 104 Arrays.copyOfRange(data, currentCharIndex, i - 1))); 105 if (i < data.length) { 106 currentChar = data[i].charAt(0); 107 currentCharIndex = i; 108 } 109 } 110 } 111 } 112 113 @Override 114 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 115 RecyclerView rv = new RecyclerView(parent.getContext()); 116 rv.setLayoutManager(new LinearLayoutManager(parent.getContext(), 117 LinearLayoutManager.HORIZONTAL, false)); 118 rv.setRecycledViewPool(mSharedPool); 119 return new ViewHolder(rv); 120 } 121 122 @Override 123 public void onBindViewHolder(ViewHolder holder, int position) { 124 holder.mRecyclerView.setAdapter(mAdapters.get(position)); 125 } 126 127 @Override 128 public int getItemCount() { 129 return mAdapters.size(); 130 } 131 } 132 133 protected RecyclerView.Adapter createAdapter() { 134 return new OuterAdapter(Cheeses.sCheeseStrings); 135 } 136 } 137