Adding a footer view in RECYCLER VIEW?
Asked Answered
T

2

5

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 SplitMembersAdapter extends RecyclerViewAdapter<SplitMember, SplitMemberViewHolder> {

        private final ArrayList<SplitMember> mSplitMembersList;
        private final ImageLoader mImageLoader;
        private static final int TYPE_HEADER = 0;
        private static final int TYPE_ITEM = 1;
        private int splitAmount;
        private boolean isAmountSplitted;

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

        @Override
        public int getItemCount() {
            return mSplitMembersList.size();
        }

        @Override
        public int getItemViewType(int position) {
            if (isPositionHeader(position)) {
                return TYPE_HEADER;
            }

            return TYPE_ITEM;
        }

        private static boolean isPositionHeader(int position) {
            return position == 0;
        }

        @Override
        public SplitMemberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View itemView = LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.split_member_item_layout, viewGroup, false);
            return new SplitMemberViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(SplitMemberViewHolder viewHolder, int position) {
            SplitMember contact = mSplitMembersList.get(position);
            viewHolder.splitMemAmount.setText(String.format("%.2f", contact.getShare()));
            viewHolder.splitMemberColor.setBackgroundColor(contact.getColor());
            if (position == TYPE_HEADER){
                viewHolder.splitMemName.setText("You");
                mImageLoader.loadImage(contact.getImg(),
                        viewHolder.splitMemImg, R.drawable.empty_user_placeholder);
            }


            else {
                viewHolder.splitMemName.setText(contact.getName());
                mImageLoader.loadImage(Utils.getProfileImageUrl(contact.getImg()),
                        viewHolder.splitMemImg,R.drawable.empty_user_placeholder);
            }

        }
    }

    class SplitMemberViewHolder extends RecyclerView.ViewHolder {

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

        public SplitMemberViewHolder(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);
        }
    }
Trellis answered 1/6, 2015 at 9:36 Comment(0)
T
4

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.

    public class SplitMembersAdapter extends HeaderFooterRecyclerViewAdapter  implements  AutoCompleteContactTextView.ContactSelectListener{

        private final ArrayList<SplitMember> mSplitMembersList;
        private final ImageLoader mImageLoader;
        private static final int TYPE_HEADER = 0;
        private static final int TYPE_ITEM = 1;
        private static final int TYPE_FOOTER = 2;
        private int splitAmount;
        private boolean isAmountSplitted;

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

        @Override
        public boolean useFooter() {
            return true;
        }

        @Override
        public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup viewGroup, int viewType) {
            View itemView = LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.split_member_footer_layout, viewGroup, false);
            return new SplitMemberFooterViewHolder(itemView);
        }


//Binding data wrt to footer
        @Override
        public void onBindFooterView(final RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof SplitMemberFooterViewHolder){
                ((SplitMemberFooterViewHolder) holder).mAddFriends.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(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(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ((SplitMemberFooterViewHolder) holder).mPhoneEnterView.setCursorVisible(true);
                    }
                });
                ((SplitMemberFooterViewHolder) holder).mPhoneEnterView.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(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());

                                    return true;
                                default:
                                    break;
                            }
                        }
                        return false;
                    }
                });


            }
        }

        @Override
        public void onContactSelect(People contact) {
            String mobileNum = String.valueOf(contact.getData());
            postContact(mobileNum);
        }

        private static void postContact(String contact) {
            EventBus.getDefault().post(new Events(Events.GET_CONTACT,
                    true, contact));
        }

        @Override
        public RecyclerView.ViewHolder onCreateBasicItemViewHolder(ViewGroup viewGroup, int viewType) {
            View itemView = LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.split_member_item_layout, viewGroup, false);
            return new SplitMemberViewHolder(itemView);
        }
    //Binding data wrt the items
        @Override
        public void onBindBasicItemView(RecyclerView.ViewHolder holder, int position) {
            SplitMember contact = 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);
                }
            }
        }


        @Override
        public int getBasicItemCount() {
            return  mSplitMembersList.size();
        }

        @Override
        public int getBasicItemType(int position) {
            return position;
        }


    }

