用RecyclerView实现瀑布流布局
2015-04-30 15:23
253 查看
1.布局文件
RecyclerView布局文件:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffcccccc" > <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recyclerview"> </android.support.v7.widget.RecyclerView> </LinearLayout>
item布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="5dp"android:gravity="center_horizontal"android:background="#ffffffff"><RelativeLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/thumbnail_layout"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:scaleType="fitXY"android:id="@+id/product_thumbnail_imageview"/><TextView android:layout_width="match_parent"android:layout_height="30dp"android:textSize="14sp"android:gravity="center"android:background="#cc555555"android:text="可爱的小绵羊"android:textColor="#ffffffff"android:id="@+id/product_name_textview"android:layout_alignBottom="@id/product_thumbnail_imageview"/></RelativeLayout><RelativeLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingTop="10dp"android:layout_below="@id/thumbnail_layout"><TextView android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:maxLines="3"android:textSize="14sp"android:lineSpacingExtra="5dp"android:textColor="#ff000000"android:text=""android:id="@+id/product_description_textview"/><RelativeLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingTop="5dp"android:paddingBottom="5dp"android:layout_below="@id/product_description_textview"><ImageButton android:layout_width="30dp"android:layout_height="30dp"android:layout_marginLeft="10dp"android:maxHeight="400dp"android:id="@+id/hot_imagebutton"android:background="@drawable/pinus"android:layout_alignParentLeft="true"/><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@id/hot_imagebutton"android:layout_centerVertical="true"android:textSize="16sp"android:textColor="#ffcccccc"android:text="45"android:id="@+id/hot_count_textview"/><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="10dp"android:layout_centerVertical="true"android:layout_alignParentRight="true"android:id="@+id/textview"android:text="元"android:textSize="16sp"android:textColor="#ff000000"/><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="2dp"android:text="185"android:layout_toLeftOf="@id/textview"android:layout_centerVertical="true"android:textSize="16sp"android:textColor="#ff00ff"android:id="@+id/price_textview"/></RelativeLayout></RelativeLayout>
2.MainActivity.class
private RecyclerView mRecyclerView;
private WaterFallAdapter waterFallAdapter;
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);waterFallAdapter = new WaterFallAdapter(mProductList, this);mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));mRecyclerView.setAdapter(waterFallAdapter);mRecyclerView.setOnTouchListener(this);
3.WaterFallAdapter.class
public class WaterFallAdapter extends RecyclerView.Adapter {private List<Product> mLists;private LayoutInflater mLayoutInflater;private OnItemClickLitener mOnItemClickLitener;private BitmapUtils bitmapUtils;private int startItem;private Context context;public WaterFallAdapter(List<Product> mLists, Context mContext) {this.mLists = mLists;this.mLayoutInflater = LayoutInflater.from(mContext);bitmapUtils = new BitmapUtils(mContext);this.context = mContext;}@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {MyViewHolder viewHolder = new MyViewHolder(mLayoutInflater.inflate(R.layout.item, viewGroup, false));return viewHolder;}private int currentHots;@Overridepublic void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {Product product = mLists.get(position);// currentHots = product.getHot();final MyViewHolder holder = (MyViewHolder) viewHolder;holder.priceTextView.setText(product.getPrice() + "");holder.descriptionTv.setText(product.getDescription());holder.hotCountTv.setText(String.valueOf(product.getHot()));holder.productNameTv.setText(product.getName());// holder.thumbnailImageView.setMaxHeight(160);bitmapUtils.display(holder.thumbnailImageView, product.getUrl());holder.hotImageBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {currentHots = currentHots++;// holder.hotCountTv.setText(currentHots + "");Toast.makeText(context, "Remoted", Toast.LENGTH_SHORT).show();}});// 如果设置了回调,则设置点击事件startItem = mLists.size();if (mOnItemClickLitener != null) {holder.itemView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {int pos = holder.getLayoutPosition();mOnItemClickLitener.onItemClick(holder.itemView, pos);}});}}@Overridepublic int getItemCount() {return mLists.size();}public void addData(List<Product> products) {mLists.addAll(products);// notifyItemInserted(products);notifyItemRangeInserted(startItem, getItemCount());}public void removeData(int position) {mLists.remove(position);notifyItemRemoved(position);}class MyViewHolder extends RecyclerView.ViewHolder {//名字TextView productNameTv;//缩略图ImageView thumbnailImageView;//心形图片ImageButton hotImageBtn;//点击数TextView hotCountTv;//产品价格TextView priceTextView;//产品描述TextView descriptionTv;public MyViewHolder(View itemView) {super(itemView);productNameTv = (TextView) itemView.findViewById(R.id.product_name_textview);thumbnailImageView = (ImageView) itemView.findViewById(R.id.product_thumbnail_imageview);priceTextView = (TextView) itemView.findViewById(R.id.price_textview);descriptionTv = (TextView) itemView.findViewById(R.id.product_description_textview);hotImageBtn = (ImageButton) itemView.findViewById(R.id.hot_imagebutton);hotCountTv = (TextView) itemView.findViewById(R.id.hot_count_textview);}}public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {this.mOnItemClickLitener = mOnItemClickLitener;}public interface OnItemClickLitener {void onItemClick(View view, int position);void onItemLongClick(View view, int position);}}4.下载源码http://pan.baidu.com/s/1bnk4znx
相关文章推荐
- Recycleview实现复杂页面 三种以上布局 瀑布流 多布局 scrollview嵌套recyclerView 显示不全 滑动冲突 之进阶终极篇
- Recycleview实现复杂页面三种以上布局 瀑布流多布 scrollview嵌套recyclerView 显示不全滑动冲突
- RecyclerView 实现横向滚动与瀑布流布局
- RecyclerView实现瀑布流布局
- RecyclerView的Item点击事件,增加删除Item瀑布流动画效果,长按拖动Item,RecyclerView复杂布局、实现新闻频道选择器
- android开发之&使用RecyclerView实现瀑布流布局(一)
- RecyclerView和ArrayAdapter的实现_瀑布流布局
- RecyclerView实现瀑布流布局
- recyclerview实现瀑布流布局
- Android RecyclerView实现瀑布流布局
- RecyclerView实现瀑布流布局
- android开发之&使用RecyclerView实现瀑布流布局(二)
- Android RecyclerView瀑布流布局添加Footer实现上拉加载
- Recyclerview实现瀑布流布局及条目的点击事件
- RecyclerView实现瀑布流布局
- 用RecyclerView的瀑布流布局实现表格展示
- RecyclerView实现瀑布流布局
- Android RecyclerView瀑布流布局添加Footer实现上拉加载
- recyclerview实现瀑布流效果,加载本地图片
- Fresco和 RecyclerView 实现瀑布流 <图片是用本地图片来模拟网络图片的>