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

RecyclerView用法

时间:2016-01-12 13:22:55      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

主界面布局:
<RelativeLayout 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" >

  <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_centerVertical="true"
    android:background="@android:color/transparent"
    android:scrollbars="none" />

</RelativeLayout>

Item布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="120dp"
  android:layout_height="120dp" >

  <ImageView
    android:id="@+id/iv_image"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:scaleType="centerCrop" />

</RelativeLayout>
RecyclerView的数据适配器,MyAdapter.java


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

  private int[] mDataset; // 外面传入的数据

  public static class ViewHolder extends RecyclerView.ViewHolder {

    ImageView mImageView;

    // TODO Auto-generated method stub
    public ViewHolder(View v) {
      super(v);
    }

  }

  public MyAdapter(int[] mDataset) {
    this.mDataset = mDataset;
  }

  /**
   * 获取总的条目数量
   */
  @Override
  public int getItemCount() {
    // TODO Auto-generated method stub
    return mDataset.length;
  }

  /**
   * 创建ViewHolder
   */
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // TODO Auto-generated method stub
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycleview, parent, false);
    ViewHolder holder = new ViewHolder(v);
    holder.mImageView = (ImageView) v.findViewById(R.id.iv_image);
    return holder;
  }

  /**
   * 将数据绑定到ViewHolder上
   */
  @Override
  public void onBindViewHolder(ViewHolder holder, int position) {
    // TODO Auto-generated method stub
    holder.mImageView.setImageResource(mDataset[position]);
  }
}
最后,就在Activity里使用这个RecyclerView,MainActivity.java


public class MainActivity extends Activity {

  /** RecyclerView对象 */
  private RecyclerView recyclerView;
  /** 图片资源 */
  private int[] mDataset;
  /** 数据适配器 */
  private MyAdapter mAdapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    // 初始化图片数据
    mDataset = new int[] { R.drawable.a, R.drawable.b, //
        R.drawable.c, R.drawable.d, R.drawable.e, //
        R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i };
    // 设置布局管理器
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    recyclerView.setLayoutManager(linearLayoutManager);
    // 设置适配器
    mAdapter = new MyAdapter(mDataset);
    recyclerView.setAdapter(mAdapter);
  }
}






RecyclerView用法

标签:

原文地址:http://www.cnblogs.com/zhaoleigege/p/5123916.html

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