2016-10-04 84 views
1

我有一個gridview,你可以在第一張圖片中看到。現在我想添加標題到gridview的某個特定部分,如同第二張圖。我正在努力實現這一目標。如何在Gridview中標題?

我想知道如何實現這個觀點。

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

ISIT解決????? –

回答

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

+0

謝謝我使用了StickyHeader這種工作,但目前它滾動時崩潰。它在「 holder.productTitle.setText(productItem.getTitle()。toString());」提供空指針對象引用 –

+0

NPE意味着你的settext中有問題..檢查它..如果答案可以幫助你接受它.. –

+0

我知道NPE,但我想知道爲什麼它使用StickyHeader之前,但現在不工作。謝謝你的幫助 :) –