Android GridView与类别?

时间:2011-09-13 07:01:59

标签: android gridview android-3.0-honeycomb

是否可以在Android中使用带有GridView的类别或某种标题?

我快速说明了我的想法:

enter image description here

非常感谢。

4 个答案:

答案 0 :(得分:15)

您可以直接使用Stickygridheaders库或作为模型来创建自己的小部件。

答案 1 :(得分:3)

可能this code会对您有所帮助。这是SectionedGridRecyclerViewAdapter,结果如下:

enter image description here

答案 2 :(得分:2)

我认为你可以做到,但你必须实施Jeff Shrkey的SeparatedListAdapter

  

没有一种简单的方法来创建这些分隔的列表,因此我将SeparatedListAdapter放在一起,它可以快速完成。总而言之,我们正在创建一个新的BaseAdapter,它可以包含其他几个适配器,每个适配器都有自己的节头。

答案 3 :(得分:2)


您可以修改常用的listview适配器以返回每行的网格,请参阅 here

public GenericModelAdapter(Context context, int textViewResourceId, List<Map<String, List<Object>>> items, Map<String, String> sectionHeaderTitles, int numberOfCols, View.OnClickListener mItemClickListener){
    super(context, textViewResourceId, items);
    this.items = items;
    this.numberOfCols = numberOfCols;
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.mItemClickListener = mItemClickListener;
    this.sectionHeaderTitles = sectionHeaderTitles;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(isHeaderPosition(position)){
        convertView = layoutInflater.inflate(R.layout.grid_header_view, null);

        TextView headerText = (TextView)convertView.findViewById(R.id.headerText);
        String section = getItemTypeAtPosition(position);
        headerText.setText(getHeaderForSection(section));
        return convertView;
    }else{
        LinearLayout row = (LinearLayout)layoutInflater.inflate(R.layout.row_item, null);
        Map<String, List<Object>> map = getItem(position);
        List<Object> list = map.get(getItemTypeAtPosition(position));

        for (int i = 0; i < numberOfCols; i++){
            FrameLayout grid = (FrameLayout)layoutInflater.inflate(R.layout.grid_item, row, false);
            ImageView imageView;
            if (i < list.size()){
                GenericModel model = (GenericModel)list.get(i);
                if (grid != null){
                    imageView = (ImageView)grid.findViewWithTag("image");
                    imageView.setBackgroundResource(model.getImageResource());

                    TextView textView = (TextView)grid.findViewWithTag("subHeader");
                    textView.setText(model.getHeader());

                    grid.setTag(R.id.row, position);
                    grid.setTag(R.id.col, i);
                    grid.setOnClickListener(mItemClickListener);
                }
            }else{
                if (grid != null){
                    grid.setVisibility(View.INVISIBLE);
                    grid.setOnClickListener(null);
                }
            }
            row.addView(grid);
        }
        return row;
    }
}

@Override
public int getCount() {
    int totalItems = 0;
    for (Map<String, List<Object>> map : items){
        Set<String> set = map.keySet();
        for(String key : set){
            //calculate the number of rows each set homogeneous grid would occupy
            List<Object> l = map.get(key);
            int rows = l.size() % numberOfCols == 0 ? l.size() / numberOfCols : (l.size() / numberOfCols) + 1;

            // insert the header position
            if (rows > 0){
                headerPositions.add(String.valueOf(totalItems));
                offsetForItemTypeMap.put(key, totalItems);

                itemTypePositionsMap.put(key, totalItems + "," + (totalItems + rows) );
                totalItems += 1; // header view takes up one position
            }
            totalItems+= rows;
        }
    }
    return totalItems;
}

@Override
public Map<String, List<Object>> getItem(int position) {
    if (!isHeaderPosition(position)){
        String itemType = getItemTypeAtPosition(position);
        List<Object> list = null;
        for (Map<String, List<Object>> map : items) {
            if (map.containsKey(itemType)){
                list = map.get(itemType);
                break;
            }
        }
        if (list != null){
            int offset = position - getOffsetForItemType(itemType);
            //remove header position
            offset -= 1;
            int low = offset * numberOfCols;
            int high = low + numberOfCols  < list.size() ? (low + numberOfCols) : list.size();
            List<Object> subList = list.subList(low, high);
            Map<String, List<Object>> subListMap = new HashMap<String, List<Object>>();
            subListMap.put(itemType, subList);
            return subListMap;
        }
    }
    return null;
}

public String getItemTypeAtPosition(int position){
    String itemType = "Unknown";
    Set<String> set = itemTypePositionsMap.keySet();

    for(String key : set){
        String[] bounds = itemTypePositionsMap.get(key).split(",");
        int lowerBound = Integer.valueOf(bounds[0]);
        int upperBoundary = Integer.valueOf(bounds[1]);
        if (position >= lowerBound && position <= upperBoundary){
            itemType = key;
            break;
        }
    }
    return itemType;
}

public int getOffsetForItemType(String itemType){
    return offsetForItemTypeMap.get(itemType);
}

public boolean isHeaderPosition(int position){
    return headerPositions.contains(String.valueOf(position));
}

private String getHeaderForSection(String section){
    if (sectionHeaderTitles != null){
        return sectionHeaderTitles.get(section);
    }else{
        return section;
    }
}

GridView with sample fruit categories