Android之RecycleView使用(瀑布流管理器及线性流管理器)

  首先我们先来了解一下RecycleView:

     RecycleView相对于原来的ListView和GridView要灵活很多,可以很快的在listView和gridView以及瀑布流之间进行切换,它主要提供了3中布局方式,LinearLayoutManager(ListView)、GridLayoutManager(GridView)、StaggeredGridLayoutManager(瀑布流)。

    RecyclerView比ListView,GridView之类控件都有哪些优点:

  • 1、数据绑定
  • 2、Item View创建
  • 3、View的回收以及重用等机制。
       下面我们来介绍一下RecycleView瀑布流的用法
 一、我们需要一个瀑布流的工具类   DividerItemDecoration.java
   
/*瀑布流
* */

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private Drawable mDivider;

    private int mOrientation;

    public DividerItemDecoration(Context context, int orientation) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        setOrientation(orientation);
    }

    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    public void drawVertical(Canvas c, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin +
                    Math.round(ViewCompat.getTranslationY(child));
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getRight() + params.rightMargin +
                    Math.round(ViewCompat.getTranslationX(child));
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }
}
二、我们来建一个Activity    RecyclerActivity.java
        先来看一下布局
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.edu.jereh.android14.recyclerlistview.RecyclerActivity">
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rv"
    ></android.support.v7.widget.RecyclerView>
</RelativeLayout>
三、我们来看一下 具体代码的写法:
public class RecyclerActivity extends Activity {

    @Bind(R.id.rv)
    RecyclerView rv;
    private List<Map> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        ButterKnife.bind(this);
        list =new ArrayList<>();
        initDate();
        MyAdapter myAdapter =new MyAdapter(list);

        //瀑布流管理器
        StaggeredGridLayoutManager sgm = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        rv.setLayoutManager(sgm);
        rv.setAdapter(myAdapter);
        //设置分割线
        DividerItemDecoration itemDecoration =
                new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST);
        rv.addItemDecoration(itemDecoration);
    }
    public void initDate(){
        HashMap map =new HashMap();
        map.put("img",R.mipmap.a);
        map.put("text","x1");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.b);
        map.put("text","x2");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.as);
        map.put("text","x3");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.e);
        map.put("text","x2");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.e);
        map.put("text","x3");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.as);
        map.put("text","x2");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.lh3);
        map.put("text","x3");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.b);
        map.put("text","x2");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.as);
        map.put("text","x3");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.lh1);
        map.put("text","x2");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.lh2);
        map.put("text","x3");
        list.add(map);
    }
    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
        private List<Map> mapList;

        public MyAdapter(List<Map> mapList) {
            this.mapList = mapList;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=LayoutInflater.from(getBaseContext()).inflate(R.layout.recycle_stag,parent,false);
            MyViewHolder viewHolder = new MyViewHolder(view);
            return viewHolder;
        }
//绑定
        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
           Map map = mapList.get(position);
            holder.imageView.setImageResource((Integer) map.get("img"));
            holder.textView.setText(map.get("text").toString());

        }

        @Override
        public int getItemCount() {
            return mapList.size();
        }
    }
    public class MyViewHolder extends RecyclerView.ViewHolder{
       ImageView imageView;
        TextView textView;
        public MyViewHolder(View itemView) {
            super(itemView);
            imageView=(ImageView)itemView.findViewById(R.id.iv);
            textView =(TextView)itemView.findViewById(R.id.tvi1);


        }
    }
}
下面是效果图






    




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值