您的位置:首页 > 产品设计 > UI/UE

BaseRecycleViewAdapter 、BaseQuickAdapter、BaseAdapter 的用法区别

2017-05-26 09:52 465 查看
BaseRecycleViewAdapter 、BaseQuickAdapter、BaseAdapter 的用法区别

 

BaseRecycleViewAdapter 和BaseQuickAdapter的用法类似,只是quickAdapter可以设置当列表下拉到底部的时候刷新获取到更多的数据,在代码中设置adapter的开启如下方法即可:adapter.openLoadAnimation();

adapter.setOnLoadMoreListener(CloudPhotosActivity.this);、
adapter.openLoadMore(pageSize, true);

而baseRecycleViewAdapter是没有如此方法实现这样的,只能手动获取其它页的数据然后再添加到之前数据列表的后面。这两种adapter在自定义撰写代码的时候,它们的代码逻辑内涵是一模一样的,仔细对照代码即可领会。

 

BaseAdapter 的最简单容易的Adapter,五个重要点掌握:

1、 public int getCount() {

        return mediaInfoList.size();

    }

2、public Object getItem(int position) {

        return mediaInfoList.get(position);

    }

3、public CloudPhotosGridViewAdapter(Context c, List<CloudPhotosMediaInfo> mediaInfoList) {

        this.context = c;

        this.mediaInfoList = mediaInfoList;

        stringList = new ArrayList<>();

    }

4、/**

     * 存放列表项控件句柄

     */

    private class ViewHolder {

        ImageView iv_check;

        TextView tv_duration;

        ImageView iv_content;

    }

5、    public View getView(int position, View convertView, ViewGroup parent) {

        viewHolder = null;

        if (convertView == null) {

            viewHolder = new ViewHolder();

            convertView = LayoutInflater.from(context).inflate(

                    R.layout.cloudphotos_gridview_item, parent, false);

           ...

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        .....

        return convertView;

}

 

代码区别:

public class CloudPhotosRecycleAdapter extends BaseRecycleViewAdapter {

    private Context context;

    private List<CloudPhotosInfo> list;//暂时定义为图片地址字符集

    public CloudPhotosRecycleAdapter(Context context, List<CloudPhotosInfo> list) {

        this.context = context;

        this.list = list;

    }

    private boolean isAllSeleted =false;

    public void setAllIsSeleted(boolean a){

        this.isAllSeleted = a;

    }

    private boolean isSeleted =false;

    public void setIsSeleted(boolean b){

        this.isSeleted = b;

    }

    @Override

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

 

        View view = LayoutInflater.from(context).inflate(R.layout.cloudphotos_recycleview_item, parent, false);

        RecyclerView.ViewHolder viewHolder  = new GroupsViewHolder(view);

        return viewHolder;

    }

 

    @Override

    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        GroupsViewHolder holder = (GroupsViewHolder) viewHolder;

        CloudPhotosInfo cloudPhotosInfo = list.get(position);

        System.out.println("CloudPhotosInfo -- list" + list.toString() );

        List<CloudPhotosMediaInfo> mediaInfoList = cloudPhotosInfo.getData();

        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        int width = windowManager.getDefaultDisplay().getWidth();//获取当前手机屏幕宽度

        int height = windowManager.getDefaultDisplay().getHeight();//获取当前手机屏幕高度

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( holder.gridView.getLayoutParams());

        int line = mediaInfoList.size() / 4;

        layoutParams.height = width/4*line +(line-1)*3;

        holder.gridView.setLayoutParams(layoutParams);

        CloudPhotosGridViewAdapter adapter = new CloudPhotosGridViewAdapter(context,mediaInfoList);

        if (isSeleted){

            adapter.setIsSeleted(true);

            adapter.notifyDataSetChanged();

        }else {

            adapter.setIsSeleted(false);

            adapter.notifyDataSetChanged();

        }

        holder.gridView.setAdapter(adapter);

        holder.tv_time.setText(cloudPhotosInfo.getDate_time());

    }

    class GroupsViewHolder extends RecyclerView.ViewHolder{

        public TextView tv_time;

        public MyCloudPhotosGridView gridView;

 

        public GroupsViewHolder(View itemView) {

            super(itemView);

            tv_time = (TextView) itemView.findViewById(R.id.tv_time);

            gridView = (MyCloudPhotosGridView) itemView.findViewById(R.id.gridView);

//            

        }

    }

    @Override

    public int getItemCount() {

        return list.size();

    }

 

}

 

 

 

public class CloudPhotosRecycleAdapter extends BaseQuickAdapter<CloudPhotosInfo>{

    private Context context;

    private List<CloudPhotosInfo> list;//暂时定义为图片地址字符集

    public CloudPhotosRecycleAdapter(Context context, List<CloudPhotosInfo> list) {

        super(R.layout.cloudphotos_recycleview_item, list);

        this.context = context;

        this.list = list;

    }

 

    private boolean isSeleted =false;

    public void setIsSeleted(boolean b){

        this.isSeleted = b;

    }

    @Override

