RecyclerView的LayoutManager库

依赖如下:

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

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

public class CeShi extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ArrayList 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 {
        @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);
        }
    }
}



    


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 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 itemTouchHelperCallback=new ItemTouchHelperCallback(shiPeiQi,arrayList);
        itemTouchHelperCallback.setOnSlideListener(new OnSlideListener() {
            @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 {
        @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 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 {
        @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);
        }
    }
}



    


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库)