Android学习之RecyclerView学习(实现瀑布流式布局)

RecyclerView,大家可以通过导入support-v7对其进行使用。 

如果使用AndroidStudio开发, 需要在build.gradle中添加:

compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'

据官方的介绍,该控件用于在有限的窗口中展示大量数据集,其实这样功能的控件我们并不陌生,例如:ListView、GridView。

整体上看RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator实现令人瞠目的效果。

  • 控制其显示的方式,请通过布局管理器LayoutManager

  • 控制Item间的间隔(可绘制),请通过ItemDecoration

  • 控制Item增删的动画,请通过ItemAnimator

  • 控制点击、长按事件(暂不支持)


RecyclerView的使用代码:

 
  

mRecyclerView = findView(R.id.id_recyclerview);//设置布局管理器 mRecyclerView.setLayoutManager(layout);//设置adaptermRecyclerView.setAdapter(adapter)//设置Item增加、移除动画mRecyclerView.setItemAnimator(new DefaultItemAnimator());//添加分割线mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.HORIZONTAL_LIST));

RecyclerView.LayoutManager吧,这是一个抽象类,好在系统提供了3个实现类:

  1. LinearLayoutManager 现行管理器,支持横向、纵向。

  2. GridLayoutManager 网格布局管理器

  3. StaggeredGridLayoutManager 瀑布就式布局管理器


RecyclerView提供自己的adapter:RecyclerView.Adapter,该适配器已经集成了ViewHolder类,只需要实现里面的onCreateViewHolder、onBindViewHolder抽象方法,具体使用如下:
package com.lzy.host.perfect.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lzy.host.perfect.R;

import java.util.List;

/**
 * RecyclerViewAdapter
 *
 * @author linzhiyong
 * @time 2017年2月8日15:17:41
 * @email [email protected]
 * @desc
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter {

    private Context context;

    private List data;

    public RecyclerViewAdapter(Context context, List data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_recyclerview, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.imageView.setImageResource(this.data.get(position));
        if (position % 2 == 0) {
            holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300));
        } else {
            holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400));
        }
    }

    @Override
    public int getItemCount() {
        return this.data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.imageView = (ImageView) itemView.findViewById(R.id.imageView1);
        }
    }

}

Activity:
package com.lzy.host.perfect.demo;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.widget.TextView;

import com.lzy.host.perfect.R;
import com.lzy.host.perfect.activity.BaseActivity;
import com.lzy.host.perfect.adapter.RecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * RecyclerView
 *
 * @author linzhiyong
 * @time 2017年2月8日15:17:41
 * @email [email protected]
 * @desc
 */
public class RecyclerViewActivity extends BaseActivity {

    private TextView topTitleView;

    private RecyclerView recyclerView;
    private RecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_recyclerview);
        topTitleView = (TextView) findViewById(R.id.base_top_title);
        topTitleView.setText("RecyclerView");

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        adapter = new RecyclerViewAdapter(this, createData());

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter);
    }

    private List createData() {
        List data = new ArrayList();
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                data.add(R.mipmap.ic_demo_circle);
            } else {
                data.add(R.mipmap.ic_address_male);
            }
        }
        return data;
    }
}

布局文件:



    

    
    


适配器item布局文件:



    





你可能感兴趣的:(android)