作家
登录

那年的RecyclerView我们从头撸一下

作者: 来源: 2017-07-14 15:51:18 阅读 我要评论

// 设置纵向的瀑布流构造 
  • StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL); 
  • recyclerView.setLayoutManager(layoutManager);  
  • 我们看效不雅和GridLayoutManager没有什么差别呢,那我们啻小小的修改一下,你就可以看到它的强大年夜。

    我们给每个item的构造参加margin:

    1. <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    2.     android:orientation="vertical" android:layout_width="match_parent" 
    3.     android:layout_height="wrap_content" 
    4.     android:layout_margin="5dp" 
    5.     android:background="@color/colorAccent"
    6.  
    7.     <TextView 
    8.         android:id="@+id/tv" 
    9.         android:layout_width="100dp" 
    10.         android:layout_height="100dp" 
    11.         android:layout_gravity="center" 
    12.         android:gravity="center" 
    13.         android:textColor="#fff" 
    14.         /></LinearLayout>  

    然后我们在适配器的onBindViewHolder办法中为我们的item设置个随机的高度:

    1. //贴上部分代码,其余的请看附件  
    2.     private List<Integer> mHeights;     
    3.     public RecyclerAdapter(Context context, List<String> mDatas) {         
    4.     this.context = context;         
    5.     this.mDatas = mDatas; 
    6.         mHeights = new ArrayList<Integer>();         
    7.             for (int i = 0; i < mDatas.size(); i++) { 
    8.             mHeights.add((int) (100 + Math.random() * 300)); 
    9.         } 
    10.     } 
    11.  
    12.     @Override     
    13.     public void onBindViewHolder(MyViewHolder holder,  

    14.   推荐阅读

        Windows 10计划任务怎么用?教你轻松玩转Windows 10自带任务计划

      【技巧沙龙】AI开辟者拭魅战营-7分钟打造1个定制技能。7月22号,我们等你一路! Win10筹划义务怎么竽暌姑?义务筹划是Windows操作体系异常强大年夜的一个功能,应用义务筹划,我们可以将任>>>详细阅读


      本文标题:那年的RecyclerView我们从头撸一下

      地址:http://www.17bianji.com/lsqh/36229.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)