gpt4 book ai didi

android - 如何在 Gridview 中显示标题?

转载 作者:行者123 更新时间:2023-11-30 00:58:01 25 4
gpt4 key购买 nike

我有一个 gridview,如您在第一张图片中所见。现在我想将 header 添加到 gridview 的某些特定部分,如第二张图片所示。我正在努力实现这一目标。

我可以知道如何实现这个 View 吗。

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;
}

最佳答案

尝试 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

关于android - 如何在 Gridview 中显示标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39850694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com