如何在Gridview中标题?

时间:2016-10-04 11:07:24

标签: android gridview

我在第一张照片中看到gridview。现在我想将标题添加到gridview的某个特定部分,如第2张图片所示。我正在努力实现这一目标。

我可以知道如何实现这种观点。

enter image description here enter image description here

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        holder = null;

        Product productItem = (Product) productList.get(position);

        if (row == null) {


                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(R.layout.products_grid_item_layout, parent, false);
                holder = new ViewHolder();
                holder.productImage = (ImageView) row.findViewById(R.id.productImage);
                holder.productImageClone = (ImageView) row.findViewById(R.id.productImageClone);
                holder.detailsIcon = (ImageView) row.findViewById(R.id.detailsIcon);
                holder.productTitle = (TextView) row.findViewById(R.id.productTitle);
                holder.productSubTitle = (TextView) row.findViewById(R.id.productSubTitle);
                holder.productQuantity = (TextView) row.findViewById(R.id.productQuantity);
                holder.priceDollar = (TextView) row.findViewById(R.id.priceDollar);
                holder.priceCent = (TextView) row.findViewById(R.id.priceCent);
                holder.productCount = (TextView) row.findViewById(R.id.productCount);
                holder.productGridLayout = (RelativeLayout) row.findViewById(R.id.productGridLayout);

                row.setTag(holder);

        } else {
            holder = (ViewHolder) row.getTag();
        }



        String price = productItem.getPrice().toString();

        String[] pricearray;
        pricearray =  price.split("\\.");

        holder.productTitle.setText(productItem.getTitle().toString());
        holder.productSubTitle.setText(productItem.getSubtitle().toString());
        holder.productQuantity.setText(productItem.getVolume().toString());
        holder.priceDollar.setText(pricearray[0]+".");
        holder.priceCent.setText(pricearray[1]);


        if(productItem.getInCart()) {
            holder.productCount.setVisibility(View.VISIBLE);
            holder.productCount.setText(productItem.getVolume());
        }

        Picasso.with(context)
                .load(productItem.getImageUrl())
                .placeholder(R.drawable.favourites)
                .error(R.drawable.favourites)
                .into(holder.productImage);

        Picasso.with(context)
                .load(productItem.getImageUrl())
                .placeholder(R.drawable.favourites)
                .error(R.drawable.favourites)
                .into(holder.productImageClone);

        urlSlug = productItem.getUrlSlug();


        holder.detailsIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final int position = currentGridView.getPositionForView(v);
                Log.e("grid position", position+"");

                Intent productDetailIntent = new Intent(context,ProductDetailActivity.class);
                productDetailIntent.putExtra("url_slug", productList.get(position).getUrlSlug());

                context.startActivity(productDetailIntent);
            }

        });

        return row;
    }

1 个答案:

答案 0 :(得分:1)

尝试this方式添加标题

public class ListAsGridExampleAdapter extends ListAsGridBaseAdapter {

    Integer[] mArray = new Integer[] {1,2,3,4,5,6,7,8,9,10,11,12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};

    private LayoutInflater mInflater;

    public ListAsGridExampleAdapter(Context context) {
        super(context);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public Integer getItem(int position) {
        return mArray[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mArray.length;
    }

    @Override
    protected View getItemView(int position, View view, ViewGroup parent) {
        if (view == null) {
            view = mInflater.inflate(R.layout.simple_list_item, null);
        }
        TextView tv = (TextView) view.findViewById(R.id.text);        
        tv.setText(String.valueOf(getItem(position)));
        return view;
    }

}

https://android.googlesource.com/platform/packages/apps/Gallery2/+/idea133/src/com/android/photos/views/HeaderGridView.java

https://github.com/liaohuqiu/android-GridViewWithHeaderAndFooter

网格Header和页脚

您还可以使用StickyHeader