0

I have an app which contain recyclerview with ads in between recyclerview and Losing progress as footer at bottom of recyclerview when I scroll at bottom of RecyclerView it is adding more data in dataset all is working fine but problem is with adding loading progress at bottom.

Code of main activity:

 contacts = new ArrayList<>();
    random = new Random();
    for (int i = 0; i < 5; i++) {
        Contact contact = new Contact();
        contact.setPhone(phoneNumberGenerating());
        contact.setEmail("Contact" + i + "@gmail.com");
        contact.setViewType(1);
        contacts.add(contact);
    }
    //Place two Admob Ads in recyclerview
    Contact myString1 = new Contact();
    myString1.setViewType(2);
    contacts.add(3,myString1);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    contactAdapter = new ContactAdapter(recyclerView, contacts, this);
    recyclerView.setAdapter(contactAdapter);

    //set load more listener for the RecyclerView adapter
    contactAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            if (contacts.size() <= 7) {
                contacts.add(null);
                contactAdapter.notifyItemInserted(contacts.size() - 1);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        contacts.remove(contacts.size() - 1);
                        contactAdapter.notifyItemRemoved(contacts.size());

                        //Generating more data
                        int index = contacts.size();
                        int end = index + 2;
                        for (int i = index; i < end; i++) {
                            Contact contact = new Contact();
                            contact.setPhone(phoneNumberGenerating());
                            contact.setEmail("DevExchanges" + i + "@gmail.com");
                            contact.setViewType(1);
                            contacts.add(contact);
                        }
                        //Place one Admob Ads in recyclerview
                        Contact myString1 = new Contact();
                        myString1.setViewType(2);
                        contacts.add(3,myString1);

                        contactAdapter.notifyDataSetChanged();
                        contactAdapter.setLoaded();
                    }
                }, 5000);
            } else {
                Toast.makeText(MainActivity.this, "Loading data completed", Toast.LENGTH_SHORT).show();
            }
        }
    });

code of adapter:

public int getItemViewType(int position) {
    if (position == contacts.size()) {
        // footer_view to check in your switch/case
        return 3;
    }
    return contacts.get(position).getViewType();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder = null;
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    switch (viewType) {
        case 1:
            View itemView = inflater.inflate(R.layout.item_recycler_view_row, parent, false);
            viewHolder = new UserViewHolder(itemView);
            break;
        case 2:
            View adView = inflater.inflate(R.layout.item_three, parent, false);
            viewHolder = new ItemThree(adView);
            break;
        case 3:
            View loading = inflater.inflate(R.layout.item_loading, parent, false);
            viewHolder = new LoadingViewHolder(loading);
            break;
    }
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case 1:
            Contact contact = contacts.get(position);
            UserViewHolder userViewHolder = (UserViewHolder) holder;
            userViewHolder.phone.setText(contact.getEmail());
            userViewHolder.email.setText(contact.getPhone());
            break;
        case 2:
            ItemThree itemThree = (ItemThree) holder;
            itemThree.textView.setText("ADs");
            break;
        case 3:
            LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
            loadingViewHolder.progressBar.setIndeterminate(true);
            break;
    }
}

@Override
public int getItemCount() {
    return contacts.size()+1;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Raghav
  • 65
  • 1
  • 12
  • Add the progress bar at last item of recycler view on the base of desired conditions. Every time when you add more data in RecyclerView make sure to add those items to above of the last item. – VikasGoyal Jul 28 '17 at 03:57
  • Pls edit my code........... – Raghav Jul 28 '17 at 03:58
  • You can treat progress as footer, please refer here for example:- https://stackoverflow.com/a/29168617/1562659 – VikasGoyal Jul 28 '17 at 04:00
  • Please see my above code as i am adding On more view in dataset of Recylerview ....pls provide me answer to that approach only – Raghav Jul 28 '17 at 04:09

1 Answers1

0

One easy way is to use this library https://github.com/mikepenz/FastAdapter

and use your progress view as footerAdapter.

But on your approach, you should add a type 3 Contact immediately before the notifyAdapterChange.

if(shouldLoadMore){
    Contact laoder = new Contact();
    contact.setViewType(3);
    contacts.add(loader);
}

And everytime a data comes always check for the last item if it is type 3. replace

contacts.remove(contacts.size()-1);

with

if(contacts.size() > 0 && contacts.get(contacts.size()-1).getViewType() == 3){
   contacts.remove(contacts.size()-1);
}
Sheychan
  • 2,415
  • 14
  • 32