//Seperate viewholders for different footer as well as items
    class SplitMemberViewHolder extends RecyclerView.ViewHolder {

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

        public SplitMemberViewHolder(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);
        }
    }class SplitMemberFooterViewHolder extends RecyclerView.ViewHolder {

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

        public SplitMemberFooterViewHolder(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);
        }
    }
Trellis answered 2/6, 2015 at 7:34 Comment(0)
R
5

Similar to to adding the header add the footer too.

public class SplitMembersAdapter extends RecyclerViewAdapter<SplitMember, SplitMemberViewHolder> {

        private final ArrayList<SplitMember> mSplitMembersList;
        private final ImageLoader mImageLoader;
        private static final int TYPE_HEADER = 0;
        private static final int TYPE_ITEM = 1;
        private static final int TYPE_FOOTER = 2;
        private int splitAmount;
        private boolean isAmountSplitted;

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

        @Override
        public int getItemCount() {
            return mSplitMembersList.size();
        }

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

        private static boolean isPositionHeader(int position) {
            return position == 0;
        }

        private static boolean isPositionFooter(int position) {
            return position == mSplitMembersList.size()-1;
        }

        @Override
        public 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;
           }
        }

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

    class SplitMemberViewHolder extends RecyclerView.ViewHolder {

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

        public SplitMemberViewHolder(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);
        }
    }
Rustie answered 1/6, 2015 at 10:6 Comment(2)
How to do you call this from your code outside the adapter class? Like in case of ListView we call lv.removeFooterView(loadMoreView); and lv.addFooterView(loadMoreView);Quixotic
Simply set this adapter to the RecyclerView, then this adapter will take care of adding the header and footer. It can differnetiate the header and footer by using getItemViewType(int position). @QuixoticRustie
T
4

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.

    public class SplitMembersAdapter extends HeaderFooterRecyclerViewAdapter  implements  AutoCompleteContactTextView.ContactSelectListener{

        private final ArrayList<SplitMember> mSplitMembersList;
        private final ImageLoader mImageLoader;
        private static final int TYPE_HEADER = 0;
        private static final int TYPE_ITEM = 1;
        private static final int TYPE_FOOTER = 2;
        private int splitAmount;
        private boolean isAmountSplitted;

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

        @Override
        public boolean useFooter() {
            return true;
        }

        @Override
        public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup viewGroup, int viewType) {
            View itemView = LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.split_member_footer_layout, viewGroup, false);
            return new SplitMemberFooterViewHolder(itemView);
        }


//Binding data wrt to footer
        @Override
        public void onBindFooterView(final RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof SplitMemberFooterViewHolder){
                ((SplitMemberFooterViewHolder) holder).mAddFriends.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(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(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ((SplitMemberFooterViewHolder) holder).mPhoneEnterView.setCursorVisible(true);
                    }
                });
                ((SplitMemberFooterViewHolder) holder).mPhoneEnterView.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(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());

                                    return true;
                                default:
                                    break;
                            }
                        }
                        return false;
                    }
                });


            }
        }

        @Override
        public void onContactSelect(People contact) {
            String mobileNum = String.valueOf(contact.getData());
            postContact(mobileNum);
        }

        private static void postContact(String contact) {
            EventBus.getDefault().post(new Events(Events.GET_CONTACT,
                    true, contact));
        }

        @Override
        public RecyclerView.ViewHolder onCreateBasicItemViewHolder(ViewGroup viewGroup, int viewType) {
            View itemView = LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.split_member_item_layout, viewGroup, false);
            return new SplitMemberViewHolder(itemView);
        }
    //Binding data wrt the items
        @Override
        public void onBindBasicItemView(RecyclerView.ViewHolder holder, int position) {
            SplitMember contact = 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);
                }
            }
        }


        @Override
        public int getBasicItemCount() {
            return  mSplitMembersList.size();
        }

        @Override
        public int getBasicItemType(int position) {
            return position;
        }


    }

//Seperate viewholders for different footer as well as items
    class SplitMemberViewHolder extends RecyclerView.ViewHolder {

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

        public SplitMemberViewHolder(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);
        }
    }class SplitMemberFooterViewHolder extends RecyclerView.ViewHolder {

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

        public SplitMemberFooterViewHolder(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);
        }
    }
Trellis answered 2/6, 2015 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.