从头开始学 RecyclerView(一) 基本使用
2017-03-30 13:39
411 查看
偶吐了个槽
android.support.v7.widget.RecyclerView 这是出现快三年了么。因做电视开发,以前都是各种ViewPager,反正它是一个大坑。目前,项目中首页中的VP还在,超恶心。
最近有闲暇时间了。准备把RV再次学一下。这次想稍稍深入一点。
最基本使用方法
把RV看成一个ListView或GridView,它肯定需要一个Adapter。需要用到RecyclerView.Adapter
rv.setAdapter(new RecyclerView.Adapter())
在重写RecyclerView.Adapter时,必须实现的三个方法:
//创建RecyclerView.ViewHolder public abstract VH onCreateViewHolder(ViewGroup parent, int viewType); //绑定RecyclerView.ViewHolder 与数据关联 public abstract void onBindViewHolder(VH holder, int position); //item总数量 public abstract int getItemCount();
RV 还需要指定一个布局管理器:
rv.setLayoutManager(layoutManager)
简单示例
basic_simple.xml<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="title"/> <View android:id="@+id/v_img" android:layout_below="@id/tv_title" android:layout_width="match_parent" android:layout_height="100dp"/> </RelativeLayout>
MainActivity:
/** * desc : * author : stone * email : aa86799@163.com * time : 29/03/2017 20 37 */ public class MainActivity extends Activity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); basicSimple(); } private void basicSimple() { RecyclerView rv = new RecyclerView(this); setContentView(rv); rv.setLayoutParams(new FrameLayout.LayoutParams(-1, -1)); final List<String> list = new ArrayList<>(); for (int i = 'A'; i < 'z'; i++) { list.add("" + (char) i); } LinearLayoutManager layoutManager = new LinearLayoutManager(this); rv.setLayoutManager(layoutManager); rv.setAdapter(new RecyclerView.Adapter<BasicSimpleVH>() { @Override//创建ViewHolder public BasicSimpleVH onCreateViewHolder(ViewGroup parent, int viewType) { return new BasicSimpleVH( LayoutInflater.from(parent.getContext()).inflate(R.layout.basic_simple, null, false)); } @Override//当绑定ViewHolder public void onBindViewHolder(BasicSimpleVH holder, int position) { holder.itemView.setFocusable(true);//加了这句,电视上就能滚动了 TextView tvTitle = (TextView) holder.itemView.findViewById(R.id.tv_title); tvTitle.setText(list.get(position)); View vImg = holder.itemView.findViewById(R.id.v_img); vImg.setBackgroundColor(getColor()); } @Override public int getItemCount() { return list.size(); } }); } private static class BasicSimpleVH extends RecyclerView.ViewHolder { public BasicSimpleVH(View itemView) { super(itemView); } } /** * 随机颜色 */ Random random = new Random(); private int getColor() { StringBuilder sb = new StringBuilder(); String temp; for (int i = 0; i < 4; i++) { temp = Integer.toHexString(random.nextInt(0xFF)); if (temp.length() == 1) { temp = "0" + temp; } sb.append(temp); } return Color.parseColor("#" + sb.toString()); } }
为了,在电视上能滚动,这里简单指定了holder.itemview可以获取焦点。
RV其它操作
RV对每个Item都可以自定制装饰(decoration)。对应的方法:
public void addItemDecoration(ItemDecoration decor)
RV对每个Item都可以自定制动画(adapter中的item发生改变时的动画)。
对应的方法:
public void setItemAnimator(ItemAnimator animator)
相关文章推荐
- Android App开发中RecyclerView控件的基本使用教程
- Android MaterialDesign控件使用以及RecyclerView,ItemDecoration的基本使用
- 【Android开发】RecyclerView的基本使用
- Android 技术之RecyclerView的基本使用(再不学会你就out了)
- RecyclerView的基本使用步骤
- 从头开始学 RecyclerView(五) ItemDecoration 详解
- RecyclerView更全解析之 - 基本使用和分割线解析
- Android开发——RecyclerView特性以及基本使用方法(二)
- Android RecyclerView基本使用详解
- android---UI---RecyclerViewd的基本使用
- RecyclerView的基本使用方法
- Android RecyclerView 的基本使用
- RecyclerView CardView的基本使用
- RecyclerView更全解析之 - 基本使用和分割线解析
- RecyclerView基本使用方法
- EasyRecyclerView的基本使用
- RecyclerView的基本使用(优化图片加载、设置条目间距、设置Adapter、设置点击事件等)
- RecyclerView 介绍 基本使用 API
- Android RecyclerView基本使用
- RecyclerView的基本使用