Skip to content Skip to sidebar Skip to footer

Adding A Footer View In Recycler View?

i want to add a footer layout at the base of the recycler view.Below is my adapter view which inflates header layout as well as other items as well.Help appreciated! public class S

Solution 1:

Similar to to adding the header add the footer too.

publicclassSplitMembersAdapterextendsRecyclerViewAdapter<SplitMember, SplitMemberViewHolder> {

        privatefinal ArrayList<SplitMember> mSplitMembersList;
        privatefinal ImageLoader mImageLoader;
        privatestaticfinalintTYPE_HEADER=0;
        privatestaticfinalintTYPE_ITEM=1;
        privatestaticfinalintTYPE_FOOTER=2;
        privateint splitAmount;
        privateboolean isAmountSplitted;

        publicSplitMembersAdapter(ArrayList<SplitMember> members,ImageLoader imageLoader) {
            mSplitMembersList = members;
            mImageLoader = imageLoader;
        }

        @OverridepublicintgetItemCount() {
            return mSplitMembersList.size();
        }

        @OverridepublicintgetItemViewType(int position) {
            if (isPositionHeader(position)) {
                return TYPE_HEADER;
            }ELSE if(isPositionFooter(position)) {
                return TYPE_Footer;
            }
            return TYPE_ITEM;
        }

        privatestaticbooleanisPositionHeader(int position) {
            returnposition== 0;
        }

        privatestaticbooleanisPositionFooter(int position) {
            returnposition== mSplitMembersList.size()-1;
        }

        @Overridepublic SplitMemberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

            switch(viewType){
               case TYPE_HEADER:
                 //return header view holder;break;
               case TYPE_ITEM:
                 //return item view holder;break;
               case TYPE_FOOTER:   
                 //return footer view holder;break;
           }
        }

        @OverridepublicvoidonBindViewHolder(SplitMemberViewHolder viewHolder, int position) {
               switch(getItemViewType(position)){
               case TYPE_HEADER:
                 //bind header data;break;
               case TYPE_ITEM:
                 //bind item databreak;
               case TYPE_FOOTER:   
                 //bind footer data.break;
           }
        }
    }

    classSplitMemberViewHolderextendsRecyclerView.ViewHolder {

        public BezelImageView splitMemImg;
        public TextView splitMemName;
        public TextView splitMemAmount;
        public View splitMemberColor;

        publicSplitMemberViewHolder(View itemView) {
            super(itemView);
            splitMemberColor = itemView.findViewById(R.id.member_color);
            splitMemImg = (BezelImageView) itemView.findViewById(R.id.member_profile_image);
            splitMemName = (TextView) itemView.findViewById(R.id.member_name);
            splitMemAmount = (TextView) itemView.findViewById(R.id.member_amount);
        }
    }

Solution 2:

I created a wrapper around my adapter using this gist.And after that i can include my own layout and viewholder for my header and footer as well as the rest of the items.

publicclassSplitMembersAdapterextendsHeaderFooterRecyclerViewAdapterimplementsAutoCompleteContactTextView.ContactSelectListener{

        privatefinal ArrayList<SplitMember> mSplitMembersList;
        privatefinal ImageLoader mImageLoader;
        privatestaticfinalintTYPE_HEADER=0;
        privatestaticfinalintTYPE_ITEM=1;
        privatestaticfinalintTYPE_FOOTER=2;
        privateint splitAmount;
        privateboolean isAmountSplitted;

        publicSplitMembersAdapter(ArrayList<SplitMember> members, ImageLoader imageLoader) {
            mSplitMembersList = members;
            mImageLoader = imageLoader;
        }

        @OverridepublicbooleanuseFooter() {
            returntrue;
        }

        @Overridepublic RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup viewGroup, int viewType) {
            ViewitemView= LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.split_member_footer_layout, viewGroup, false);
            returnnewSplitMemberFooterViewHolder(itemView);
        }


