记录

    private class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<Post> list;
        private LayoutInflater mInflater;
        private int load_more_status = 0;
        //上拉加载更多
        public static final int PULLUP_LOAD_MORE = 0;
        //正在加载中
        public static final int LOADING_MORE = 1;

        public RecyclerAdapter(List<Post> list) {
            this.list = list;
            mInflater = LayoutInflater.from(getActivity());
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == TYPE_ITEM) {
                View view = mInflater.inflate(R.layout.post_list_item, parent, false);
                PostItemHolder postItemHolder = new PostItemHolder(view);
                return postItemHolder;
            } else if (viewType == TYPE_FOOTER) {
                View foot_view = mInflater.inflate(R.layout.footer, parent, false);
                FooterViewHolder footerViewHolder = new FooterViewHolder(foot_view);
                return footerViewHolder;
            }
            return null;


        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof PostItemHolder) {
                Post post = list.get(position);
                if (post.getPost_content().length() > 20) {
                    ((PostItemHolder) holder).content.setText(post.getPost_content().substring(0, 20));
                } else {
                    ((PostItemHolder) holder).content.setText(post.getPost_content());
                }


                if (post.getThird_flag() == 1) {
                    Picasso.with(getActivity()).load(BaseUrlPath.BASE_URL + post.getHeader_img())
                            .into(((PostItemHolder) holder).circleImageView);
                } else {
                    Picasso.with(getActivity()).load(post.getHeader_img())
                            .into(((PostItemHolder) holder).circleImageView);
                }
                if (post.getPost_grab_flag() == 1) {
                    Picasso.with(getActivity()).load(post.getPost_img())
                            .into(((PostItemHolder) holder).imageView);
                } else {
                    Picasso.with(getActivity()).load(BaseUrlPath.BASE_URL + post.getPost_img())
                            .into(((PostItemHolder) holder).imageView);
                }
                ((PostItemHolder) holder).username.setText(post.getUser_name());
            } else {
                FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
                switch (load_more_status) {
                    case PULLUP_LOAD_MORE:
                        footerViewHolder.footer_textview.setText("上拉加载更多");
                        break;
                    case LOADING_MORE:
                        footerViewHolder.footer_textview.setText("正在加载更多");
                        break;
                }
            }


        }

        @Override
        public int getItemCount() {
            if (have_more) {
                return list.size() + 1;
            } else {
                return list.size();
            }
        }


        public class PostItemHolder extends RecyclerView.ViewHolder {
            private CircleImageView circleImageView;
            private TextView username;
            private TextView content;
            private ImageView imageView;


            public PostItemHolder(View itemView) {
                super(itemView);
                circleImageView = (CircleImageView) itemView.findViewById(R.id.user_header_image);
                username = (TextView) itemView.findViewById(R.id.post_item_username);
                content = (TextView) itemView.findViewById(R.id.post_item_content);
                imageView = (ImageView) itemView.findViewById(R.id.post_item_img);
            }
        }

        @Override
        public int getItemViewType(int position) {
            if (position + 1 == getItemCount() && have_more) {
                return TYPE_FOOTER;
            } else {
                return TYPE_ITEM;
            }
        }

        public class FooterViewHolder extends RecyclerView.ViewHolder {
            private TextView footer_textview;

            public FooterViewHolder(View itemView) {
                super(itemView);
                footer_textview = (TextView) itemView.findViewById(R.id.footer_text);
            }
        }

        public void addItem(List<Post> newDatas) {
            newDatas.addAll(list);
            list.removeAll(list);
            list.addAll(newDatas);
            sortList(list);
            notifyDataSetChanged();
        }

        public void addMoreItem(List<Post> newDatas) {

            list.addAll(newDatas);

            sortList(list);
            notifyDataSetChanged();
        }

        private void sortList(List<Post> list) {
            ComparePost comparePost = new ComparePost();
            Collections.sort(list, comparePost);
        }

        public void changeMoreStatus(int status) {
            load_more_status = status;
            notifyDataSetChanged();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值