简体   繁体   English

如何为回收站 gridview 设置多个跨度并显示页眉和页脚

[英]How to set multiple span for recycler gridview and show header and footer

I have a fragment listing couple of items from an array where array is filled from a API, Also I have created a fragment class and a recycler adapter where I had called Layout manager as grid view.我有一个片段列出了数组中的几个项目,其中数组是从 API 填充的,我还创建了一个片段类和一个回收器适配器,我将布局管理器称为网格视图。 my view have a header contains two item of array and a footer of ad.我的视图有一个标题,其中包含两个数组项和一个广告页脚。 I successfully implemented the header but am not getting my footer properly.我成功实现了页眉,但没有正确获取页脚。 suggested ways are valuable without a major change.建议的方法在没有重大改变的情况下很有价值。

Fragment分段

  mRecyclerView = (RecyclerView) view.findViewById(R.id.mrecycler_channel);
        mRecyclerView.setHasFixedSize(true);
        GridLayoutManager gm = new GridLayoutManager(getActivity(),2);
        gm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return position == 0 ? 2:1;
            }
        });
        mRecyclerView.setLayoutManager(gm);
        mAdapter = new StationsListAdapter(getActivity(), listStations, listlikedStations, listner);
        mRecyclerView.setAdapter(mAdapter);

recyclerview Adapter回收视图适配器

      @Override
        public int getItemCount() {
        int array = listAddress.size();
            Log.d("abhil",""+array);
    //        if(listAddress.size()>0) {
                return listAddress.size() - 1;
    //        }else{
    //            return 0;
    //        }
        }

        @Override
        public int getItemViewType(int position) {
            if (position == 0) {
                return TYPE_HEADER;
            } else if(isPositionFooter(position)){
                return AD_TYPE;
            }
            return TYPE_ITEM;
        }
 private boolean isPositionFooter (int position) {
        return position == listAddress.size() + 1;
    }

need to set ad on last position as a normal banner ad showing in Recyclerlistview.需要在最后一个位置设置广告作为在 Recyclerlistview 中显示的普通横幅广告。

You could achieve it like this:你可以这样实现:

gm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            //Do your lookup here

            return (position == 0 || position == listStations.size() - 1) ? 2 : 1;
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM