How to add a FrameLayout in the footer of RecyclerView in the Fragment

April 15, 2019, at 4:40 PM

I use the ListView and addFooterView to insert a FrameLayout in the Fragment, it's works!

But now I want to use RecyclerView and to add FrameLayout in the footer of the RecyclerView in the Fragment.

What can I do? Thank you!

Here is my ListView code:

public class NewFragment extends Fragment {
    private ListView mListView;
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.new_layout,container,false);
        mListView = (ListView) view.findViewById(;
        // AD FrameLayout.
        FrameLayout footerLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.footerview, null);
        // ListView.
        ArrayList<Card> list = new ArrayList<>();
        list.add(new Card("drawable://" + R.drawable.icon1, "Batman","1 min","drawable://" + R.drawable.img_pic1, "list1 post1.", "10000 likes", "10000 message"));
        list.add(new Card("drawable://" + R.drawable.icon2, "Superman","10 min","drawable://" + R.drawable.img_pic2, "list1 post2.", "1234 likes", "123 message"));
        list.add(new Card("drawable://" + R.drawable.icon3, "Ironman","30 min","drawable://" + R.drawable.img_pic3, "list1 post3.", "10 likes", "10 message"));
        CustomListAdapter adapter = new CustomListAdapter(getActivity(), R.layout.card_layout_main, list);
        mListView.addFooterView(footerLayout); // add FrameLayout in the footer of listview.
        return view;

Here is my RecyclerView code(notadd FrameLayout yet):

public class HotFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
private RecyclerView postcardRecycler;
private ArrayList<PostCard> post_data = new ArrayList<>();
private ArrayList<PostCard> refresh_data = new ArrayList<>();
private PostCardImageAdapter adapter;
private Handler handler = new Handler();
private SwipeRefreshLayout swipeRefreshLayout;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.hot_layout, container, false);
    postcardRecycler = view.findViewById(;
    swipeRefreshLayout = view.findViewById(;
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    adapter = new PostCardImageAdapter(getActivity(), post_data);
    return postcardRecycler;
private void init() {
private void initPostData() {
    post_data.add(new PostCard("", "Batman", "1min", "Original RecyclerView.", "", "100 likes", "100 messages"));
    post_data.add(new PostCard("", "Superman", "10min", "Original RecyclerView.", "", "66likes", "66messages"));
private void initRefreshData() {
    refresh_data.add(new PostCard("", "Batman", "1min", "Refresh RecyclerView.", "", "100 likes", "100 messages"));
    refresh_data.add(new PostCard("", "Superman", "10min", "Refresh RecyclerView.", "", "66likes", "66messages"));
public void onRefresh() {
    handler.postDelayed(new Runnable() {
        public void run() {
            Log.e("refresh", "Enter Refresh function!");
            post_data.addAll(0, refresh_data);
    }, 1000);


Here is ma adpter:

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class PostCardImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int Post_Layout = 0;
    public static final int AD_Layout = 0;
    private Context mContext;
    private ArrayList<PostCard> mData;
    public int getItemViewType(int position) {
        if(position == mData.size() + 1) {
            Log.e("LOG! adapter position: ", String.valueOf(position));
            return AD_Layout;
            return Post_Layout;
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if(viewType == Post_Layout)
            view = LayoutInflater.from(mContext).inflate(R.layout.postcard_layout,parent,false);
            ItemViewHolder viewHolder = new ItemViewHolder(view);
            viewHolder.postcard_userIcon = (ImageView) view.findViewById(;
            viewHolder.postcard_name = (TextView) view.findViewById(;
            viewHolder.postcard_time = (TextView) view.findViewById(;
            viewHolder.postcard_postText = (TextView) view.findViewById(;
            viewHolder.postcard_postPic = (ImageView) view.findViewById(;
            viewHolder.postcard_likes = (TextView) view.findViewById(;
            viewHolder.postcard_comments = (TextView) view.findViewById(;
            return viewHolder;
        else if(viewType == AD_Layout)
            view = LayoutInflater.from(mContext).inflate(R.layout.footerview,parent,false);
            FooterViewHolder viewHolder = new FooterViewHolder(view);
            return viewHolder;
            return null;
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if(holder.getItemViewType() == Post_Layout) {
            ItemViewHolder itemViewHolder = (ItemViewHolder)holder;
            PostCard post = mData.get(position);
        else if(holder.getItemViewType() == AD_Layout){
            FooterViewHolder footerViewHolder = (FooterViewHolder)holder;
    class ItemViewHolder extends RecyclerView.ViewHolder {
        public ImageView postcard_userIcon;
        public TextView postcard_name;
        public TextView postcard_time;
        public TextView postcard_postText;
        public ImageView postcard_postPic;
        public TextView postcard_likes;
        public TextView postcard_comments;
        public ItemViewHolder(View itemView) {
    class FooterViewHolder extends RecyclerView.ViewHolder {
        FooterViewHolder(View itemView) { super(itemView); }
    public PostCardImageAdapter(Context context, ArrayList<PostCard> data) {
        this.mContext = context;
        this.mData = data;
    public int getItemCount() {
        return mData.size();
Answer 1

You need to create two view holder class . One of this you already created which inside PostCardImageAdapter.

And you create FooterViewHolder inside PostCardImageAdapter

class FooterViewHolder extends RecyclerView.ViewHolder {
FooterViewHolder(View itemView) {

Override getItemViewType inside PostCardImageAdapter and prepare last item will be FooterViewHolder.

public int getItemViewType(int position) {
  if(position < data.size())
     return ITEM; 
     return FOOTER;

Create your viewholder inside onCreateViewHolder with method's viewType parameter

if (viewType == ITEM) 
  return new ItemViewHolder();
  return new FooterViewHolder();

In brief RecyclerView has not exist any footer or header view you can separate each different view with viewholder and then use with position or data status.

How to navigate from contentView to fragment

How to navigate from contentView to fragment

I would like to know how to move from a ContentView to fragment

Build failed showing error &ldquo;Attribute &rdquo;android:focusable&ldquo; was already specified for element &rdquo;EditText"

Build failed showing error “Attribute ”android:focusable“ was already specified for element ”EditText"

my code is not being build when trying to run it on device or emulator with the error Error:Attribute "android:focusable" was already specified for element "EditText"but it gradle build is successful

Errors when changing Android version (Target Framework) in Xamarin Forms

Errors when changing Android version (Target Framework) in Xamarin Forms

I have a C# Xamarin Forms Solution in Visual Studio 2019 that was created from the Xamarin Forms Template

Keep getting Parse Error launching fileprovider intent

Keep getting Parse Error launching fileprovider intent

I'm trying to launch an update for my app using intent and file provider