AbhiRam
AbhiRam

Reputation: 2061

How to add FooterView for RecyclerView

Hi i am new for Android material designing and in my app i am using RecyclerView and i need to add footerView for this RecyclerView,I wrote below code for adding footerView

But according to my code it's adding on Header why it's happening can some one help me how can i add this as a footerView

my code:-

public class PersonalInformationRecyclerLanguagesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOTER = 0;
    private static final int TYPE_ITEM = 1;
    RecyclerViewFooter recyclerViewFooter;
    private List<LanguagesBean> languagesBeanList;
    private Context context;

    public PersonalInformationRecyclerLanguagesAdapter(Context context, RecyclerViewFooter recyclerViewFooter, List<LanguagesBean> languagesBeanList) {

        this.context = context;
        this.recyclerViewFooter = recyclerViewFooter;
        this.languagesBeanList = languagesBeanList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_FOOTER) {

            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lagnguages_recyclerview_footer_layout, parent, false);
            return new VHFooter(v);

        } else if (viewType == TYPE_ITEM) {

            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_languages_list_items, parent, false);
            return new VHItem(v);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof VHFooter) {

            VHFooter VHfooter = (VHFooter) holder;
            VHfooter.addMoreButton.setText(recyclerViewFooter.getAddMore());
            VHfooter.nextButton.setText(recyclerViewFooter.getNext());

        } else if (holder instanceof VHItem) {

            LanguagesBean currentItem = getItem(position - 1);
            VHItem VHitem = (VHItem) holder;
            VHitem.languageName.setText(currentItem.getName());
        }
    }

    private LanguagesBean getItem(int position) {

        return languagesBeanList.get(position);
    }

    @Override
    public int getItemCount() {

        return languagesBeanList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {

        if (isPositionFooter(position))
            return TYPE_FOOTER;
        return TYPE_ITEM;
    }

    private boolean isPositionFooter(int position) {

        return position == 0;
    }

    class VHFooter extends RecyclerView.ViewHolder {

        private Button addMoreButton, nextButton;

        public VHFooter(View itemView) {

            super(itemView);
            addMoreButton = (Button) itemView.findViewById(R.id.addMore_button);
            nextButton = (Button) itemView.findViewById(R.id.next_button);
        }
    }

    class VHItem extends RecyclerView.ViewHolder {

        private TextView languageName;

        public VHItem(View view) {
            super(view);
            languageName = (TextView) view.findViewById(R.id.language_textview);
        }
    }
}

Activity:-

 private void intializingView(View view) {
            //Implement Recycler View
            recyclerView = (RecyclerView) view.findViewById(R.id.languages_recyclerView);
            recyclerView.setHasFixedSize(true);

            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(layoutManager);

            languagesBeanArrayList = new ArrayList<>();
            setRecyclerViewData();
            personalInformationRecyclerLanguagesAdapter = new PersonalInformationRecyclerLanguagesAdapter(getActivity(), getFooter(), languagesBeanArrayList);

            //RecyclerView.ItemDecoration itemDecoration = new RecyclerViewDividerItemDecoration(getActivity());
            //recyclerView.addItemDecoration(itemDecoration);

            recyclerView.setAdapter(personalInformationRecyclerLanguagesAdapter);

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    /**
     * setRecyclerViewData
     */
    private void setRecyclerViewData() {

        for (int i = 0; i < 5; i++) {
            languagesBeanArrayList.add(new LanguagesBean("English", 1));
        }
    }

Upvotes: 1

Views: 2507

Answers (4)

anhtuannd
anhtuannd

Reputation: 963

private boolean isPositionFooter(int position) {

    return position == getItemCount() - 1;
}

And also change this:

    @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

    if (holder instanceof VHFooter) {

        VHFooter VHfooter = (VHFooter) holder;
        VHfooter.addMoreButton.setText(recyclerViewFooter.getAddMore());
        VHfooter.nextButton.setText(recyclerViewFooter.getNext());

    } else if (holder instanceof VHItem) {

        LanguagesBean currentItem = getItem(position);
        VHItem VHitem = (VHItem) holder;
        VHitem.languageName.setText(currentItem.getName());
    }
}

Upvotes: 1

Harry Sharma
Harry Sharma

Reputation: 2200

Edit:

Method 1:

Just change

private boolean isPositionFooter(int position) {

    return position == 0;
}

to

private boolean isPositionFooter(int position) {

    return position == languagesBeanList.size() + 1;
}

Method 2:

You can use this GitHub library to add a Header or Footer to your RecyclerView in the simplest way possible.

https://github.com/lopspower/HFRecyclerView

You need to add the HFRecyclerView library in your project or you can also grab it from Gradle:

compile 'com.mikhaellopez:hfrecyclerview:1.0.0'

This library is based on a work at Is there an addHeaderView equivalent for RecyclerView? @hister

So i believe this is the finest way to do this.If this is still unclear follow the sample provided in this library.

Mark this up if this helps.

Upvotes: 4

Novinyo Amegadje
Novinyo Amegadje

Reputation: 699

I had the same issue. My code was working perfectly until I upgraded my android studio since then my footer can't work anymore so I simply added another LinearLayout where I style my footer and did the same thing for the header and whenever I perform an action on the Recyclerview I update my footer as well manually. Maybe you should try that option

I did something like this

 <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

            </LinearLayout>

Upvotes: 1

sumit singh
sumit singh

Reputation: 608

  private boolean isPositionFooter (int position) {
        return position == languagesBeanList.size ();
    }

Upvotes: 1

Related Questions