zoukankan      html  css  js  c++  java
  • RecyclerView用法

    主界面布局:
    <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);
      }
    }






  • 相关阅读:
    mysql替代like模糊查询的方法
    8个超实用的jQuery插件应用
    判断登陆设备是否为手机
    SQL tp3.2 批量更新 saveAll
    SQL-批量插入和批量更新
    防止手机端底部导航被搜索框顶起
    php COM
    thinkphp3.2 where 条件查询 复查的查询语句
    Form表单提交,js验证
    jupyter notebook 使用cmd命令窗口打开
  • 原文地址:https://www.cnblogs.com/zhaoleigege/p/5123916.html
Copyright © 2011-2022 走看看