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

15
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;
    @Nullable
    @Override
    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(R.id.listView);
        // 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.
        mListView.setAdapter(adapter);
        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;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.hot_layout, container, false);
    postcardRecycler = view.findViewById(R.id.postcard_recycler);
    swipeRefreshLayout = view.findViewById(R.id.postcard_swipe_refresh);
    swipeRefreshLayout.setOnRefreshListener(this);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    postcardRecycler.setLayoutManager(layoutManager);
    adapter = new PostCardImageAdapter(getActivity(), post_data);
    postcardRecycler.setAdapter(adapter);
    init();
    return postcardRecycler;
}
private void init() {
    initPostData();
    initRefreshData();
}
private void initPostData() {
    post_data.add(new PostCard("http://clipart-library.com/images/8T65akk8c.png", "Batman", "1min", "Original RecyclerView.", "https://pmcvariety.files.wordpress.com/2013/07/dark_knight_rises.jpg", "100 likes", "100 messages"));
    post_data.add(new PostCard("https://cdn.iconscout.com/icon/free/png-256/superman-6-282336.png", "Superman", "10min", "Original RecyclerView.", "https://ichef.bbci.co.uk/news/660/cpsprodpb/FA79/production/_102512146_d8e385de-4e0c-45c0-b265-e105d0b47294.jpg", "66likes", "66messages"));
}
private void initRefreshData() {
    refresh_data.add(new PostCard("http://clipart-library.com/images/8T65akk8c.png", "Batman", "1min", "Refresh RecyclerView.", "https://pmcvariety.files.wordpress.com/2013/07/dark_knight_rises.jpg", "100 likes", "100 messages"));
    refresh_data.add(new PostCard("https://cdn.iconscout.com/icon/free/png-256/superman-6-282336.png", "Superman", "10min", "Refresh RecyclerView.", "https://ichef.bbci.co.uk/news/660/cpsprodpb/FA79/production/_102512146_d8e385de-4e0c-45c0-b265-e105d0b47294.jpg", "66likes", "66messages"));
}
@Override
public void onRefresh() {
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.e("refresh", "Enter Refresh function!");
            post_data.addAll(0, refresh_data);
            adapter.notifyDataSetChanged();
            swipeRefreshLayout.setRefreshing(false);
        }
    }, 1000);
}

}

Here is ma adpter:

package playground.com.pgapp;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
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;
    @Override
    public int getItemViewType(int position) {
        if(position == mData.size() + 1) {
            Log.e("LOG! adapter position: ", String.valueOf(position));
            return AD_Layout;
        }
        else
            return Post_Layout;
    }
    @Override
    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(R.id.postcard_userIcon);
            viewHolder.postcard_name = (TextView) view.findViewById(R.id.postcard_name);
            viewHolder.postcard_time = (TextView) view.findViewById(R.id.postcard_time);
            viewHolder.postcard_postText = (TextView) view.findViewById(R.id.postcard_postText);
            viewHolder.postcard_postPic = (ImageView) view.findViewById(R.id.postcard_postPic);
            viewHolder.postcard_likes = (TextView) view.findViewById(R.id.postcard_likes);
            viewHolder.postcard_comments = (TextView) view.findViewById(R.id.postcard_comments);
            return viewHolder;
        }
        else if(viewType == AD_Layout)
        {
            view = LayoutInflater.from(mContext).inflate(R.layout.footerview,parent,false);
            FooterViewHolder viewHolder = new FooterViewHolder(view);
            return viewHolder;
        }
        else{
            return null;
        }
    }
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if(holder.getItemViewType() == Post_Layout) {
            ItemViewHolder itemViewHolder = (ItemViewHolder)holder;
            PostCard post = mData.get(position);
            itemViewHolder.postcard_name.setText(post.name);
            itemViewHolder.postcard_time.setText(post.time);
            itemViewHolder.postcard_postText.setText(post.postText);
            itemViewHolder.postcard_likes.setText(post.likes);
            itemViewHolder.postcard_comments.setText(post.comments);
            Glide.with(mContext)
                    .load(post.userIconUrl)
                    .into(itemViewHolder.postcard_userIcon);
            Glide.with(mContext)
                    .load(post.postPicUrl)
                    .into(itemViewHolder.postcard_postPic);
        }
        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) {
            super(itemView);
        }
    }
    class FooterViewHolder extends RecyclerView.ViewHolder {
        FooterViewHolder(View itemView) { super(itemView); }
    }
    public PostCardImageAdapter(Context context, ArrayList<PostCard> data) {
        this.mContext = context;
        this.mData = data;
    }
    @Override
    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) {
        super(itemView);
    }
}

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

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

Create your viewholder inside onCreateViewHolder with method's viewType parameter

if (viewType == ITEM) 
  return new ItemViewHolder();
else
  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.

READ ALSO
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

20
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

52
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

52
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

23