Is there an addHeaderView equivalent for RecyclerView?

后端 未结 19 1614
独厮守ぢ
独厮守ぢ 2020-11-21 23:35

I\'m looking for an equivalent to addHeaderView for a recycler view. Basically I want to have an image with 2 buttons be added as a header to the listview. Is there a differ

19条回答
  •  孤街浪徒
    2020-11-22 00:22

    Going to show you to make header with items in a Recycler view.

    Step 1- Add dependency into your gradle file.

    compile 'com.android.support:recyclerview-v7:23.2.0'
    // CardView
    compile 'com.android.support:cardview-v7:23.2.0'
    

    Cardview is used for decoration purpose.

    Step2- Make three xml files. One for main activity.Second for Header layout.Third for list item layout.

    activity_main.xml

    
    

    header.xml

    
    
        
    
    
    

    list.xml

    
    
        
    
            
    
        
    
    
    

    Step 3- Create three bean classes.

    Header.java

    public class Header extends ListItem {
        private String header;
    
        public String getHeader() {
            return header;
        }
        public void setHeader(String header) {
            this.header = header;
        }
    }
    

    ContentItem.java

    public class ContentItem extends ListItem {
    
        private String name;
        private String rollnumber;
    
        @Override
        public String getName() {
            return name;
        }
    
        @Override
        public void setName(String name) {
            this.name = name;
        }
    
        public String getRollnumber() {
            return rollnumber;
        }
    
        public void setRollnumber(String rollnumber) {
            this.rollnumber = rollnumber;
        }
    }
    

    ListItem.java

    public class ListItem {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        private int id;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    }
    

    Step 4- Create an adapter named MyRecyclerAdapter.java

    public class MyRecyclerAdapter extends  RecyclerView.Adapter {
        private static final int TYPE_HEADER = 0;
        private static final int TYPE_ITEM = 1;
    
        //Header header;
        List list;
        public MyRecyclerAdapter(List headerItems) {
            this.list = headerItems;
        }
    
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            if (viewType == TYPE_HEADER) {
                View v = inflater.inflate(R.layout.header, parent, false);
                return  new VHHeader(v);
            } else {
                View v = inflater.inflate(R.layout.list, parent, false);
                return new VHItem(v);
            }
            throw new IllegalArgumentException();
        }
    
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof VHHeader) {
               // VHHeader VHheader = (VHHeader)holder;
                Header  currentItem = (Header) list.get(position);
                VHHeader VHheader = (VHHeader)holder;
                VHheader.txtTitle.setText(currentItem.getHeader());
            } else if (holder instanceof VHItem) 
                ContentItem currentItem = (ContentItem) list.get(position);
                VHItem VHitem = (VHItem)holder;
                VHitem.txtName.setText(currentItem.getName());
            }
        }
    
        @Override
        public int getItemViewType(int position) {
            if (isPositionHeader(position))
                return TYPE_HEADER;
            return TYPE_ITEM;
        }
    
        private boolean isPositionHeader(int position) {
            return list.get(position) instanceof Header;
        }
    
        @Override
        public int getItemCount() {
            return list.size();
        }
    
        class VHHeader extends RecyclerView.ViewHolder{
            TextView txtTitle;
            public VHHeader(View itemView) {
                super(itemView);
                this.txtTitle = (TextView) itemView.findViewById(R.id.txtHeader);
            }
        }
        class VHItem extends RecyclerView.ViewHolder{
            TextView txtName;
            public VHItem(View itemView) {
                super(itemView);
                this.txtName = (TextView) itemView.findViewById(R.id.txtName);
            }
        }
    }
    

    Step 5- In MainActivity add the following code:

    public class MainActivity extends AppCompatActivity {
        RecyclerView recyclerView;
        List> arraylist;
        MyRecyclerAdapter adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            recyclerView = (RecyclerView)findViewById(R.id.my_recycler_view);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            adapter = new MyRecyclerAdapter(getList());
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(adapter);
        }
    
        private ArrayList getList() {
            ArrayList arrayList = new ArrayList<>();
            for(int j = 0; j <= 4; j++) {
                Header header = new Header();
                header.setHeader("header"+j);
                arrayList.add(header);
                for (int i = 0; i <= 3; i++) {
                    ContentItem item = new ContentItem();
                    item.setRollnumber(i + "");
                    item.setName("A" + i);
                    arrayList.add(item);
                }
            }
            return arrayList;
        }
    
    }
    

    The function getList() is dynamically generating the data for the headers and for list items.

提交回复
热议问题