流式布局 ChipsLayoutManager

ChipsLayoutManager
ChipsLayoutManager github地址
流式布局 ChipsLayoutManager_第1张图片


android:layoutDirection=“ltr” 从左到右

public static void initFlowLayout(Context context, RecyclerView recyclerView, ChipsAdapter adapter){
        adapter.setOnRemoveListener(new MyRemoveListener(adapter));
        ChipsLayoutManager chipsLayoutManager = ChipsLayoutManager.newBuilder(context)
                //set vertical gravity for all items in a row. Default = Gravity.CENTER_VERTICAL
                .setChildGravity(Gravity.TOP)
                //whether RecyclerView can scroll. TRUE by default
                .setScrollingEnabled(true)
                //set maximum views count in a particular row
//                .setMaxViewsInRow(2)
                //set gravity resolver where you can determine gravity for item in position.
                //This method have priority over previous one
                .setGravityResolver(new IChildGravityResolver() {
                    @Override
                    public int getItemGravity(int position) {
                        return Gravity.CENTER;
                    }
                })
                //a layoutOrientation of layout manager, could be VERTICAL OR HORIZONTAL. HORIZONTAL by default
                .setOrientation(ChipsLayoutManager.HORIZONTAL)
                // row strategy for views in completed row, could be STRATEGY_DEFAULT, STRATEGY_FILL_VIEW,
                //STRATEGY_FILL_SPACE or STRATEGY_CENTER
                .setRowStrategy(ChipsLayoutManager.STRATEGY_DEFAULT)
                // whether strategy is applied to last row. FALSE by default
                .withLastRow(true)
                .build();
        recyclerView.setLayoutManager(chipsLayoutManager);
        recyclerView.setAdapter(adapter);
    }

    private static class MyRemoveListener implements OnRemoveListener {

        private ChipsAdapter adapter;

        public MyRemoveListener(ChipsAdapter adapter){
            this.adapter = adapter;
        }

        @Override
        public void onItemRemoved(int position) {
            adapter.getChipsEntitys().remove(position);
            adapter.notifyItemRemoved(position);
        }
    }

ChipsLayoutManager.STRATEGY_DEFAULT,默认左对齐或右对齐
ChipsLayoutManager.STRATEGY_FILL_SPACE, item左右两边分布,中间空格

setGravityResolver(new IChildGravityResolver() ) 可能报错,看实际选择
java.lang.IllegalArgumentException: bottom point of input rect can’t be bigger than maxTop

.setRowBreaker(new IRowBreaker() {
 @Override
      public boolean isItemBreakRow(@IntRange(from = 0) int position) {
          return position == 6 || position == 11 || position == 2;
      }
  })

//对应position的item,如果右边可再放置一个item,可放置的item必须放下一行,对应position不受影响,不用单独放上一行。

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.lianyucn.szfjzxy.R;
import com.lianyucn.szfjzxy.bean.ChipsEntity;
import com.lianyucn.szfjzxy.tool.listener.OnRemoveListener;

import java.util.List;


public class ChipsAdapter extends  RecyclerView.Adapter {

    private List chipsEntities;
    private OnRemoveListener onRemoveListener;

    public ChipsAdapter(List chipsEntities) {
        this.chipsEntities = chipsEntities;
    }

    public ChipsAdapter(List chipsEntities, OnRemoveListener onRemoveListener) {
        this.chipsEntities = chipsEntities;
        this.onRemoveListener = onRemoveListener;
    }

    public ChipsAdapter(List chipsEntities, OnRemoveListener onRemoveListener, boolean isShowingPosition) {
        this.chipsEntities = chipsEntities;
        this.onRemoveListener = onRemoveListener;
    }

    public void setOnRemoveListener(OnRemoveListener onRemoveListener){
        this.onRemoveListener = onRemoveListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chip, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bindItem(chipsEntities.get(position));
    }

    @Override
    public int getItemCount() {
        return chipsEntities.size();
    }

    public List getChipsEntitys(){
        return chipsEntities;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvName;
        private ImageButton ibClose;

        ViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tvName);
            ibClose = (ImageButton) itemView.findViewById(R.id.ibClose);
        }

        void bindItem(ChipsEntity entity) {
            itemView.setTag(entity.getName());

            tvName.setText(entity.getName());

            ibClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onRemoveListener != null && getAdapterPosition() != -1) {
                        onRemoveListener.onItemRemoved(getAdapterPosition());
                    }
                }
            });
        }
    }

}


自定义布局



    
    


布局:
在这里插入图片描述

你可能感兴趣的:(流式布局 ChipsLayoutManager)