//Binding data wrt to footer@OverridepublicvoidonBindFooterView(final RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof SplitMemberFooterViewHolder){
                ((SplitMemberFooterViewHolder) holder).mAddFriends.setOnClickListener(newView.OnClickListener() {
                    @OverridepublicvoidonClick(View v) {
                        ((SplitMemberFooterViewHolder) holder).mAddFriendPlaceholderTxt.setVisibility(View.GONE);
                        ((SplitMemberFooterViewHolder) holder).mAddFriends.setVisibility(View.GONE);
                        ((SplitMemberFooterViewHolder) holder).mPhoneEnterView.setVisibility(View.VISIBLE);
                    }
                });

                ((SplitMemberFooterViewHolder) holder).mPhoneEnterView.setmContactSelectListener(this);
                ((SplitMemberFooterViewHolder) holder).mPhoneEnterView.setDisplayPhoto(true);
                ((SplitMemberFooterViewHolder) holder).mPhoneEnterView.setOnClickListener(newView.OnClickListener() {
                    @OverridepublicvoidonClick(View view) {
                        ((SplitMemberFooterViewHolder) holder).mPhoneEnterView.setCursorVisible(true);
                    }
                });
                ((SplitMemberFooterViewHolder) holder).mPhoneEnterView.setOnKeyListener(newView.OnKeyListener() {
                    @OverridepublicbooleanonKey(View view, int keyCode, KeyEvent event) {
                        if (event.getAction() == KeyEvent.ACTION_UP)
                        {
                            switch (keyCode)
                            {
                                case KeyEvent.KEYCODE_DPAD_CENTER:
                                case KeyEvent.KEYCODE_ENTER:

                                    postContact(((SplitMemberFooterViewHolder) holder)
                                            .mPhoneEnterView.getText().toString());

                                    returntrue;
                                default:
                                    break;
                            }
                        }
                        returnfalse;
                    }
                });


            }
        }

        @OverridepublicvoidonContactSelect(People contact) {
            StringmobileNum= String.valueOf(contact.getData());
            postContact(mobileNum);
        }

        privatestaticvoidpostContact(String contact) {
            EventBus.getDefault().post(newEvents(Events.GET_CONTACT,
                    true, contact));
        }

        @Overridepublic RecyclerView.ViewHolder onCreateBasicItemViewHolder(ViewGroup viewGroup, int viewType) {
            ViewitemView= LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.split_member_item_layout, viewGroup, false);
            returnnewSplitMemberViewHolder(itemView);
        }
    //Binding data wrt the items@OverridepublicvoidonBindBasicItemView(RecyclerView.ViewHolder holder, int position) {
            SplitMembercontact= mSplitMembersList.get(position);

            if (holder instanceof SplitMemberViewHolder){
                ((SplitMemberViewHolder) holder).splitMemAmount.setText(String.format("%.2f", contact.getShare()));
                ((SplitMemberViewHolder) holder).splitMemberColor.setBackgroundColor(contact.getColor());
                if (position == TYPE_HEADER) {
                    ((SplitMemberViewHolder) holder).splitMemName.setText("You");
                    mImageLoader.loadImage(contact.getImg(),
                            ((SplitMemberViewHolder) holder).splitMemImg, R.drawable.empty_user_placeholder);
                }  else {
                    ((SplitMemberViewHolder) holder).splitMemName.setText(contact.getName());
                    mImageLoader.loadImage(Utils.getProfileImageUrl(contact.getImg()),
                            ((SplitMemberViewHolder) holder).splitMemImg, R.drawable.empty_user_placeholder);
                }
            }
        }


        @OverridepublicintgetBasicItemCount() {
            return  mSplitMembersList.size();
        }

        @OverridepublicintgetBasicItemType(int position) {
            return position;
        }


    }

//Seperate viewholders for different footer as well as itemsclassSplitMemberViewHolderextendsRecyclerView.ViewHolder {

        public BezelImageView splitMemImg;
        public TextView splitMemName;
        public TextView splitMemAmount;
        public View splitMemberColor;

        publicSplitMemberViewHolder(View itemView) {
            super(itemView);
            splitMemberColor = itemView.findViewById(R.id.member_color);
            splitMemImg = (BezelImageView) itemView.findViewById(R.id.member_profile_image);
            splitMemName = (TextView) itemView.findViewById(R.id.member_name);
            splitMemAmount = (TextView) itemView.findViewById(R.id.member_amount);
        }
    }classSplitMemberFooterViewHolderextendsRecyclerView.ViewHolder {

        public BezelImageView mAddFriends;
        public AutoCompleteContactTextView mPhoneEnterView;
        public TextView mAddFriendPlaceholderTxt;

        publicSplitMemberFooterViewHolder(View itemView) {
            super(itemView);
            mAddFriends = (BezelImageView) itemView.findViewById(R.id.add_friends_img);
            mPhoneEnterView = (AutoCompleteContactTextView) itemView.findViewById(R.id.add_member_edit_text);
            mAddFriendPlaceholderTxt = (TextView) itemView.findViewById(R.id.add_friend_static_txt);
        }
    }

Post a Comment for "Adding A Footer View In Recycler View?"