RecyclerView 流式布局

RecyclerView 流式布局的一个demo,从网上下载的,我稍微改了一下,随手记下来备用.
另外这个demo的代码忘记是从哪个blog下载的了,后续找到了我会补上link
RecyclerView 流式布局_第1张图片

主要有几个类:
Activity


public class MainActivity extends Activity {
    RecyclerView mRecyclerView;
    ArrayList mProductList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycleview);
        //设置layoutManager
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        //设置adapter
        initData();
        MasonryAdapter adapter = new MasonryAdapter(mProductList);
        mRecyclerView.setAdapter(adapter);
        //设置item之间的间隔
        SpacesItemDecoration decoration = new SpacesItemDecoration(16);
        mRecyclerView.addItemDecoration(decoration);

    }

    private int getRandomInt(int size) {
        Random random = new Random();
        return random.nextInt(size);
    }

    int[] ids = {R.drawable.img0, R.drawable.img1, R.drawable.img2, R.drawable.img3};
    String title = "asdasdasdasdasdasdasd";
    String[] titles = {title + title, title + title + title, title, title + title + title + title};

    void initData() {
        if (mProductList == null) {
            mProductList = new ArrayList();
        }
        for (int i = 0; i < 40; i++) {
            Product p = new Product(ids[getRandomInt(4)], titles[getRandomInt(4)]);
            mProductList.add(p);
        }
    }
}

Adapter

public class MasonryAdapter extends RecyclerView.Adapter<MasonryAdapter.MasonryView>{
    private List products;


    public MasonryAdapter(List list) {
        products=list;
    }

    @Override
    public MasonryView onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        return new MasonryView(view);
    }

    @Override
    public void onBindViewHolder(MasonryView masonryView, int position) {
        masonryView.imageView.setImageResource(products.get(position).getImg());
        masonryView.textView.setText(products.get(position).getTitle()+"----"+String.valueOf(position));
//        masonryView.textView.setText(String.valueOf(position));
    }

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

    public static class MasonryView extends  RecyclerView.ViewHolder{

        ImageView imageView;
        TextView textView;

        public MasonryView(View itemView){
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.img );
            textView= (TextView) itemView.findViewById(R.id.title);
        }

    }

}

还有这个比较重要

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {

    private int space;

    public SpacesItemDecoration(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;
        //第一个不设置间隔,否则顶部有空白
        if (parent.getChildPosition(view) == 0 || parent.getChildPosition(view) == 1) {
            outRect.top = space;
        }
    }
}

你可能感兴趣的:(实际应用,转载-存档)