美文网首页
RecyclerView的LayoutManager库

RecyclerView的LayoutManager库

作者: 名字_都被占了 | 来源:发表于2018-06-07 11:19 被阅读0次

    依赖如下:

    maven { url 'https://jitpack.io' }
    
    compile 'com.github.DingMouRen:LayoutManagerGroup:643fb71c8e'
    

    1:EchelonLayoutManager(梯形布局),示例代码如下(注意看下备注):

    public class CeShi extends AppCompatActivity {
        private RecyclerView recyclerView;
        private ArrayList<String> arrayList;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ceshi);
            recyclerView = findViewById(R.id.recycler);
            arrayList = new ArrayList<>();
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            EchelonLayoutManager echelonLayoutManager = new EchelonLayoutManager(this);
            Log.d("CeShi", "echelonLayoutManager.getVerticalSpace():" + echelonLayoutManager.getVerticalSpace());
            //如果getVerticalSpace方法得到的值为0,那么就会出现java.lang.ArithmeticException: divide by zero异常,也就是除数不能为0异常,看源码可知
            //在EchelonLayoutManager源码的layoutChild方法中要除getVerticalSpace,所以RecyclerView的layout_height属性不能为wrap_content,不然会报错,可以是
            //match_parent或者是具体的数值。
            recyclerView.setLayoutManager(echelonLayoutManager);
            recyclerView.setAdapter(new ShiPeiQi());
        }
    
        class ShiPeiQi extends RecyclerView.Adapter<ShiTu> {
            @NonNull
            @Override
            public ShiTu onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                ShiTu shiTu = new ShiTu(LayoutInflater.from(CeShi.this).inflate(R.layout.item, null));
                return shiTu;
            }
    
            @Override
            public void onBindViewHolder(@NonNull ShiTu holder, int position) {
                holder.textView.setText(arrayList.get(position) + position);
            }
    
            @Override
            public int getItemCount() {
                return arrayList.size();
            }
        }
    
        public class ShiTu extends RecyclerView.ViewHolder {
            private TextView textView;
            private ImageView imageView;
    
            ShiTu(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.textView);
                imageView = itemView.findViewById(R.id.imageview);
            }
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/lin"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </LinearLayout>
    

    2:SkidRightLayoutManager(右滑布局),示例代码如下(内容和上面一样,就该了两行代码):

    ...
    SkidRightLayoutManager skidRightLayoutManager = new SkidRightLayoutManager(1.5f, 0.85f);
    recyclerView.setLayoutManager(skidRightLayoutManager);
    ...
    

    3:SlideLayoutManager(滑动布局),示例代码如下

    public class CeShi extends AppCompatActivity {
        private RecyclerView recyclerView;
        private ArrayList<String> arrayList;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ceshi);
            recyclerView = findViewById(R.id.recycler);
            arrayList = new ArrayList<>();
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            ShiPeiQi shiPeiQi=new ShiPeiQi();
            ItemTouchHelperCallback<String> itemTouchHelperCallback=new ItemTouchHelperCallback<String>(shiPeiQi,arrayList);
            itemTouchHelperCallback.setOnSlideListener(new OnSlideListener<String>() {
                @Override
                public void onSliding(RecyclerView.ViewHolder viewHolder, float ratio, int direction) {
                    if (direction == ItemConfig.SLIDING_LEFT) {
                    } else if (direction == ItemConfig.SLIDING_RIGHT) {
                    }
                }
    
                @Override
                public void onSlided(RecyclerView.ViewHolder viewHolder, String o, int direction) {
                    if (direction == ItemConfig.SLIDED_LEFT) {
                        Toast.makeText(CeShi.this, "不喜欢", Toast.LENGTH_SHORT).show();
                    } else if (direction == ItemConfig.SLIDED_RIGHT) {
                        Toast.makeText(CeShi.this, "喜欢", Toast.LENGTH_SHORT).show();
                    }
                }
    
                @Override
                public void onClear() {
                    Toast.makeText(CeShi.this, "没数据了", Toast.LENGTH_SHORT).show();
                }
            });
            ItemTouchHelper itemTouchHelper=new ItemTouchHelper(itemTouchHelperCallback);
            SlideLayoutManager slideLayoutManager = new SlideLayoutManager(recyclerView,itemTouchHelper);
            itemTouchHelper.attachToRecyclerView(recyclerView);
            recyclerView.setLayoutManager(slideLayoutManager);
            recyclerView.setAdapter(shiPeiQi);
        }
    
        class ShiPeiQi extends RecyclerView.Adapter<ShiTu> {
            @NonNull
            @Override
            public ShiTu onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                ShiTu shiTu = new ShiTu(LayoutInflater.from(CeShi.this).inflate(R.layout.item, null));
                return shiTu;
            }
    
            @Override
            public void onBindViewHolder(@NonNull ShiTu holder, int position) {
                holder.textView.setText(arrayList.get(position) + position);
            }
    
            @Override
            public int getItemCount() {
                return arrayList.size();
            }
        }
    
        public class ShiTu extends RecyclerView.ViewHolder {
            private TextView textView;
            private ImageView imageView;
    
            ShiTu(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.textView);
                imageView = itemView.findViewById(R.id.imageview);
            }
        }
    }
    

    4:BannerLayoutManager(横幅布局),示例代码如下

    public class CeShi extends AppCompatActivity {
        private RecyclerView recyclerView;
        private ArrayList<String> arrayList;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ceshi);
            recyclerView = findViewById(R.id.recycler);
            arrayList = new ArrayList<>();
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            arrayList.add("第");
            BannerLayoutManager bannerLayoutManager = new BannerLayoutManager(this,recyclerView,4, OrientationHelper.VERTICAL);
            bannerLayoutManager.setTimeSmooth(500f);
            recyclerView.setLayoutManager(bannerLayoutManager);
            recyclerView.setAdapter(new ShiPeiQi());
        }
    
        class ShiPeiQi extends RecyclerView.Adapter<ShiTu> {
            @NonNull
            @Override
            public ShiTu onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                ShiTu shiTu = new ShiTu(LayoutInflater.from(CeShi.this).inflate(R.layout.item, null));
                return shiTu;
            }
    
            @Override
            public void onBindViewHolder(@NonNull ShiTu holder, int position) {
                holder.textView.setText(arrayList.get(position%8) + position);//这里需要取余,不然arrayList中没有无穷个数据。
            }
    
            @Override
            public int getItemCount() {
                return Integer.MAX_VALUE;//这里只有是无穷大,item才能不停的滚动
            }
        }
    
        public class ShiTu extends RecyclerView.ViewHolder {
            private TextView textView;
            private ImageView imageView;
    
            ShiTu(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.textView);
                imageView = itemView.findViewById(R.id.imageview);
            }
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/lin"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"/><!--这里的layout_height的值不能是match_parent和wrap_content,否则item滚动不起来,得是具体的数值才行-->
    
    </LinearLayout>
    

    5:ViewPagerLayoutManager(视图页布局),示例代码如下

    ...
    ViewPagerLayoutManager viewPagerLayoutManager = new ViewPagerLayoutManager(this, OrientationHelper.VERTICAL);
            viewPagerLayoutManager.setOnViewPagerListener(new OnViewPagerListener() {
                @Override
                public void onPageRelease(boolean b, int i) {
                    Toast.makeText(CeShi.this, "页面划出去了", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onPageSelected(int i, boolean b) {
                    Toast.makeText(CeShi.this, "页面选择住了", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onLayoutComplete() {
    
                }
            });
            recyclerView.setLayoutManager(viewPagerLayoutManager);
    ...
    

    github地址如下:
    https://github.com/DingMouRen/LayoutManagerGroup

    相关文章

      网友评论

          本文标题:RecyclerView的LayoutManager库

          本文链接:https://www.haomeiwen.com/subject/ucawsftx.html