Android网格布局实现--recyclerview

接上一篇内容,使用RecyclerView实现;

    android:id="@+id/rvGrid"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_marginRight="30px"
    android:layout_marginTop="20px"
    android:layout_weight="1"

    android:splitMotionEvents="false" />

java代码:

     mPhotoWallRecyclerView.setLayoutManager(mSGLM);
        mPhotoWallRecyclerView.setHasFixedSize(true);
        mPhotoWallRecyclerView.addOnScrollListener(mOnScrollListener);


        mPhotoWallAdapter = new PhotoWallAdapter2(mCtx, null);
        mPhotoWallAdapter.setAddrCardListener(mAddrCardListener);
        // Set the RecyclerView's Adapter
        mPhotoWallRecyclerView.setAdapter(mPhotoWallAdapter);

适配器代码

public class PhotoWallAdapter2 extends RecyclerView.Adapter {

    private ArrayList mDataset;
    private List mHeights;
    private AddrCardListener mListener;
    private DisplayImageOptions options;

    public PhotoWallAdapter2(Context context, ArrayList myDataset) {
        if (myDataset == null)
            this.mDataset = new ArrayList();
        else
            this.mDataset = myDataset;

        options = new DisplayImageOptions.Builder()

                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    public void getRandomHeight() {
        mHeights = new ArrayList();
        int min = 252;
        for (int i = 0; i < mDataset.size(); i++) {
            //随机的获取一个范围为200-600直接的高度
            String name = mDataset.get(i).getName();
            int w = (name.length() + 1) * 24 + 36;
            int ww = Math.max(min, w);
            mHeights.add(ww);
        }
    }

    public void setAddrCardListener(AddrCardListener listener) {
        mListener = listener;
    }

    private void setAdapterListener(MyViewHolder holder) {
        if (mListener == null) return;
        if (holder.tvTitle.getVisibility() == View.GONE) {
            return;
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyViewHolder holder = (MyViewHolder) v.getTag();
                final int position = holder.getLayoutPosition();
                notifyItemChanged(position);
                mListener.onAddrItemClick(position);
            }
        });
        holder.itemView.setTag(holder);

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Create a new view by inflating the row item xml.
        final View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.photo_card_cell2, parent, false);

        MyViewHolder holder = new MyViewHolder(v);
        setAdapterListener(holder);

        return holder;
    }

    public void addData(List datalist) {
        if ((datalist != null) && (datalist.size() > 0)) {
            mDataset.clear();
            notifyDataSetChanged();

            mDataset.addAll(getItemCount(), datalist);

            getRandomHeight();
            notifyItemChanged(0);
            notifyDataSetChanged();
        }
    }

    public ArrayList getData() {
        return mDataset;
    }

    public CardAddrItem getItem(int position) {
        return (mDataset != null && position >= 0 && position < mDataset.size()) ?
                mDataset.get(position) : null;
    }

    @Override
    public int getItemCount() {
        return (mDataset == null) ? 0 : mDataset.size();
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        CardAddrItem map = mDataset.get(position);
        holder.tvTitle.setText(map.getName().trim());
        holder.tvTitle.setVisibility(View.VISIBLE);
        holder.tvAddress.setText(map.getAddress().trim());
        holder.tvAddress.setVisibility(View.VISIBLE);


        String url = map.getUrl();
        holder.ivPhoto.setTag(holder.spinner);
        holder.ivPhoto.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ImageLoader.getInstance().displayImage(url, holder.ivPhoto, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                holder.spinner.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                String message = null;
                switch (failReason.getType()) {
                    case IO_ERROR:
                        message = "Input/Output error";
                        break;
                    case DECODING_ERROR:
                        message = "Image can't be decoded";
                        break;
                    case NETWORK_DENIED:
                        message = "Downloads are denied";
                        break;
                    case OUT_OF_MEMORY:
                        message = "Out Of Memory error";
                        break;
                    case UNKNOWN:
                        message = "Unknown error";
                        break;
                }
                Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();

                holder.spinner.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                holder.spinner.setVisibility(View.GONE);
            }
        }, new ImageLoadingProgressListener() {
            @Override
            public void onProgressUpdate(String imageUri, View view, int current, int total) {
                holder.spinner.setProgress(Math.round(100.0f * current / total));
            }
        });
    }

    /**
     * Create a ViewHolder to represent your cell layout
     * and data element structure
     */
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView ivPhoto;
        TextView tvTitle;
        TextView tvAddress;
        View llCardBack;
        ProgressBar spinner;

        public MyViewHolder(View itemView) {
            super(itemView);

            ivPhoto = (ImageView) itemView.findViewById(R.id.photo);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            tvAddress = (TextView) itemView.findViewById(R.id.tvAddress);
            llCardBack = itemView.findViewById(R.id.llCardBack);
            spinner = (ProgressBar) itemView.findViewById(R.id.loading);
        }
    }

    public interface AddrCardListener {
        void onAddrItemClick(int position);

        void onAddrNaviClick(int position);

        void onAddrCollClick(int position);
    }



你可能感兴趣的:(android)