第一行代码笔记 RecycleView使用

第一步:在gradle中引用

compile 'com.android.support:recyclerview-v7:23.0.0'


第二步:在布局文件中引用

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.lvyequeen.test.day01.recyclerView.RecyclerActivity">

            android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycle"
        />



第三步:定义子item的布局

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

            android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

            android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />



第四步:编写数据源的bean 类

public class MyData {
    private String discreble;
    private int image;

    public MyData(String discreble, int image) {
        this.discreble = discreble;
        this.image = image;
    }

    public String getDiscreble() {
        return discreble;
    }

    public void setDiscreble(String discreble) {
        this.discreble = discreble;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "discreble='" + discreble + '\'' +
                ", image=" + image +
                '}';
    }
}


第五步:编写adapter

public class RecycleViewAdapter extends RecyclerView.Adapter {

    //    定义数据源
    private List mData;

    /**
     * 通过构造方法,在这里将数据源传入
     *
     * @param mData
     */
    public RecycleViewAdapter(List mData) {
        this.mData = mData;
    }

    /**
     * 设置子itemview的布局,同时设置点击事件
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_layout, parent, false);
        final ViewHolder viewHolder = new ViewHolder(view);
//        点击最最外层子view
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getAdapterPosition();
                MyData myData = mData.get(position);
                Toast.makeText(parent.getContext(),"click:"+myData.getDiscreble(),Toast.LENGTH_SHORT).show();
            }
        });
//        点击图标
        viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getAdapterPosition();
                MyData myData = mData.get(position);
                Toast.makeText(parent.getContext(),"click Image:"+myData.getDiscreble(),Toast.LENGTH_SHORT).show();

            }
        });

        return viewHolder;
    }

    /**
     * 将数据和控件绑定
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MyData myDate = mData.get(position);
        holder.imageView.setImageResource(myDate.getImage());
        holder.textView.setText(myDate.getDiscreble());

    }

    /**
     * 返回多长的item
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return mData.size();
    }

    /**
     * 定义内部类,实例化item的每一个view
     * 同时保存最外层的item布局实例
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        ImageView imageView;
        TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;//保存子项最外层布局的实例
            imageView = (ImageView) itemView.findViewById(R.id.image);
            textView = (TextView) itemView.findViewById(R.id.text);
        }
    }
}

第六步:在activty中使用

public class RecyclerActivity extends AppCompatActivity {

    private RecyclerView recycle;
    private List mydatas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        initData();//初始化数据源
        initView();
    }

    private void initView() {
        recycle = ((RecyclerView) findViewById(R.id.recycle));
//      LinearLayoutManager使用线性布局,实现的是listview类似的效果。
//      LinearLayoutManager manager = new LinearLayoutManager(this);
//      默认为纵向排列,通过 manager.setOrientation 可以设置横向滚动。
//      manager.setOrientation(LinearLayoutManager.HORIZONTAL);
//       设置样式为网格布局 参数1为上下文,参数而为列数
//        GridLayoutManager manager = new GridLayoutManager(this, 4);
//        设置瀑布流布局 参数1为列数,参数二为排布方式
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

        recycle.setLayoutManager(manager);
        RecycleViewAdapter adapter = new RecycleViewAdapter(mydatas);
        recycle.setAdapter(adapter);

    }

    /**
     * 伪造数据源
     */
    private void initData() {
        MyData mydate = new MyData("a", R.mipmap.a);
        mydatas.add(mydate);
        MyData mydate1 = new MyData("a1******************", R.mipmap.a1);
        mydatas.add(mydate1);
        MyData mydate2 = new MyData("a2@@@@@@@@@@@@", R.mipmap.a2);
        mydatas.add(mydate2);
        MyData mydate3 = new MyData("a3#######", R.mipmap.a3);
        mydatas.add(mydate3);
        MyData mydate4 = new MyData("a4$$$", R.mipmap.a4);
        mydatas.add(mydate4);
        MyData mydate5 = new MyData("a5", R.mipmap.a5);
        mydatas.add(mydate5);
        MyData mydate6 = new MyData("a6^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^", R.mipmap.a6);
        mydatas.add(mydate6);
        MyData mydate7 = new MyData("a7&&&&&&&&", R.mipmap.a7);
        mydatas.add(mydate7);
        MyData mydate8 = new MyData("a8****************************", R.mipmap.a8);
        mydatas.add(mydate8);

    }
}






你可能感兴趣的:(android常见功能,第一行代码笔记)