仿横向grid

 

 

https://github.com/pakerfeldt/android-viewflow
public class GridAdapter extends BaseAdapter
{
    private String TAG = "GrideAdapter";
    
    private Context mContext;
    
    private LayoutInflater mInflater;
    
    private List<DishInfo> mData;
    
    private int mRowNum;
    
    private int mColNum;
    
    private int totalPages;
    
    private int mPage;
    
    private int mTotalDataSize;
    
    private int mRemainder;
    
    public GridAdapter(Context context, List<DishInfo> data, int colNum, int rowNum)
    {
        mContext = context;
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mData = data;
        mColNum = colNum;
        mRowNum = rowNum;
        mTotalDataSize = mData.size();
        totalPages = (mTotalDataSize / mColNum / mRowNum);
        mRemainder = mTotalDataSize % (mColNum * mRowNum);
        mPage = 0;
    }
    
    @Override
    public int getCount()
    {
        return mRemainder > 0 ? totalPages + 1 : totalPages;
    }
    
    @Override
    public Object getItem(int position)
    {
        return position;
    }
    
    @Override
    public long getItemId(int position)
    {
        return position;
    }
    
    GAdapter gAdapter;
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            convertView = mInflater.inflate(R.layout.dish_grid, null);
            ((GridView)convertView).setNumColumns(mColNum);
            gAdapter = new GAdapter();
            ((GridView)convertView).setAdapter(gAdapter);
        }
        ((GridView)convertView).setNumColumns(mColNum);
        gAdapter.setPage(position);
        return convertView;
    }
    
    private class GAdapter extends BaseAdapter
    {
        // strat from 0
        private int gmPage;
        
        public void setPage(int page)
        {
            gmPage = page;
        }
        
        @Override
        public int getCount()
        {
            if (gmPage < totalPages)
            {
                return mColNum * mRowNum;
            }
            else
            {
                return mTotalDataSize - (mColNum * mRowNum * gmPage);
            }
        }
        
        @Override
        public Object getItem(int position)
        {
            return mData.get(position + (gmPage * mColNum * mRowNum));
        }
        
        @Override
        public long getItemId(int position)
        {
            return position;
        }
        int rPos = 0;
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ViewHolder holder;
            BitmapFactory.Options options = new BitmapFactory.Options();
//            options.inSampleSize = 10;
            if (convertView == null)
            {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.dish_item, null);
                holder.dishItemNameTextView = (TextView)convertView.findViewById(R.id.dish_item_name);
                holder.dishItemDescTextView = (TextView)convertView.findViewById(R.id.dish_item_des);
                holder.dishItemPriceTextView = (TextView)convertView.findViewById(R.id.dish_item_price);
                holder.dishImageView = (ImageView)convertView.findViewById(R.id.dish_img);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder)convertView.getTag();
            }
            rPos = position + (gmPage * mColNum * mRowNum);
            holder.dishItemNameTextView.setText(mData.get(rPos).getDishName());
            holder.dishItemDescTextView.setText(mData.get(rPos).getDescription());
            holder.dishItemPriceTextView.setText(Float.toString(mData.get(rPos).getPrice()));
            holder.dishImageView.setBackgroundDrawable((new BitmapDrawable(
                BitmapFactory.decodeFile(mData.get(rPos).getSmallpictureUrl(), options))));
            holder.dishImageView.setScaleType(ScaleType.FIT_XY);
            return convertView;
        }
    }
    
    private class ViewHolder
    {
        TextView dishItemNameTextView;
        
        TextView dishItemDescTextView;
        
        TextView dishItemPriceTextView;
        
        ImageView dishImageView;
    }
}

 

 

你可能感兴趣的:(grid)