RecyclerView瀑布流实现第一个item占两行

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40391500/article/details/80268788

在使用应用的时候经常看到网格模式列表中第一个item占两行,自己也实现了下

activity:

    private RecyclerView rv_list;
    private StaggeredGLMAdapter adapter;

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

        rv_list = (RecyclerView) findViewById(R.id.rv_list);

        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);
        rv_list.setLayoutManager(manager);

        adapter = new StaggeredGLMAdapter(this, 10);
        rv_list.setAdapter(adapter);

    }
adapter:

public class StaggeredGLMAdapter extends RecyclerView.Adapter<StaggeredGLMAdapter.ViewHodler> {

    private Context mContext;
    private LayoutInflater mInflater;
    private int mSize;

    public StaggeredGLMAdapter(Context context, int size){
        mContext = context;
        mSize = size;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public ViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.staggered_item, null);
        ViewHodler hodler = new ViewHodler(view);
        return hodler;
    }

    @Override
    public void onBindViewHolder(ViewHodler holder, int position) {
        if (position == 0){
            ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) holder.layout.getLayoutParams();
            lp.height = lp.height*2;
            holder.layout.setLayoutParams(lp);
        }
        holder.msg.setText("哈哈 " + position);
    }

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

    class ViewHodler extends RecyclerView.ViewHolder{

        private LinearLayout layout;
        private ImageView img;
        private TextView msg;

        public ViewHodler(View view) {
            super(view);
            layout = (LinearLayout) view.findViewById(R.id.layout);
            img = (ImageView) view.findViewById(R.id.img);
            msg = (TextView) view.findViewById(R.id.msg);
        }
    }
}
item.xml:

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:orientation="vertical"
        android:gravity="center">

        <ImageView
            android:id="@+id/img"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:background="@mipmap/ic_launcher"/>

        <TextView
            android:id="@+id/msg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="nihao"/>
    </LinearLayout>

猜你喜欢

转载自blog.csdn.net/weixin_40391500/article/details/80268788
0条评论
添加一条新回复