RecycleView一个敲简单的瀑布流实现、如何实现流式布局?

实现效果:

一、创建一个activity,在布局文件XML中写入recycleview控件

    

二、在刚创建的activity初始化recycle并为其设置布局管理器适配器等:

public class RecycleViewActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerAdapter recyclerAdapter;

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

    private void initView() {
        recyclerView = findViewById(R.id.recycleview);
        //设置布局管理器
        //列表布局
//        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //表格布局
//        recyclerView.setLayoutManager(new GridLayoutManager(this,2));
        //瀑布流布局
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        //设置adapter
        recyclerAdapter = new RecyclerAdapter();
        recyclerView.setAdapter(recyclerAdapter);
        //添加分割线
        recyclerView.addItemDecoration(new RecycleItemDecoration(15));

       //item的点击和长按事件
        recyclerAdapter.setOnItemClickLitener(new RecyclerAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getBaseContext(),"点击事件"+position,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(getBaseContext(),"长按监听事件"+position,Toast.LENGTH_SHORT).show();
            }
        });

    }

}

三、分割线如何写:

public class RecycleItemDecoration extends RecyclerView.ItemDecoration{
    private int space;

    public RecycleItemDecoration(int space) {
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.left = space;
        outRect.right = space;
        outRect.bottom = space; 
// Add top margin only for the first item to avoid double space between items if (parent.getChildPosition(view) == 0) outRect.top = space; }

    }
}

三、写一个适配器的item布局:




    
    

        

        
    

    

四、适配器代码如下:(由于recycleview高度耦合 所以点击事件需要自己写)

public class RecyclerAdapter extends RecyclerView.Adapter {
    private View view;
    private List heights = new ArrayList<>();

    @Override
    public RecycleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载item布局
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, null);
        //new一个ViewHolder
        return new RecycleHolder(view);
    }

    @Override
    public void onBindViewHolder(RecycleHolder holder, int position) {
        //设置动态高度
        heights.add(300);
        heights.add(400);
        ViewGroup.LayoutParams layoutParams = holder.recycler_item_image.getLayoutParams();
        layoutParams.height = heights.get(position);

        //item的点击事件传递数据
        holder.item_cardview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickLitener.onItemClick(holder.itemView, position);
            }
        });
        holder.item_cardview.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int pos = holder.getLayoutPosition();
                onItemClickLitener.onItemLongClick(holder.itemView, pos);
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return 30;
    }
//===========================点击事件==========================================
    OnItemClickLitener onItemClickLitener;

    public OnItemClickLitener getOnItemClickLitener() {
        return onItemClickLitener;
    }

    public void setOnItemClickLitener(OnItemClickLitener onItemClickLitener) {
        this.onItemClickLitener = onItemClickLitener;
    }

    public interface OnItemClickLitener {
        /*点击事件*/
        void onItemClick(View view, int position);
        /*长按事件*/
        void onItemLongClick(View view, int position);
    }
//=========================点击事件==========================================
}

class RecycleHolder extends RecyclerView.ViewHolder {
    ImageView recycler_item_image;
    CardView item_cardview;

    //查找控件id
    public RecycleHolder(View itemView) {
        super(itemView);
        recycler_item_image = (ImageView) itemView.findViewById(R.id.recycler_item_image);
        item_cardview = itemView.findViewById(R.id.recycleview_cardview);
    }

}

 

你可能感兴趣的:(RecycleView一个敲简单的瀑布流实现、如何实现流式布局?)