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();
}
}
记录
最新推荐文章于 2024-10-08 15:18:03 发布