Home | History | Annotate | Download | only in widget
      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 package com.example.android.supportv7.widget;
     17 
     18 import android.view.View;
     19 import android.view.ViewGroup;
     20 
     21 import androidx.core.view.ViewCompat;
     22 import androidx.recyclerview.widget.GridLayoutManager;
     23 import androidx.recyclerview.widget.LinearLayoutManager;
     24 import androidx.recyclerview.widget.RecyclerView;
     25 
     26 import com.example.android.supportv7.Cheeses;
     27 import com.example.android.supportv7.R;
     28 import com.example.android.supportv7.widget.adapter.SimpleStringAdapter;
     29 import com.example.android.supportv7.widget.util.ConfigToggle;
     30 
     31 /**
     32  * A sample Activity to demonstrate capabilities of {@link GridLayoutManager}.
     33  */
     34 public class GridLayoutManagerActivity extends BaseLayoutManagerActivity<GridLayoutManager> {
     35     SimpleStringAdapter mAdapter;
     36     @Override
     37     protected GridLayoutManager createLayoutManager() {
     38         GridLayoutManager lm = new GridLayoutManager(this, 3);
     39         lm.setReverseLayout(true);
     40         lm.setSpanSizeLookup(mSpanSizeLookup);
     41         return lm;
     42     }
     43 
     44     GridLayoutManager.SpanSizeLookup mSpanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
     45         @Override
     46         public int getSpanSize(int position) {
     47             String item = mAdapter.getValueAt(position);
     48             return 1 + (Math.abs(item.hashCode()) % mLayoutManager.getSpanCount());
     49         }
     50     };
     51 
     52     @Override
     53     protected ConfigToggle[] createConfigToggles() {
     54         return new ConfigToggle[]{
     55                 new ConfigToggle(this, R.string.checkbox_orientation) {
     56                     @Override
     57                     public boolean isChecked() {
     58                         return mLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL;
     59                     }
     60 
     61                     @Override
     62                     public void onChange(boolean newValue) {
     63                         mLayoutManager.setOrientation(newValue ? LinearLayoutManager.HORIZONTAL
     64                                 : LinearLayoutManager.VERTICAL);
     65                     }
     66                 },
     67                 new ConfigToggle(this, R.string.checkbox_reverse) {
     68                     @Override
     69                     public boolean isChecked() {
     70                         return mLayoutManager.getReverseLayout();
     71                     }
     72 
     73                     @Override
     74                     public void onChange(boolean newValue) {
     75                         mLayoutManager.setReverseLayout(newValue);
     76                     }
     77                 },
     78                 new ConfigToggle(this, R.string.checkbox_layout_dir) {
     79                     @Override
     80                     public boolean isChecked() {
     81                         return ViewCompat.getLayoutDirection(mRecyclerView) ==
     82                                 ViewCompat.LAYOUT_DIRECTION_RTL;
     83                     }
     84 
     85                     @Override
     86                     public void onChange(boolean newValue) {
     87                         ViewCompat.setLayoutDirection(mRecyclerView, newValue ?
     88                                 ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);
     89                     }
     90                 },
     91                 new ConfigToggle(this, R.string.checkbox_stack_from_end) {
     92                     @Override
     93                     public boolean isChecked() {
     94                         return mLayoutManager.getStackFromEnd();
     95                     }
     96 
     97                     @Override
     98                     public void onChange(boolean newValue) {
     99                         mLayoutManager.setStackFromEnd(newValue);
    100                     }
    101                 }
    102         };
    103     }
    104 
    105     @Override
    106     protected void scrollToPositionWithOffset(boolean smooth, int position, int offset) {
    107         if (smooth) {
    108             super.scrollToPositionWithOffset(smooth, position, offset);
    109         } else {
    110             mLayoutManager.scrollToPositionWithOffset(position, offset);
    111         }
    112     }
    113 
    114     @Override
    115     protected RecyclerView.Adapter createAdapter() {
    116         mAdapter = new SimpleStringAdapter(this, Cheeses.sCheeseStrings) {
    117             @Override
    118             public SimpleStringAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
    119                     int viewType) {
    120                 final SimpleStringAdapter.ViewHolder vh = super
    121                         .onCreateViewHolder(parent, viewType);
    122                 vh.itemView.setOnClickListener(new View.OnClickListener() {
    123                     @Override
    124                     public void onClick(View v) {
    125                         final int pos = vh.getAdapterPosition();
    126                         if (pos == RecyclerView.NO_POSITION) {
    127                             return;
    128                         }
    129                         if (pos + 1 < getItemCount()) {
    130                             swap(pos, pos + 1);
    131                         }
    132                     }
    133                 });
    134                 return vh;
    135             }
    136         };
    137         return mAdapter;
    138     }
    139 }
    140