1

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));
        }
    }
AbhiRam
  • 2,033
  • 7
  • 41
  • 94

4 Answers4

4

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.

Community
  • 1
  • 1
Harry Sharma
  • 2,190
  • 2
  • 15
  • 41
  • this doesn't answer the question and it is better to stay away from external libraries when native development covers it quite nicely. – mfaisalhyder Aug 09 '16 at 12:17
  • @MFaisalHyder They have turned a meaningful answer on stackoverflow to a library to avoid broilercode.Go through the main class of that library they are doing it in purely a native way without any workaround. – Harry Sharma Aug 09 '16 at 12:19
  • but it depends on requirement too, library is good - no doubt, unless it is clustered with non required documentation and useless code which increases APK size for very less functionality. :) – mfaisalhyder Aug 09 '16 at 12:24
  • I agree but the code there gives better understanding. That is upto the developer which snippet of code he has to use according to his requirement :) – Harry Sharma Aug 09 '16 at 12:26
  • so let's help this guy, it is not good to ask him to replace all logic at this stage when he has cooked all food and only thing upsetting him is that he gets white rice yet he cooked Biryani :D just need to tweak position of view where it shows the footer.! else he will need to change little bit of snippets in main activity and adapter. :) – mfaisalhyder Aug 09 '16 at 12:30
  • @MFaisalHyder i have edited it. :) Upvote it if you find it right.. :D – Harry Sharma Aug 09 '16 at 12:41
  • Its not position +1 its languagesBeanList.size() +1 :D. Which will definately point to the last index watch closely – Harry Sharma Aug 09 '16 at 13:04
  • i forgot to upvote :D but how it feels old posts getting upvote :D – mfaisalhyder Nov 22 '16 at 06:32
1
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());
    }
}
anhtuannd
  • 918
  • 9
  • 16
1
  private boolean isPositionFooter (int position) {
        return position == languagesBeanList.size ();
    }
sumit singh
  • 588
  • 1
  • 5
  • 20
1

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>
Novinyo Amegadje
  • 699
  • 4
  • 11