Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2015 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.util;
     17 
     18 import com.example.android.supportv7.R;
     19 import android.os.Bundle;
     20 import android.support.v7.app.ActionBarActivity;
     21 import android.support.v7.util.SortedList;
     22 import android.support.v7.widget.LinearLayoutManager;
     23 import android.support.v7.widget.RecyclerView;
     24 import android.support.v7.widget.util.SortedListAdapterCallback;
     25 import android.view.KeyEvent;
     26 import android.view.LayoutInflater;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import android.view.inputmethod.EditorInfo;
     30 import android.widget.CheckBox;
     31 import android.widget.CompoundButton;
     32 import android.widget.EditText;
     33 import android.widget.TextView;
     34 
     35 /**
     36  * A sample activity that uses {@link SortedList} in combination with RecyclerView.
     37  */
     38 public class SortedListActivity extends ActionBarActivity {
     39     private RecyclerView mRecyclerView;
     40     private LinearLayoutManager mLinearLayoutManager;
     41     private SortedListAdapter mAdapter;
     42     @Override
     43     protected void onCreate(Bundle savedInstanceState) {
     44         super.onCreate(savedInstanceState);
     45         setContentView(R.layout.sorted_list_activity);
     46         mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
     47         mRecyclerView.setHasFixedSize(true);
     48         mLinearLayoutManager = new LinearLayoutManager(this);
     49         mRecyclerView.setLayoutManager(mLinearLayoutManager);
     50         mAdapter = new SortedListAdapter(getLayoutInflater(),
     51                 new Item("buy milk"), new Item("wash the car"),
     52                 new Item("wash the dishes"));
     53         mRecyclerView.setAdapter(mAdapter);
     54         mRecyclerView.setHasFixedSize(true);
     55         final EditText newItemTextView = (EditText) findViewById(R.id.new_item_text_view);
     56         newItemTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
     57             @Override
     58             public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
     59                 if (id == EditorInfo.IME_ACTION_DONE &&
     60                         (keyEvent == null || keyEvent.getAction() == KeyEvent.ACTION_DOWN)) {
     61                     final String text = textView.getText().toString().trim();
     62                     if (text.length() > 0) {
     63                         mAdapter.addItem(new Item(text));
     64                     }
     65                     textView.setText("");
     66                     return true;
     67                 }
     68                 return false;
     69             }
     70         });
     71     }
     72 
     73     private static class SortedListAdapter extends RecyclerView.Adapter<TodoViewHolder> {
     74         SortedList<Item> mData;
     75         final LayoutInflater mLayoutInflater;
     76         public SortedListAdapter(LayoutInflater layoutInflater, Item... items) {
     77             mLayoutInflater = layoutInflater;
     78             mData = new SortedList<Item>(Item.class, new SortedListAdapterCallback<Item>(this) {
     79                 @Override
     80                 public int compare(Item t0, Item t1) {
     81                     if (t0.mIsDone != t1.mIsDone) {
     82                         return t0.mIsDone ? 1 : -1;
     83                     }
     84                     int txtComp = t0.mText.compareTo(t1.mText);
     85                     if (txtComp != 0) {
     86                         return txtComp;
     87                     }
     88                     if (t0.id < t1.id) {
     89                         return -1;
     90                     } else if (t0.id > t1.id) {
     91                         return 1;
     92                     }
     93                     return 0;
     94                 }
     95 
     96                 @Override
     97                 public boolean areContentsTheSame(Item oldItem,
     98                         Item newItem) {
     99                     return oldItem.mText.equals(newItem.mText);
    100                 }
    101 
    102                 @Override
    103                 public boolean areItemsTheSame(Item item1, Item item2) {
    104                     return item1.id == item2.id;
    105                 }
    106             });
    107             for (Item item : items) {
    108                 mData.add(item);
    109             }
    110         }
    111 
    112         public void addItem(Item item) {
    113             mData.add(item);
    114         }
    115 
    116         @Override
    117         public TodoViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
    118             return new TodoViewHolder (
    119                     mLayoutInflater.inflate(R.layout.sorted_list_item_view, parent, false)) {
    120                 @Override
    121                 void onDoneChanged(boolean isDone) {
    122                     int adapterPosition = getAdapterPosition();
    123                     if (adapterPosition == RecyclerView.NO_POSITION) {
    124                         return;
    125                     }
    126                     mBoundItem.mIsDone = isDone;
    127                     mData.recalculatePositionOfItemAt(adapterPosition);
    128                 }
    129             };
    130         }
    131 
    132         @Override
    133         public void onBindViewHolder(TodoViewHolder holder, int position) {
    134             holder.bindTo(mData.get(position));
    135         }
    136 
    137         @Override
    138         public int getItemCount() {
    139             return mData.size();
    140         }
    141     }
    142 
    143     abstract private static class TodoViewHolder extends RecyclerView.ViewHolder {
    144         final CheckBox mCheckBox;
    145         Item mBoundItem;
    146         public TodoViewHolder(View itemView) {
    147             super(itemView);
    148             mCheckBox = (CheckBox) itemView;
    149             mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    150                 @Override
    151                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    152                     if (mBoundItem != null && isChecked != mBoundItem.mIsDone) {
    153                         onDoneChanged(isChecked);
    154                     }
    155                 }
    156             });
    157         }
    158 
    159         public void bindTo(Item item) {
    160             mBoundItem = item;
    161             mCheckBox.setText(item.mText);
    162             mCheckBox.setChecked(item.mIsDone);
    163         }
    164 
    165         abstract void onDoneChanged(boolean isChecked);
    166     }
    167 
    168     private static class Item {
    169         String mText;
    170         boolean mIsDone = false;
    171         final public int id;
    172         private static int idCounter = 0;
    173 
    174         public Item(String text) {
    175             id = idCounter ++;
    176             this.mText = text;
    177         }
    178     }
    179 }
    180