无法在RecyclerView中添加footerView

时间:2017-01-09 08:42:37

标签: android listview android-recyclerview

我正在尝试在RecyclerView中添加我的自定义footerView。我写了适配器源代码,但我无法显示我的footerView.This是我的来源

     private List<Transaction> transactionList;
    public Context mContext;
    private boolean loading;
    private static final int TYPE_ITEM = 1;
    private static final int TYPE_FOOTER = 2;


    public void setmOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
        this.mOnLoadMoreListener = mOnLoadMoreListener;
    }

    private OnLoadMoreListener mOnLoadMoreListener;
    private int visibleThreshold = 5;
    private int lastVisibleItem, totalItemCount;


    public void setLoaded() {
        loading = false;
    }


    class LoadingViewHolder extends RecyclerView.ViewHolder {
        public MaterialProgressBar progressBar;

        public LoadingViewHolder(View itemView) {
            super(itemView);
            progressBar = (MaterialProgressBar) itemView.findViewById(R.id.progressBar);
        }
    }

    public class TransactionHolder extends RecyclerView.ViewHolder {
        private TextView transactionName, transactionStatus;

        public TransactionHolder(View view) {
            super(view);
            transactionName = (TextView) view.findViewById(R.id.u_transaction_name);
            transactionStatus = (TextView) view.findViewById(R.id.u_transaction_status);

        }
    }


    public TransactionRecyclerViewAdapter(List<Transaction> transactions, Context context, RecyclerView recyclerView) {
        super();
        this.transactionList = transactions;
        this.mContext = context;
        if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
            final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            recyclerView
                    .addOnScrollListener(new RecyclerView.OnScrollListener() {
                        @Override
                        public void onScrolled(RecyclerView recyclerView,
                                               int dx, int dy) {
                            super.onScrolled(recyclerView, dx, dy);

                            totalItemCount = linearLayoutManager.getItemCount();
                            lastVisibleItem = linearLayoutManager
                                    .findLastVisibleItemPosition();
                            if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {

                                if (mOnLoadMoreListener != null) {
                                    mOnLoadMoreListener.onLoadMore();
                                }
                                loading = true;
                            }
                        }
                    });
        }


    }
    @Override
    public int getItemViewType(int position) {
        if (isPositionFooter(position)) {
            return TYPE_FOOTER;
        }
        return TYPE_ITEM;
    }

    private boolean isPositionFooter(int position) {
        return position == transactionList.size();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        if (viewType == TYPE_FOOTER) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_loading, parent, false);
            return new TransactionRecyclerViewAdapter.LoadingViewHolder(v);
        } else if (viewType == TYPE_ITEM) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_transaction_recycler_item, parent, false);
            return new TransactionRecyclerViewAdapter.TransactionHolder(v);
        }
        return null;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof TransactionHolder) {
            TransactionHolder transactionHolder = (TransactionHolder) holder;
            Transaction transaction = transactionList.get(position);
            if (transaction != null) {
                transaction.setAmount(transaction.getAmount().replace("-", ""));
                transactionHolder.transactionName.setText(transaction.getName());
                transactionHolder.transactionStatus.setText(transaction.getType());


            }
        } else {
            LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
            loadingViewHolder.progressBar.setVisibility(View.VISIBLE);

        }


    }

    @Override
    public int getItemCount() {
        return transactionList == null ? 0 : transactionList.size();

    }
    public Transaction getItem(int position) {
        return transactionList.get(position);
    }


}

我使用我的适配器

 final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(layoutManager);
    mTransactionAdapter = new TransactionRecyclerViewAdapter(mTransactionList, getActivity(), mRecyclerView);
    mRecyclerView.setAdapter(mTransactionAdapter);
    mTransactionAdapter.notifyDataSetChanged();

我怎么能解决我的问题?我不知道我的代码中有什么问题

2 个答案:

答案 0 :(得分:0)

  @Override
    public int getItemCount() {
        return transactionList == null ? 0 : transactionList.size();

    }

您需要拥有transactionList.size()+ 1项。 (因为你也有一个页脚)。

答案 1 :(得分:0)

您需要将计数增加1

@Override
public int getItemCount() {
    return transactionList == null ? 0 : transactionList.size()+1;

}