Android开发-RecyclerView瀑布流,多ViewHolder,XRecyclerView

1.StaggeredGridLayoutManager瀑布流

效果图

瀑布流效果图

代码

RecyclerView



    


RecyclerViewItem



    


Activity

public class PuRecyclerViewActivity extends AppCompatActivity {

    private RecyclerView mRvPu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pu_recycler_view);
        mRvPu = findViewById(R.id.rv_pu);
        mRvPu.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        mRvPu.addItemDecoration(new MyDecoration());
        mRvPu.setAdapter(new StaggeredAdapter(PuRecyclerViewActivity.this, new StaggeredAdapter.OnItemClickListener() {
            @Override
            public void onClick(int pos) {
                Toast.makeText(PuRecyclerViewActivity.this, "点击:"+pos, Toast.LENGTH_SHORT).show();
            }
        }));
    }

    class MyDecoration extends RecyclerView.ItemDecoration {

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);

            int gap = getResources().getDimensionPixelOffset(R.dimen.dividerHeight);
            outRect.set(gap,gap,gap,gap);
        }
    }
}

Adapter

public class StaggeredAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private OnItemClickListener mListener;

    public StaggeredAdapter(Context context, OnItemClickListener listener) {

        mContext = context;
        mListener = listener;
    }

    @NonNull
    @Override
    public StaggeredAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_staggered_recycler_view_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull StaggeredAdapter.LinearViewHolder holder, final int position) {
        if (position % 2 == 0) {
            holder.imageView.setImageResource(R.drawable.bg_ironman);
        } else  {
            holder.imageView.setImageResource(R.drawable.image1);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onClick(position);
//                Toast.makeText(mContext, "点击:" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

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

    class LinearViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        public LinearViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv);
        }
    }

    public interface OnItemClickListener {
        void onClick(int pos);
    }
}

2.加载不同的ViewHolder

多个ViewHolder效果图

item2



    

    


ViewHolder2

    class LinearViewHolder2 extends RecyclerView.ViewHolder {
        private TextView textView;
        private ImageView imageView;
        public LinearViewHolder2(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_title);
            imageView = itemView.findViewById(R.id.tv_image);
        }
    }

Adapter

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (viewType == 0) {
            return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item, parent, false));
        } else {
            return new LinearViewHolder2(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item2, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        if (getItemViewType(position) == 0) {
            ((LinearViewHolder)holder).textView.setText("Hello World!");
        } else {
            ((LinearViewHolder2)holder).textView.setText("在奔跑!");
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onClick(position);
//                Toast.makeText(mContext, "点击:" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
    
    @Override
    public int getItemViewType(int position) {
        if (position%2 == 0) {
            return 0;
        } else {
            return 1;
        }
    }

3.XRecyclerView: addHeadView、addFootView、下拉刷新、上拉加载

Github链接XRecyclerView

你可能感兴趣的:(Android开发-RecyclerView瀑布流,多ViewHolder,XRecyclerView)