現在,安卓開發者用RecyclerView替代了以前的ListView或GridView。RecyclerView控制元件是更高階和靈活的ListView版本。這個控制元件是顯示大資料集的容器,只維持有限數量的view就可以高效地滾動。
遺憾的是,不像之前ListView那樣,Google並沒有給我們提供一個預設的Recycler View Adapter類來繼承。我首先將討論Recycer View Adapter,它可以輕鬆的將物件列表繫結到你的RecyclerView上。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public abstract class RecyclerViewArrayAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> { private final List<T> items; public void bindData(final List<T> items) { this.items = items; this.notifyDataSetChanged(); } public final T getItem(final int position) { return this.items.get(position); } public List<T> getItems() { return items; } @Override public int getItemCount() { return this.items != null ? this.items.size() : 0; } @Override public final void onBindViewHolder(final VH holder, final int position) { final T item = this.getItem(position); this.onBindViewHolder(holder, item); } public abstract void onBindViewHolder(final VH holder, final T item); } |
這個類不難理解,也不包含大量的邏輯,而且它可以避免重複實現包含一個簡單列表的Adapter類。
下面程式碼中你可以找到一個簡單的Adapter,這個Adapter用來向一個RecyclerView中顯示搜尋結果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import butterknife.Bind; import butterknife.ButterKnife; public class SearchResultsAdapter extends RecyclerViewArrayAdapter<SearchResult, SearchResultsAdapter.SearchResultViewHolder> implements View.OnClickListener { private final LayoutInflater layoutInflater; private OnItemClickListener onItemClickListener; public SearchResultsAdapter(final Context context) { super(); this.layoutInflater = LayoutInflater.from(context); } public void setOnItemClickListener(final OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } @Override public SearchResultViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) { final View view = this.layoutInflater.inflate(R.layout.listitem_search, parent, false); view.setOnClickListener(this); return new SearchResultViewHolder(view); } @Override public void onBindViewHolder(final SearchResultViewHolder holder, final SearchResult item) { holder.bindData(item); } /* * View.OnClickListener */ @Override public void onClick(final View view) { if (this.onItemClickListener != null) { final RecyclerView recyclerView = (RecyclerView) view.getParent(); final int position = recyclerView.getChildLayoutPosition(view); if (position != RecyclerView.NO_POSITION) { final SearchResult item = this.getItem(position); this.onItemClickListener.onItemClicked(item); } } } public static class SearchResultViewHolder extends RecyclerView.ViewHolder { @Bind(R.id.textview_name) TextView textViewName; public SearchResultViewHolder(final View itemView) { super(itemView); ButterKnife.bind(this, itemView); } public void bindData(final SearchResult item) { this.textViewName.setText(item.getName()); } } public interface OnItemClickListener { void onItemClicked(SearchResult item); } } |
為adapter繫結資料:
1 2 |
final List<SearchResult> items = this.getSearchResults(); this.adapter.bindData(items); |