RecyclerView,是在v7包加入的,一个灵活的view可以展示巨大的数据集,类似于listview的viewholder复用已经优化好了。

语言是苍白的,代码是最生动的叙说:

布局:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.MainActivity"
android:padding="12dp"
android:orientation="vertical"> <android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:clipToPadding="false"/> <ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
/> </FrameLayout>

  

adapter:

package com.escorps.retrodagger.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.escorps.retrodagger.R;
import com.escorps.retrodagger.models.PetModel;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView; public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{ private List<PetModel> items; public void setItems(List<PetModel> items){
this.items = items;
} @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler, parent, false);
return new ViewHolder(v);
} @Override public void onBindViewHolder(final ViewHolder holder, int position) {
final PetModel item = items.get(position); holder.textName.setText(item.getName()); holder.itemView.setTag(item);
} @Override public int getItemCount() {
return items.size();
} protected static class ViewHolder extends RecyclerView.ViewHolder { @InjectView(R.id.textName) TextView textName; public ViewHolder(View itemView) {
super(itemView); ButterKnife.inject(this, itemView);
}
} }

注意在使用adapter之前: recyclerView.setLayoutManager(new GridLayoutManager(this, 1));

最新文章

  1. git本地有修改如何强制更新
  2. 通过Chrome浏览器检测和优化页面
  3. poj 1324 Holedox Moving
  4. ubuntu忘记密码怎么办
  5. 【转】基于Ubuntu 14.04 LTS编译Android4.4.2源代码
  6. Android写入文件操作权限
  7. 常用HQL集锦
  8. mysql事务隔离级别和MVCC
  9. SQL瓶颈分析,以及适应最佳执行计划的探讨
  10. 「CTSC2018」暴力写挂
  11. C++\CLI语法 在项目中的使用
  12. [LOJ#2386]. 「USACO 2018.01 Platinum」Cow at Large[点分治]
  13. react项目的react-router-dom路由的使用
  14. git安装与初始化
  15. 如何在Ubuntu中用firefox浏览器查看chm文档?
  16. Java分布式:消息队列(Message Queue)
  17. 使用js写的确认提交表单确认框
  18. luogu P1772 [ZJOI2006]物流运输
  19. [转]RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较
  20. 解决nsis error!cant initialize plug-ins directory.please try again later

热门文章

  1. javascript 面向对象的继承的实现
  2. asp.net 使用HttpModule记录全局错误
  3. 20141129 LinQ to SQL
  4. javascript限制input只允许输入数字
  5. JVM学习之Eclipse输出GC日志
  6. ubuntu中Mysql常用命令整理
  7. Docker的C/S模式详解
  8. 浮动层固定兼容IE6 position:fixed的最佳解决方案
  9. JS输出当前时间,且每秒变化
  10. scss + react + webpack + es6