在gridview中添加单独的标头

时间:2015-02-05 09:15:26

标签: android gridview header

我正在使用api调用将数据加载到gridview。我想要有4个节标题,我想为每个标题加载相应的值,我有4个SubCategoryID。现在所有项目都加载到同一个网格

这样做的方法是什么?任何帮助将不胜感激。

enter image description here

public class PFragment extends ListFragment implements OnTaskCompleted {

    GridView grid;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.menu_grid_main, container, false);

        new PMenuAsyncTask(getActivity(), this).execute();

        grid = (GridView) view.findViewById(R.id.grid);
        return view;
    }

    @Override
    public void onTaskCompleted(JSONArray responseJson) {

        try {
            List<String> descriptions = new ArrayList<String>();
            List<String> imageUrls = new ArrayList<String>();

            for (int i = 0; i < responseJson.length(); ++i) {
                JSONObject object = responseJson.getJSONObject(i);

                if ((object.getString("MainCategoryID")).equals("1") && (object.getString("SubCategoryID")).equals("1") ) {
                    Log.i("ImageURL ", object.getString("ImageURL"));
                    imageUrls.add(object.getString("ImageURL"));
                    Log.i("Description ", object.getString("Description"));
                    descriptions.add(object.getString("Description"));
                }

                if ((object.getString("MainCategoryID")).equals("1") && (object.getString("SubCategoryID")).equals("2") ) {
                    Log.i("ImageURL ", object.getString("ImageURL"));
                    imageUrls.add(object.getString("ImageURL"));
                    Log.i("Description ", object.getString("Description"));
                    descriptions.add(object.getString("Description"));
                }

                if ((object.getString("MainCategoryID")).equals("1") && (object.getString("SubCategoryID")).equals("8") ) {
                    Log.i("ImageURL ", object.getString("ImageURL"));
                    imageUrls.add(object.getString("ImageURL"));
                    Log.i("Description ", object.getString("Description"));
                    descriptions.add(object.getString("Description"));
                }

                if ((object.getString("MainCategoryID")).equals("1") && (object.getString("SubCategoryID")).equals("9") ) {
                    Log.i("ImageURL ", object.getString("ImageURL"));
                    imageUrls.add(object.getString("ImageURL"));
                    Log.i("Description ", object.getString("Description"));
                    descriptions.add(object.getString("Description"));
                }
            }

            CustomGrid adapter = new CustomGrid(getActivity(), descriptions, imageUrls);
            grid.setAdapter(adapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:0)

这个过程不可能......没有直接的方法。你可以使用这个StickyGridHeaders

Andorid-StickyHeaderGridView

你可以阅读一些博客,例如StickygridheadersA Sectioned Grid for Android ..