1e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar/* 2e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar * Copyright (C) 2014 The Android Open Source Project 3e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar * 4e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License"); 5e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar * you may not use this file except in compliance with the License. 6e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar * You may obtain a copy of the License at 7e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar * 8e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar * http://www.apache.org/licenses/LICENSE-2.0 9e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar * 10e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar * Unless required by applicable law or agreed to in writing, software 11e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS, 12e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar * See the License for the specific language governing permissions and 14e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar * limitations under the License. 15e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar */ 16e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 17e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyarpackage com.example.android.supportv7.widget.adapter; 18e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 19e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyarimport android.content.Context; 201244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyarimport android.graphics.Color; 21e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyarimport android.util.TypedValue; 22e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyarimport android.view.ViewGroup; 23e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyarimport android.widget.TextView; 24e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 25def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.recyclerview.widget.RecyclerView; 26def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikas 27e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyarimport java.util.ArrayList; 28e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyarimport java.util.Collections; 29a41c174e52ec211ef950259b274b120a705af438Yigit Boyarimport java.util.List; 30e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 31849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas/** 32849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas * A simple RecyclerView adapter that displays every string passed in a constructor as an item. 33849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas */ 34e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyarpublic class SimpleStringAdapter extends RecyclerView.Adapter<SimpleStringAdapter.ViewHolder> { 35e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 36e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar private int mBackground; 37e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 38a41c174e52ec211ef950259b274b120a705af438Yigit Boyar private List<String> mValues; 39e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 40e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar public static class ViewHolder extends RecyclerView.ViewHolder { 411f30fc8732560027f364ad42bbc3f7db8aace8adYigit Boyar public String mBoundString; 421244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar public TextView mTextView; 43e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 44e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar public ViewHolder(TextView v) { 45e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar super(v); 461244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar mTextView = v; 47e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar } 48e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 49e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar @Override 50e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar public String toString() { 511244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar return super.toString() + " '" + mTextView.getText(); 52e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar } 53e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar } 54e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 551f30fc8732560027f364ad42bbc3f7db8aace8adYigit Boyar public String getValueAt(int position) { 561f30fc8732560027f364ad42bbc3f7db8aace8adYigit Boyar return mValues.get(position); 571f30fc8732560027f364ad42bbc3f7db8aace8adYigit Boyar } 581f30fc8732560027f364ad42bbc3f7db8aace8adYigit Boyar 59e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar public SimpleStringAdapter(Context context, String[] strings) { 60e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar TypedValue val = new TypedValue(); 61e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar if (context.getTheme() != null) { 62e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar context.getTheme().resolveAttribute( 63e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar android.R.attr.selectableItemBackground, val, true); 64e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar } 65e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar mBackground = val.resourceId; 66e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar mValues = new ArrayList<String>(); 67e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar Collections.addAll(mValues, strings); 68e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar } 69e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 70e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar public void swap(int pos1, int pos2) { 71e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar String tmp = mValues.get(pos1); 72e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar mValues.set(pos1, mValues.get(pos2)); 73e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar mValues.set(pos2, tmp); 74e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar notifyItemRemoved(pos1); 75e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar notifyItemInserted(pos2); 76e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar } 77e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 78e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar @Override 79e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar public SimpleStringAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 80e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar final ViewHolder h = new ViewHolder(new TextView(parent.getContext())); 811244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar h.mTextView.setMinimumHeight(128); 821244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar h.mTextView.setPadding(20, 0, 20, 0); 831244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar h.mTextView.setFocusable(true); 841244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar h.mTextView.setBackgroundResource(mBackground); 85849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas RecyclerView.LayoutParams lp = getLayoutParams(); 861244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar h.mTextView.setLayoutParams(lp); 87e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar return h; 88e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar } 89e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 90e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar @Override 91e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar public void onBindViewHolder(ViewHolder holder, int position) { 921f30fc8732560027f364ad42bbc3f7db8aace8adYigit Boyar holder.mBoundString = mValues.get(position); 931244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar holder.mTextView.setText(position + ":" + mValues.get(position)); 941244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar holder.mTextView.setMinHeight((200 + mValues.get(position).length() * 10)); 951244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar holder.mTextView.setBackgroundColor(getBackgroundColor(position)); 961244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar } 971244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar 98849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas 99849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas /** 100849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas * Returns LayoutParams to be used for each item in this adapter. It can be overridden 101849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas * to provide different LayoutParams. 102849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas * @return LayoutParams to be used for each item in this adapter. 103849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas */ 104849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas public RecyclerView.LayoutParams getLayoutParams() { 105849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams( 106849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas ViewGroup.LayoutParams.WRAP_CONTENT, 107849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas ViewGroup.LayoutParams.WRAP_CONTENT); 108849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas lp.leftMargin = 10; 109849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas lp.rightMargin = 5; 110849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas lp.topMargin = 20; 111849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas lp.bottomMargin = 15; 112849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas return lp; 113849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas } 114849542dfdc5e83411c8b959251eb6f2a1556fc9dAurimas Liutikas 1151244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar private int getBackgroundColor(int position) { 1161244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar switch (position % 4) { 1171244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar case 0: return Color.BLACK; 1181244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar case 1: return Color.RED; 1191f30fc8732560027f364ad42bbc3f7db8aace8adYigit Boyar case 2: return Color.DKGRAY; 1201244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar case 3: return Color.BLUE; 1211244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar } 1221244264d8066e64e90fa0b8088ffa50ae4624ca4Yigit Boyar return Color.TRANSPARENT; 123e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar } 124e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar 125e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar @Override 126e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar public int getItemCount() { 127e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar return mValues.size(); 128e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar } 129a41c174e52ec211ef950259b274b120a705af438Yigit Boyar 130a41c174e52ec211ef950259b274b120a705af438Yigit Boyar public List<String> getValues() { 131a41c174e52ec211ef950259b274b120a705af438Yigit Boyar return mValues; 132a41c174e52ec211ef950259b274b120a705af438Yigit Boyar } 133a41c174e52ec211ef950259b274b120a705af438Yigit Boyar 134a41c174e52ec211ef950259b274b120a705af438Yigit Boyar public void setValues(List<String> values) { 135a41c174e52ec211ef950259b274b120a705af438Yigit Boyar mValues = values; 136a41c174e52ec211ef950259b274b120a705af438Yigit Boyar } 137e9f9a04c426e7423db0cd58ec8d2dc3ff2238a30Yigit Boyar} 138