1 /* 2 * Copyright (C) 2014 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.app.Activity; 20 import android.os.Bundle; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.widget.BaseAdapter; 25 import android.widget.CheckBox; 26 import android.widget.EditText; 27 import android.widget.Spinner; 28 import android.widget.TextView; 29 30 import androidx.recyclerview.widget.DefaultItemAnimator; 31 import androidx.recyclerview.widget.LinearLayoutManager; 32 import androidx.recyclerview.widget.RecyclerView; 33 34 import com.example.android.supportv7.Cheeses; 35 import com.example.android.supportv7.R; 36 import com.example.android.supportv7.widget.adapter.SimpleStringAdapter; 37 import com.example.android.supportv7.widget.util.ConfigToggle; 38 import com.example.android.supportv7.widget.util.ConfigViewHolder; 39 40 /** 41 * A simple activity that can be extended to demonstrate LayoutManagers. 42 * <p> 43 * It initializes a sample adapter and a list of configuration options. Extending activities can 44 * define the {@link ConfigToggle} list depending on its functionality. 45 */ 46 abstract public class BaseLayoutManagerActivity<T extends RecyclerView.LayoutManager> 47 extends Activity { 48 49 protected T mLayoutManager; 50 51 protected RecyclerView mRecyclerView; 52 53 private ConfigToggle[] mConfigToggles; 54 55 @Override onCreate(Bundle savedInstanceState)56 protected void onCreate(Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 setContentView(R.layout.activity_base_layout_manager); 59 initToggles(); 60 initRecyclerView(); 61 initSpinner(); 62 } 63 createLayoutManager()64 abstract protected T createLayoutManager(); 65 initRecyclerView()66 private void initRecyclerView() { 67 mRecyclerView = findViewById(R.id.recycler_view); 68 mRecyclerView.setHasFixedSize(true); 69 mLayoutManager = createLayoutManager(); 70 mRecyclerView.setLayoutManager(mLayoutManager); 71 mRecyclerView.setAdapter(createAdapter()); 72 ((DefaultItemAnimator)mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(true); 73 onRecyclerViewInit(mRecyclerView); 74 } 75 onRecyclerViewInit(RecyclerView recyclerView)76 protected void onRecyclerViewInit(RecyclerView recyclerView) { 77 78 } 79 createAdapter()80 protected RecyclerView.Adapter createAdapter() { 81 return new SimpleStringAdapter(this, Cheeses.sCheeseStrings) { 82 @Override 83 public SimpleStringAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 84 int viewType) { 85 final SimpleStringAdapter.ViewHolder vh = super 86 .onCreateViewHolder(parent, viewType); 87 vh.itemView.setOnClickListener(new View.OnClickListener() { 88 @Override 89 public void onClick(View v) { 90 final int pos = vh.getAdapterPosition(); 91 if (pos != RecyclerView.NO_POSITION && pos + 1 < getItemCount()) { 92 swap(pos, pos + 1); 93 } 94 } 95 }); 96 return vh; 97 } 98 }; 99 } 100 101 private void initToggles() { 102 mConfigToggles = createConfigToggles(); 103 RecyclerView configView = findViewById(R.id.config_recycler_view); 104 configView.setAdapter(mConfigAdapter); 105 configView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, 106 false)); 107 configView.setHasFixedSize(true); 108 } 109 110 public void onScrollClicked(View view) { 111 final EditText scrollOffset = findViewById(R.id.scroll_offset); 112 final CheckBox checkBox = findViewById(R.id.enable_smooth_scroll); 113 final Spinner spinner = findViewById(R.id.spinner); 114 115 Integer offset = null; 116 String offsetString = scrollOffset.getText().toString(); 117 try { 118 offset = Integer.parseInt(offsetString); 119 } catch (NumberFormatException ex) { 120 121 } 122 final boolean smooth = checkBox.isChecked(); 123 if (offset == null) { 124 scrollToPosition(smooth, spinner.getSelectedItemPosition()); 125 } else { 126 scrollToPositionWithOffset(smooth, spinner.getSelectedItemPosition(), offset); 127 } 128 } 129 130 private void initSpinner() { 131 final Spinner spinner = findViewById(R.id.spinner); 132 spinner.setAdapter(new BaseAdapter() { 133 @Override 134 public int getCount() { 135 return mRecyclerView.getAdapter().getItemCount(); 136 } 137 138 @Override 139 public Integer getItem(int position) { 140 return position; 141 } 142 143 @Override 144 public long getItemId(int position) { 145 return position; 146 } 147 148 @Override 149 public View getView(int position, View convertView, ViewGroup parent) { 150 if (convertView == null) { 151 convertView = new TextView(parent.getContext()); 152 } 153 ((TextView) convertView).setText(" " + position + " "); 154 return convertView; 155 } 156 }); 157 } 158 159 protected void scrollToPosition(boolean smooth, int position) { 160 if (smooth) { 161 mRecyclerView.smoothScrollToPosition(position); 162 } else { 163 mRecyclerView.scrollToPosition(position); 164 } 165 } 166 167 protected void scrollToPositionWithOffset(boolean smooth, int position, int offset) { 168 scrollToPosition(smooth, position); 169 } 170 171 protected ConfigToggle[] createConfigToggles() { 172 return new ConfigToggle[] {}; 173 } 174 175 private RecyclerView.Adapter mConfigAdapter = new RecyclerView.Adapter<ConfigViewHolder>() { 176 @Override 177 public ConfigViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 178 return new ConfigViewHolder(LayoutInflater.from(parent.getContext()) 179 .inflate(R.layout.config_view_toggle, parent, false)); 180 } 181 182 @Override 183 public void onBindViewHolder(ConfigViewHolder holder, int position) { 184 ConfigToggle toggle = mConfigToggles[position]; 185 holder.bind(toggle); 186 } 187 188 @Override 189 public int getItemCount() { 190 return mConfigToggles.length; 191 } 192 }; 193 } 194