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 17 18 package com.example.android.supportv7.widget; 19 20 import android.app.Activity; 21 import android.content.Context; 22 import android.os.Bundle; 23 import android.support.v7.util.AsyncListUtil; 24 import android.support.v7.widget.LinearLayoutManager; 25 import android.support.v7.widget.RecyclerView; 26 import android.view.Menu; 27 import android.view.MenuItem; 28 import android.view.ViewGroup; 29 import android.widget.TextView; 30 31 import com.example.android.supportv7.Cheeses; 32 33 /** 34 * A sample Activity to demonstrate capabilities of {@link AsyncListUtil}. 35 */ 36 public class AsyncListUtilActivity extends Activity { 37 38 private static final String TAG = "AsyncListUtilActivity"; 39 40 private RecyclerView mRecyclerView; 41 42 private LinearLayoutManager mLinearLayoutManager; 43 44 @Override 45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 48 mRecyclerView = new RecyclerView(this); 49 mLinearLayoutManager = new LinearLayoutManager(this); 50 mRecyclerView.setLayoutManager(mLinearLayoutManager); 51 mRecyclerView.setHasFixedSize(true); 52 final ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams( 53 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 54 mRecyclerView.setLayoutParams(layoutParams); 55 mRecyclerView.setAdapter(new AsyncAdapter()); 56 setContentView(mRecyclerView); 57 } 58 59 @Override 60 public boolean onCreateOptionsMenu(Menu menu) { 61 super.onCreateOptionsMenu(menu); 62 menu.add("Layout").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 63 return true; 64 } 65 66 @Override 67 public boolean onOptionsItemSelected(MenuItem item) { 68 mRecyclerView.requestLayout(); 69 return super.onOptionsItemSelected(item); 70 } 71 72 private static class TextViewHolder extends RecyclerView.ViewHolder { 73 TextView textView; 74 public TextViewHolder(Context context) { 75 super(new TextView(context)); 76 textView = (TextView) itemView; 77 } 78 } 79 80 private class AsyncAdapter extends RecyclerView.Adapter<TextViewHolder> { 81 82 private AsyncListUtil<String> mAsyncListUtil; 83 84 AsyncAdapter() { 85 mAsyncListUtil = new AsyncStringListUtil(); 86 } 87 88 @Override 89 public TextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 90 return new TextViewHolder(parent.getContext()); 91 } 92 93 @Override 94 public void onBindViewHolder(TextViewHolder holder, int position) { 95 final String itemString = mAsyncListUtil.getItem(position); 96 if (itemString == null) { 97 holder.textView.setText("loading..."); 98 } else { 99 holder.textView.setText(itemString); 100 } 101 } 102 103 @Override 104 public int getItemCount() { 105 return mAsyncListUtil.getItemCount(); 106 } 107 } 108 109 private class AsyncStringListUtil extends AsyncListUtil<String> { 110 111 private static final int TILE_SIZE = 5; 112 113 private static final long DELAY_MS = 500; 114 115 public AsyncStringListUtil() { 116 super(String.class, TILE_SIZE, 117 new AsyncListUtil.DataCallback<String>() { 118 @Override 119 public int refreshData() { 120 return Cheeses.sCheeseStrings.length; 121 } 122 123 @Override 124 public void fillData(String[] data, int startPosition, int itemCount) { 125 sleep(); 126 for (int i = 0; i < itemCount; i++) { 127 data[i] = Cheeses.sCheeseStrings[startPosition + i]; 128 } 129 } 130 131 private void sleep() { 132 try { 133 Thread.sleep(DELAY_MS); 134 } catch (InterruptedException e) { 135 e.printStackTrace(); 136 } 137 } 138 }, 139 new AsyncListUtil.ViewCallback() { 140 @Override 141 public void getItemRangeInto(int[] outRange) { 142 outRange[0] = mLinearLayoutManager.findFirstVisibleItemPosition(); 143 outRange[1] = mLinearLayoutManager.findLastVisibleItemPosition(); 144 } 145 146 @Override 147 public void onDataRefresh() { 148 mRecyclerView.getAdapter().notifyDataSetChanged(); 149 } 150 151 @Override 152 public void onItemLoaded(int position) { 153 mRecyclerView.getAdapter().notifyItemChanged(position); 154 } 155 }); 156 157 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 158 @Override 159 public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 160 onRangeChanged(); 161 } 162 }); 163 } 164 } 165 } 166