RecyclerView自定义行个数

在某些需求下,会出现在一个ReyclerView列表中,各行的元素个数不相等,此时可以使用两种方式解决问题。

使用StaggeredGridLayoutManager和StaggeredGridLayoutManager.LayoutParams中的setFullSpan方法,可以设置某个元素单独占据一行,其他元素照常排列。但是这种方式缺点只能应对一个元素占据一行的情景。
比如,常规是一行三个item,我希望某些行是1一个item,某些行是2个item,此方法则不可用。此时则可以使用GridLayoutManager和GridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup)来完成。

具体示例:用StaggeredGridLayoutManager

public class TestRecyclerViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_recycler_view);

        testWithStaggerLayoutManger();
    }

    public static int getScreenWidth(Context context) {
        WindowManager mgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = mgr.getDefaultDisplay();
        return display.getWidth();
    }

    private void testWithStaggerLayoutManger() {
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, LinearLayoutManager.VERTICAL));
        recyclerView.setAdapter(new TestAdapter());
    }

    class TestAdapter extends RecyclerView.Adapter<TestViewHolder>{

        @NonNull
        @Override
        public TestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            TextView textView = new TextView(getBaseContext());
            StaggeredGridLayoutManager.LayoutParams params = new StaggeredGridLayoutManager.LayoutParams(getScreenWidth(getBaseContext()) / 3, 180);
            textView.setLayoutParams(params);
            textView.setTextColor(Color.BLACK);
            textView.setGravity(Gravity.CENTER);
            return new TestViewHolder(textView);
        }

        @Override
        public void onBindViewHolder(@NonNull TestViewHolder holder, int position) {
            holder.bind(position);
        }

        @Override
        public int getItemCount() {
            return 18;
        }
    }

    class TestViewHolder extends RecyclerView.ViewHolder{

        public TestViewHolder(View itemView) {
            super(itemView);
        }

        public void bind(int pos){
            ((TextView)itemView).setText("第" + pos + "个item");
            if(pos % 3 == 0){
                itemView.setBackgroundColor(Color.RED);
            } else if (pos % 3 == 1){
                itemView.setBackgroundColor(Color.GREEN);
            } else if (pos % 3 == 2){
                itemView.setBackgroundColor(Color.BLUE);
            }

            if(pos % 4 == 0){
                StaggeredGridLayoutManager.LayoutParams clp = (StaggeredGridLayoutManager.LayoutParams) itemView.getLayoutParams();
                clp.setFullSpan(true);
                ((TextView) itemView).setWidth(getScreenWidth(getBaseContext()));
                itemView.setLayoutParams(clp);
                itemView.setBackgroundColor(Color.YELLOW);
            }
        }
    }
}

页面效果:
image

使用GridLayoutManager
public class TestRecyclerViewActivity extends AppCompatActivity {

    private GridLayoutManager gridLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_recycler_view);

//        testWithStaggerLayoutManger();
        testWithGridLayoutManager();
    }

    private void testWithGridLayoutManager() {
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        gridLayoutManager = new GridLayoutManager(this, 6, LinearLayoutManager.VERTICAL, false);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int mod = position % 6;
                if(mod == 0){
                    return 6;
                } else if(mod == 1 || mod == 2) {
                    return 3;
                } else {
                    return 2;
                }
            }
        });
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(new TestAdapter2());
    }

    public static int getScreenWidth(Context context) {
        WindowManager mgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = mgr.getDefaultDisplay();
        return display.getWidth();
    }

    class TestAdapter2 extends RecyclerView.Adapter<TestViewHolder2>{

        @NonNull
        @Override
        public TestViewHolder2 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            TextView textView = new TextView(getBaseContext());
            StaggeredGridLayoutManager.LayoutParams params = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 180);
            textView.setLayoutParams(params);
            textView.setTextColor(Color.BLACK);
            textView.setGravity(Gravity.CENTER);
            return new TestViewHolder2(textView);
        }

        @Override
        public void onBindViewHolder(@NonNull TestViewHolder2 holder, int position) {
            holder.bind(position);
        }

        @Override
        public int getItemCount() {
            return 21;
        }
    }

    class TestViewHolder2 extends RecyclerView.ViewHolder{

        public TestViewHolder2(View itemView) {
            super(itemView);
        }

        public void bind(int pos){
            ((TextView)itemView).setText("第" + pos + "个item");
            itemView.setBackgroundColor(Color.YELLOW);
            int mode = pos % 6;
            if(mode == 0 ){
                ((TextView) itemView).setWidth(getScreenWidth(getBaseContext()));
            } else if(mode == 1 || mode == 2){
                ((TextView) itemView).setWidth(getScreenWidth(getBaseContext())/2);
                if(mode == 1){
                    itemView.setBackgroundColor(Color.GREEN);
                } else {
                    itemView.setBackgroundColor(Color.RED);
                }
            } else {
                ((TextView) itemView).setWidth(getScreenWidth(getBaseContext())/3);
                if(mode == 3){
                    itemView.setBackgroundColor(Color.BLUE);
                } else if(mode == 4){
                    itemView.setBackgroundColor(Color.LTGRAY);
                } else {
                    itemView.setBackgroundColor(Color.MAGENTA);
                }
            }
        }
    }
}

页面效果:
image

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值