如何在recyclerview底部添加加载进度?

时间:2017-07-28 03:35:49

标签: android

我有一个应用程序,其中包含recyclerview,在recyclelerview和丢失进度之间的广告介于Recyclerview底部,当我在RecyclerView底部滚动它在数据集中添加更多数据时,所有工作正常但问题是添加加载进度底部。

主要活动代码:

 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();
            }
        }
    });

适配器代码:

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;
}

1 个答案:

答案 0 :(得分:0)

一种简单的方法是使用此库https://github.com/mikepenz/FastAdapter

并将您的进度视图用作footerAdapter。

但是根据您的方法,您应该在notifyAdapterChange之前添加类型3联系人。

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

每次数据出现时,如果是类型3,则始终检查最后一项。替换

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

if(contacts.size() > 0 && contacts.get(contacts.size()-1).getViewType() == 3){
   contacts.remove(contacts.size()-1);
}