【问题标题】:How to display a title row in a GridView in Android?如何在 Android 的 GridView 中显示标题行?
【发布时间】:2017-01-09 11:35:24
【问题描述】:

我可以在RecyclerView 中使用GridLayoutManager 显示一个GridView。但我需要根据某些条件添加一行名为 title 的行。如何在 GridView 中显示标题行?请参考下图:

我需要像上图一样的UI。项目 1、项目 2 等等都在 GridView 中。标题、标题 2、标题 3 是基于某些条件的标题行。如何显示标题行?

【问题讨论】:

  • 你能分享你的 RecyclerView Adapter 布局代码

标签: android gridview android-recyclerview gridlayoutmanager


【解决方案1】:

试试这个方法

public class MainActivity extends Activity {

    GenericModelAdapter adapter;
    ListView listView;
    private static final int NUMBER_OF_COLS = 4;
    List<Map<String, List<Object>>> items = new ArrayList<Map<String, List<Object>>>();
    Map<String, String> sectionHeaderTitles = new HashMap<String, String>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView)findViewById(R.id.listView);

        initDummyItems();
        adapter = new GenericModelAdapter(this,R.layout.list_item, items, sectionHeaderTitles, NUMBER_OF_COLS, mItemClickListener);
        listView.setAdapter(adapter);
    }

    View.OnClickListener mItemClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = (Integer)v.getTag(R.id.row);
            int col = (Integer)v.getTag(R.id.col);

            Map<String, List<Object>> map = adapter.getItem(position);
            String selectedItemType = adapter.getItemTypeAtPosition(position);
            List<Object> list = map.get(selectedItemType);
            GenericModel model = (GenericModel)list.get(col);
            Toast.makeText(getApplicationContext(), "" + model.getHeader(), Toast.LENGTH_SHORT).show();
        }
    };

    private void initDummyItems(){
        List<String> itemTypesList = new ArrayList<String>();
        itemTypesList.add(Constants.ORANGE);
        itemTypesList.add(Constants.PINEAPPLE);
        itemTypesList.add(Constants.BANANA);
        sectionHeaderTitles.put(Constants.ORANGE, "Oranges");
        sectionHeaderTitles.put(Constants.PINEAPPLE, "Pineapples");
        sectionHeaderTitles.put(Constants.BANANA, "Bananas");

        for (String itemType : itemTypesList){
            Map<String, List<Object>> map = new HashMap<String, List<Object>>();
            List<Object> list = new ArrayList<Object>();

            for (int i = 0 ; i < 10 ; i++){
                String itemName = itemType + " " + i;
                String countryOfOrigin = "Country " + i;
                int image;
                Random rand = new Random();

                Object object = null;
                if (itemType == Constants.BANANA){
                    image = Constants.bananaImages[rand.nextInt(Constants.bananaImages.length)];
                    object = new BananaModel(itemName, countryOfOrigin, image);
                }
                else if(itemType == Constants.ORANGE){
                    image = Constants.orangeImages[rand.nextInt(Constants.orangeImages.length)];
                    object = new OrangeModel(itemName, countryOfOrigin, image);
                }
                else if (itemType == Constants.PINEAPPLE){
                    image = Constants.pineappleImages[rand.nextInt(Constants.pineappleImages.length)];
                    object = new PineappleModel(itemName, countryOfOrigin, image);
                }
                list.add(object);
            }

            map.put(itemType, list);
            items.add(map);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

http://tonicartos.github.io/StickyGridHeaders/

https://github.com/koros/GridViewExample/tree/master

【讨论】:

  • 如果我使用列表视图,它可以工作。你有任何例子如何在recyclerview中做到这一点
【解决方案2】:

您可以使用SectionedGridView 来完成您的任务。

我对SectionedGridView有个人经验,它非常好用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多