    protected void convert(BaseViewHolder helper, CloudPhotosInfo item) {

        TextView tv_time = (TextView) helper.getView(R.id.tv_time);

        MyCloudPhotosGridView  gridView = (MyCloudPhotosGridView) helper.getView(R.id.gridView);

//        CloudPhotosInfo cloudPhotosInfo = list.get();

        System.out.println("CloudPhotosInfo -- list" + list.toString() );

        List<CloudPhotosMediaInfo> mediaInfoList = item.getData();

        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        int width = windowManager.getDefaultDisplay().getWidth();//获取当前手机屏幕宽度

        int height = windowManager.getDefaultDisplay().getHeight();//获取当前手机屏幕高度

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( gridView.getLayoutParams());

        int line = mediaInfoList.size() / 4;

        layoutParams.height = width/4*line +(line-1)*3;

        gridView.setLayoutParams(layoutParams);

        CloudPhotosGridViewAdapter adapter = new CloudPhotosGridViewAdapter(context,mediaInfoList);

        if (isSeleted){

            adapter.setIsSeleted(true);

            adapter.notifyDataSetChanged();

        }else {

            adapter.setIsSeleted(false);

            adapter.notifyDataSetChanged();

        }

        gridView.setAdapter(adapter);

        tv_time.setText(item.getDate_time());

 

}

 

 

public class CloudPhotosGridViewAdapter extends BaseAdapter {

    private Context context;

    private List<CloudPhotosMediaInfo> mediaInfoList;

    private ViewHolder viewHolder;

    private List<String> stringList;

 

    public CloudPhotosGridViewAdapter(Context c, List<CloudPhotosMediaInfo> mediaInfoList) {

        this.context = c;

        this.mediaInfoList = mediaInfoList;

        stringList = new ArrayList<>();

    }

    private boolean isSeleted = false;

 

    public void setIsSeleted(boolean b) {

        this.isSeleted = b;

    }

 

    @Override

    public int getCount() {

        return mediaInfoList.size();

    }

 

    @Override

    public Object getItem(int position) {

        return mediaInfoList.get(position);

    }

 

    @Override

    public long getItemId(int position) {

        return 0;

    }

 

 

    /**

     * 存放列表项控件句柄

     */

    private class ViewHolder {

        ImageView iv_check;

        TextView tv_duration;

        ImageView iv_content;

    }

 

    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        viewHolder = null;

        if (convertView == null) {

            viewHolder = new ViewHolder();

            convertView = LayoutInflater.from(context).inflate(

                    R.layout.cloudphotos_gridview_item, parent, false);

            viewHolder.iv_check = (ImageView) convertView.findViewById(R.id.iv_check);

            viewHolder.iv_content = (ImageView) convertView.findViewById(R.id.iv_content);

            viewHolder.tv_duration = (TextView) convertView.findViewById(R.id.tv_duration);

            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

            int width = windowManager.getDefaultDisplay().getWidth();//获取当前手机屏幕宽度

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(viewHolder.iv_content.getLayoutParams());

            lp.height = width / 4;//因为GridView分为4列,为了好看,也设置图片的高度和图片的宽度差不多

            lp.width = width / 4 ;

            viewHolder.iv_content.setLayoutParams(lp);

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        CloudPhotosMediaInfo mediaInfo = mediaInfoList.get(position);

        String url;

        if (mediaInfo.getPhoType() .equals("media")  ) {

            if (mediaInfo.getFrontImg() != null){

                url = mediaInfo.getFrontImg().getPhoUrl();

            }else {

                url = "";

            }

        } else {

            url = mediaInfo.getPhoUrl();

            viewHolder.tv_duration.setVisibility(View.INVISIBLE);

        }

//        Glide.with(context).load(url)

//                .diskCacheStrategy(DiskCacheStrategy.ALL)

//                .error(R.drawable.ic_fail)

//                .centerCrop()

//                .into(viewHolder.iv_content);

        Glide.with(context).load(Constant.photo_url_small + url)

                .diskCacheStrategy(DiskCacheStrategy.ALL)

//                    .error(R.drawable.ic_fail)

                .centerCrop()

                .into(viewHolder.iv_content);

        ImageView ivCheck = viewHolder.iv_check;

 

        if (mediaInfoList.get(position).getIsSeleted()) {

            ivCheck.setVisibility(View.VISIBLE);

        } else {

            ivCheck.setVisibility(View.INVISIBLE);

        }

        viewHolder.iv_content.setOnLongClickListener(new View.OnLongClickListener() {

            @Override

            public boolean onLongClick(View v) {

                isSeleted = true;

//                Toast.makeText(context, "图片被长按了,右上显示", Toast.LENGTH_SHORT).show();

                EventBus.getDefault().post(new AllSeletedEvent(true));

                return true;

            }

        });

        if (isSeleted) {

            viewHolder.iv_content.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View v) {

                    if (ivCheck.getVisibility() == View.VISIBLE) {

                        ivCheck.setVisibility(View.INVISIBLE);

//                        Toast.makeText(context, position + "隐藏勾选", Toast.LENGTH_SHORT).show();

                        mediaInfoList.get(position).setIsSeleted(false);

                    } else {

                        ivCheck.setVisibility(View.VISIBLE);

//                        Toast.makeText(context, position + "显示勾选", Toast.LENGTH_SHORT).show();

                        mediaInfoList.get(position).setIsSeleted(true);

                    }

                }

            });

        } else {

            viewHolder.iv_content.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View v) {

                    Intent intent = new Intent(context, CloudphotosPhotoDetailActivity.class);

                    intent.putExtra("url",url);

                    context.startActivity(intent);

//                    Toast.makeText(context, "显示图片详情", Toast.LENGTH_SHORT).show();

                }

            });

        }

        return convertView;

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