Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add Header in RecyclerView without using any external library?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.75k
    Comment on it

    In the below example code I have described how to add Header in RecyclerView without using any external library. For that, Firstly, I have created a header.xml layout and here I have added a TextView.

    Now see programing In MyAdapter class I have declared constant variable for header and recycler view items. In onCreateViewHolder method I have inflated both header.xml and list_item.xml layout. In onBindViewHolder method I have set text header name. After this, I have used getItemViewType method for set header postion .

     

     

    Step(1)Created a new header.xml layout-
     

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
    
        <TextView
            android:id="@+id/txt_header"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textStyle="bold"
            android:background="@android:color/holo_orange_dark"
            android:gravity="center"
            android:textColor="@android:color/white"
            android:textSize="24dp"/>
    </LinearLayout>

     

    Step(2)-MyAdapter class-

    public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
        private static final int TYPE_HEADER=0;
        private static final int TYPE_ITEM=1;
    
        ArrayList<Data>datas;
        Context context;
    
        public MyAdapter(Context context,ArrayList<Data>datas){
            this.context=context;
            this.datas=datas;
        }
    
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if(viewType==TYPE_HEADER){
                View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.header,parent,false);
                return new HeaderViewHolder(view);
            }else if(viewType==TYPE_ITEM){
                View view =LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
                return new ItemListViewHolder(view);
            }
            return null;
        }
        private Data getItem(int position){
            return  datas.get(position);
        }
    
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            if(holder instanceof HeaderViewHolder){
                HeaderViewHolder headerViewHolder=(HeaderViewHolder)holder;
                headerViewHolder.txtHeader.setText("Header-List");
                headerViewHolder.txtHeader.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,"You Clicked header",Toast.LENGTH_SHORT).show();
                    }
                });
            }else if(holder instanceof ItemListViewHolder){
                Data currentItem =getItem(position-1);
                ItemListViewHolder itemListViewHolder=(ItemListViewHolder)holder;
                itemListViewHolder.txtName.setText(currentItem.getName());
                itemListViewHolder.txtName.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,"Clicked item"+(position-1),Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    
    
              @Override
              public  int getItemViewType(int postion){
                  if(positionHeader(postion)){
                      return TYPE_HEADER;
                  }
                  return TYPE_ITEM;
              }
             private boolean positionHeader(int position){
               return position ==0;
              }
    
     public class HeaderViewHolder extends RecyclerView.ViewHolder{
            TextView txtHeader;
            public HeaderViewHolder (View itemView){
            super(itemView);
            this.txtHeader=(TextView)itemView.findViewById(R.id.txt_header);
    
          }
       }
           public class ItemListViewHolder extends RecyclerView.ViewHolder{
             TextView txtName;
             public ItemListViewHolder(View itemView) {
                 super(itemView);
                this.txtName=(TextView)itemView.findViewById(R.id.txt_name);
            }
        }
    
        @Override
        public int getItemCount(){
            return datas.size();
        }
    }
    
    
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: