RecycleView的用法

来源:互联网 发布:网络电视剧排名榜 编辑:程序博客网 时间:2024/05/01 13:52


用到了个别人写的Recycleview


https://github.com/WuXiaolong/PullLoadMoreRecyclerView

dependencies {  compile 'com.wuxiaolong.pullloadmorerecyclerview:library:1.1.1'}

========================================================================================================

传了参数

followAdapter = new FollowListAdapter(checkList, FollowInActivity.this);mPullLoadMoreRecyclerView.setAdapter(followAdapter);
并且在赋值checklist时用了addAll(),notifyDataSetChanged()

checkList.addAll(module.getList());followAdapter.notifyDataSetChanged();

=============================================================================

以下是adpter:

继承自RecycleViewAdapter<xxx.ViewHolder>

public class FollowListAdapter extends RecyclerView.Adapter<FollowListAdapter.MyHolder>{    private List<CheckListModel.subList> items;//数据    private OnRecyclerViewItemClickListener listener;    private Context context;    public interface OnRecyclerViewItemClickListener {        void onItemClick(View v, int position);    }    public void setOnItemClickListsner(OnRecyclerViewItemClickListener listener) {        this.listener = listener;    }    public FollowListAdapter(List<CheckListModel.subList> items,Context context){        this.context = context;        this.items = items;//数据    }    @Override    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View v = LayoutInflater.from(context).inflate(R.layout.item_index_check_layout, parent, false);        return new MyHolder(v);    }    @SuppressWarnings("deprecation")    @Override    public void onBindViewHolder(final MyHolder holder, int position) {        holder.itemView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (listener != null) {                    listener.onItemClick(holder.itemView, holder.getAdapterPosition());                }            }        });        //5.0以上添加点击水波纹动画效果        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {            StateListAnimator stateListAnimator = AnimatorInflater.loadStateListAnimator(context, R.drawable.cardview_anim);            holder.cardView.setStateListAnimator(stateListAnimator);        }        CheckListModel.subList item = items.get(position);//单个条目中的数据        String time = TimeUtils.convertTimeToFormat(Long.parseLong(item.getDate()));        holder.time.setText(time);        holder.name.setText(item.getName());        holder.status_desc.setText(item.getProgress());        holder.status_desc.setTextColor(context.getResources().getColor(R.color.red_light));    }    @Override    public int getItemCount() {        if (items == null || items.size() == 0) {            return 0;        }        return items.size();    }    public static class MyHolder extends RecyclerView.ViewHolder {        TextView name, time, status_desc;        CircleImageView image;        CardView cardView;        public MyHolder(View contentView) {            super(contentView);            name = (TextView) contentView.findViewById(R.id.name);            time = (TextView) contentView.findViewById(R.id.time);            status_desc = (TextView) contentView.findViewById(R.id.status_desc);            image = (CircleImageView) contentView.findViewById(R.id.portrait_image);            cardView = (CardView) contentView.findViewById(R.id.cardView);        }    }}

========================================================================================================

item的布局中的marginBottom可以调节条目间距


======================================================================






以上的Adapter太乱,付个简单点的

public class CarPickListAdapter extends RecyclerView.Adapter<CarPickListAdapter.VH> {    private List<CarListModel.subList> items;    private OnRecyclerViewItemClickListener listener;    private Context context;    public interface OnRecyclerViewItemClickListener {        void onItemClick(View v, int position);    }    public void setOnItemClickListsner(OnRecyclerViewItemClickListener listsner) {        this.listener = listsner;    }    public CarPickListAdapter(Context context, List<CarListModel.subList> items) {        this.items = items;        this.context = context;    }    @Override    public VH onCreateViewHolder(ViewGroup parent, int viewType) {        View v = LayoutInflater.from(context).inflate(R.layout.item_custom_list_layout, parent, false);        return new VH(v);    }    @Override    public void onBindViewHolder(final VH holder, int position) {        if (position != 0) {            holder.itemView.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    if (listener != null) {                        listener.onItemClick(holder.itemView, holder.getAdapterPosition() - 1);                    }                }            });        }        if (position == 0) {            holder.id.setText("客户编号");            holder.name.setText("客户姓名");        } else {            CarListModel.subList item = items.get(position - 1);            holder.id.setText(item.getCustId());            holder.name.setText(item.getName());        }    }    @Override    public int getItemCount() {        if (items == null || items.isEmpty()) {            return 0;        }        return items.size() + 1;    }    public static class VH extends RecyclerView.ViewHolder {        private TextView id, name;        public VH(View itemView) {            super(itemView);            id = (TextView) itemView.findViewById(R.id.name_tv);            name = (TextView) itemView.findViewById(R.id.status_tv);        }    }}


0 0