码迷,mamicode.com
首页 > 其他好文 > 详细

RecyclerView 介绍01

时间:2015-02-27 20:06:43      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

RecyclerView是Android support v7里面是一个自定义控件。用来显示大量数据集合。类似ListView和GridView这两个控件,RecyclerView同样可以实现,甚至更好。

以前我们在实现ListView和GridView的时候,都会定义一个ViewHolder用于优化UI性能【详见】,RecycleView的实现,已经是自带ViewHolder的实现。请看示例。

  1. 实现Adapter,定义每个Item的布局UI和数据。

    ?

    1. public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
    2. ?
    3. ????private List<String> mDatas = new ArrayList<>(0);
    4. ?
    5. ????public MyRecyclerAdapter(List<String> datas) {
    6. ????????if (datas != null) {
    7. ????????????mDatas.addAll(datas);
    8. ????????}
    9. ????}
    10. ?
    11. ????public static final class ViewHolder extends RecyclerView.ViewHolder {
    12. ?
    13. ????????private final TextView textView;
    14. ????????private final Button button;
    15. ?
    16. ?
    17. ????????public ViewHolder(View itemView) {
    18. ????????????super(itemView);
    19. ????????????textView = (TextView) itemView.findViewById(R.id.item_text);
    20. ????????????button = (Button) itemView.findViewById(R.id.item_button);
    21. ????????????button.setOnClickListener(new View.OnClickListener() {
    22. ?
    23. ????????????????@Override
    24. ????????????????public void onClick(View v) {
    25. ????????????????????Toast.makeText(v.getContext(), String.valueOf(getPosition()), Toast.LENGTH_SHORT).show();
    26. ????????????????}
    27. ????????????});
    28. ????????}
    29. ?
    30. ????????public TextView getTextView() {
    31. ????????????return textView;
    32. ????????}
    33. ?
    34. ????????public Button getButton() {
    35. ????????????return button;
    36. ????????}
    37. ????}
    38. ?
    39. ????@Override
    40. ????public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    41. ????????View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);
    42. ????????return new ViewHolder(view);
    43. ????}
    44. ?
    45. ????@Override
    46. ????public void onBindViewHolder(ViewHolder holder, int position) {
    47. ????????holder.getButton().setText(String.valueOf(position));
    48. ????????holder.getTextView().setText(mDatas.get(position));
    49. ????}
    50. ?
    51. ????@Override
    52. ????public int getItemCount() {
    53. ????????return mDatas.size();
    54. ????}
    55. ?
    56. }

    ?

  2. 初始化RecyclerView

    设置Adapter和LayoutManager。LayoutManager定义了每个Item的排列形式。官方提供两种实现,LinearLayoutManager和GridLayoutManager,当然我们也可以根据需求,自己实现LayoutManager。

    1. recyclerView = (RecyclerView) findViewById(R.id.recycler);
    2. MyRecyclerAdapter adapter = new MyRecyclerAdapter(Datas.getDatas(1000));
    3. RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
    4. recyclerView.setLayoutManager(layoutManager);
    5. recyclerView.setAdapter(adapter);

RecyclerView就是这么简单。

RecyclerView 介绍01

标签:

原文地址:http://www.cnblogs.com/halzhang/p/4303948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!