ShoppingMallApp02

2018-10-03  本文已影响6人  GeekGray

阅读原文

关联使用 Banner 库

下载地址:

 https://github.com/youth5201314/banner

使用 Banner 库

1.通过在build.gradle文件中添加远程依赖库在线使用,可能会出现下载慢或者无法下载的情况,可以考虑从阿里或者其他可用的中央仓库下载

2.从github上把banner库down下来,添加到项目中,本项目采用该方法,在添加banner库时遇到库中的依赖无法下载

//compile 'com.github.bumptech.glide:glide:3.6.1'

此时更换该依赖为本地jar包(偷梁换柱),即banner的build.gradle依赖如下配置

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    //compile 'com.github.bumptech.glide:glide:3.6.1'
    compile files('libs/glide-3.7.0.jar')
}

3.在更换本地jar包后,由于app这个module中也使用了同样的glide-3.7.0.jar包,所以运行会报value值为2 jar包冲突错误。解决方案如下

1.把app这个module中依赖的本地glide-3.7.0.jar包注释掉

2.也可以通过搜索类名移除重复类的方式,不过比较繁琐

3.推荐引用或下载最新的库,避免类似情况,影响开发进度

4.Android Studio 3.0+版本能有效避免jar包冲突的情况

在onCreateViewHolder方法和onBindViewHolder方法中加载绑定各个类型的布局

 /**
     * 相当于getView 创建ViewHolder部分代码
     * 创建ViewHolder
     *
     * @param parent
     * @param viewType 当前的类型
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        if (viewType == BANNER)
        {
            return new BannerViewHolder(mContext, mLayoutInflater.inflate(R.layout.banner_viewpager, null));
        }
//        else if (viewType == CHANNEL)
//        {
//            return new ChannelViewHolder(mContext, mLayoutInflater.inflate(R.layout.channel_item, null));
//        }
//        else if (viewType == ACT)
//        {
//            return new ActViewHolder(mContext, mLayoutInflater.inflate(R.layout.act_item, null));
//        }
//        else if (viewType == SECKILL)
//        {
//            return new SeckillViewHolder(mContext, mLayoutInflater.inflate(R.layout.seckill_item, null));
//        }
//        else if (viewType == RECOMMEND)
//        {
//            return new RecommendViewHolder(mContext, mLayoutInflater.inflate(R.layout.recommend_item, null));
//        }
//        else if (viewType == HOT)
//        {
//            return new HotViewHolder(mContext, mLayoutInflater.inflate(R.layout.hot_item, null));
//        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        if (getItemViewType(position) == BANNER)
        {
            BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
            bannerViewHolder.setData(resultBean.getBanner_info());
        }
//        else if (getItemViewType(position) == CHANNEL)
//        {
//            ChannelViewHolder channelViewHolder = (ChannelViewHolder) holder;
//            channelViewHolder.setData(resultBean.getChannel_info());
//        }
//        else if (getItemViewType(position) == ACT)
//        {
//            ActViewHolder actViewHolder = (ActViewHolder) holder;
//            actViewHolder.setData(resultBean.getAct_info());
//        }
//        else if (getItemViewType(position) == SECKILL)
//        {
//            SeckillViewHolder seckillViewHolder = (SeckillViewHolder) holder;
//            seckillViewHolder.setData(resultBean.getSeckill_info());
//        }
//        else if (getItemViewType(position) == RECOMMEND)
//        {
//            RecommendViewHolder recommendViewHolder = (RecommendViewHolder) holder;
//            recommendViewHolder.setData(resultBean.getRecommend_info());
//        }
//        else if (getItemViewType(position) == HOT)
//        {
//            HotViewHolder hotViewHolder = (HotViewHolder) holder;
//            hotViewHolder.setData(resultBean.getHot_info());
//        }
    }

创建BannerViewHolder

 class BannerViewHolder extends RecyclerView.ViewHolder
    {
        private Context mContext;
        private Banner banner;

        public BannerViewHolder(Context mContext, View itemView)
        {
            super(itemView);
            this.mContext = mContext;
            this.banner = (Banner) itemView.findViewById(R.id.banner);
        }

        public void setData(List<ResultBeanData.ResultBean.BannerInfoBean> banner_info)
        {
            //设置Banner的数据
            //得到图片集合地址
            List<String> imagesUrl = new ArrayList<>();
            for (int i = 0; i < banner_info.size(); i++)
            {
                String imageUrl = banner_info.get(i).getImage();
                imagesUrl.add(imageUrl);
            }

            //设置循环指示点
            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            //设置手风琴效果
            banner.setBannerAnimation(Transformer.Accordion);
            banner.setImages(imagesUrl, new OnLoadImageListener()
            {
                @Override
                public void OnLoadImage(ImageView view, Object url)
                {

                    //联网请求图片-Glide
                    Glide.with(mContext).load(Constants.BASE_URL_IMAGE + url).into(view);

                }
            });

            //设置item的点击事件
            banner.setOnBannerClickListener(new OnBannerClickListener()
            {
                @Override
                public void OnBannerClick(int position)
                {
                    Toast.makeText(mContext, "position==" + position, Toast.LENGTH_SHORT).show();
//                    startGoodsInfoActivity(goodsBean);
                }
            });
        }
    }

设置 ChannelViewHolder

    /**
     * Channel
     */
    class ChannelViewHolder extends RecyclerView.ViewHolder
    {
        private Context mContext;
        private GridView gv_channel;
        private ChannelAdapter adapter;

        public ChannelViewHolder(final Context mContext, View itemView)
        {
            super(itemView);
            this.mContext = mContext;
            gv_channel = (GridView) itemView.findViewById(R.id.gv_channel);

            //设置item的点击事件
            gv_channel.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                {
                    Toast.makeText(mContext, "position" + position, Toast.LENGTH_SHORT).show();
                }
            });
        }


        public void setData(List<ResultBeanData.ResultBean.ChannelInfoBean> channel_info)
        {
            //得到数据了
            //设置GridView的适配器
            adapter = new ChannelAdapter(mContext, channel_info);
            gv_channel.setAdapter(adapter);
        }
    }

频道适配器 ChannelAdapter

/**
 * @author: Hashub
 * @WeChat: NGSHMVP
 * @Date: 2018/8/27 16:24
 * @function:频道的适配器
 */
public class ChannelAdapter extends BaseAdapter
{
    private final Context mContext;
    private final List<ResultBeanData.ResultBean.ChannelInfoBean> datas;

    public ChannelAdapter(Context mContext, List<ResultBeanData.ResultBean.ChannelInfoBean> channel_info)
    {
        this.mContext = mContext;
        this.datas = channel_info;
    }

    @Override
    public int getCount()
    {
        return datas.size();
    }

    @Override
    public Object getItem(int position)
    {
        return null;
    }

    @Override
    public long getItemId(int position)
    {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder;
        if (convertView == null)
        {
            convertView = View.inflate(mContext, R.layout.item_channel, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_channel);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_channel);
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //根据位置得到对应的数据
        ResultBeanData.ResultBean.ChannelInfoBean channelInfoBean = datas.get(position);
        Glide.with(mContext)
                .load(Constants.BASE_URL_IMAGE + channelInfoBean.getImage())
                .into(viewHolder.iv_icon);
        viewHolder.tv_title.setText(channelInfoBean.getChannel_name());
        return convertView;
    }

    static class ViewHolder
    {
        ImageView iv_icon;
        TextView tv_title;
    }
}

设置 ActViewHolder

    /**
     * Act
     */
    class ActViewHolder extends RecyclerView.ViewHolder
    {
        private Context mContext;
        private ViewPager act_viewpager;

        public ActViewHolder(Context mContext, View itemView)
        {
            super(itemView);
            this.mContext = mContext;
            act_viewpager = (ViewPager) itemView.findViewById(R.id.act_viewpager);
        }

        public void setData(final List<ResultBeanData.ResultBean.ActInfoBean> act_info)
        {
            act_viewpager.setPageMargin(20);
            act_viewpager.setOffscreenPageLimit(3);//>=3

            //setPageTransformer 决定动画效果
            act_viewpager.setPageTransformer(true, new ScaleInOutTransformer());

            //1.有数据了
            //2.设置适配器
            act_viewpager.setAdapter(new PagerAdapter()
            {
                @Override
                public int getCount()
                {
                    return act_info.size();
                }

                /**
                 *
                 * @param view 页面
                 * @param object instantiateItem方法返回的值
                 * @return
                 */
                @Override
                public boolean isViewFromObject(View view, Object object)
                {
                    return view == object;
                }

                /**
                 *
                 * @param container ViewPager
                 * @param position 对应页面的位置
                 * @return
                 */
                @Override
                public Object instantiateItem(ViewGroup container, final int position)
                {
                    ImageView imageView = new ImageView(mContext);
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);

                    Glide.with(mContext)
                            .load(Constants.BASE_URL_IMAGE+act_info.get(position).getIcon_url())
                            .into(imageView);

                    //添加到容器中
                    container.addView(imageView);

                    //设置点击事件
                    imageView.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v)
                        {
                            Toast.makeText(mContext, "position=="+position, Toast.LENGTH_SHORT).show();
                        }
                    });
                    return imageView;
                }

                @Override
                public void destroyItem(ViewGroup container, int position, Object object)
                {
                    container.removeView((View) object);
                }
            });
        }

    }

对 ViewPager 动画进行美化库的使用

需要用到第三方库
下载地址:

 https://github.com/hongyangAndroid/MagicViewPager

本项目采用Banner库中的美化库


秒杀适配器

设置 SeckillViewHolder

    /**
     * SeckillViewHolder
     */
    class SeckillViewHolder extends RecyclerView.ViewHolder
    {
        private final Context mContext;
        private TextView tv_time_seckill;
        private TextView tv_more_seckill;
        private RecyclerView rv_seckill;
        private SeckillRecyclerViewAdapter adapter;

        /**
         * 相差多少时间-毫秒
         */
        private long dt = 0;

        private Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                super.handleMessage(msg);
                dt = dt - 1000;
                SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                String time = formatter.format(new Date(dt));
                tv_time_seckill.setText(time);

                handler.removeMessages(0);
                handler.sendEmptyMessageDelayed(0, 1000);
                if (dt <= 0)
                {
                    //把消息移除
                    handler.removeCallbacksAndMessages(null);
                }

            }
        };

        public SeckillViewHolder(Context mContext, View itemView)
        {
            super(itemView);
            tv_time_seckill = (TextView) itemView.findViewById(R.id.tv_time_seckill);
            tv_more_seckill = (TextView) itemView.findViewById(R.id.tv_more_seckill);
            rv_seckill = (RecyclerView) itemView.findViewById(R.id.rv_seckill);
            this.mContext = mContext;
        }

        public void setData(final ResultBeanData.ResultBean.SeckillInfoBean seckill_info)
        {
            //1.得到数据了
            //2.设置数据:文本和RecyclerView的数据
            adapter = new SeckillRecyclerViewAdapter(mContext, seckill_info.getList());
            rv_seckill.setAdapter(adapter);

            //设置布局管理器
            rv_seckill.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
            //设置item的点击事件

            adapter.setOnSeckillRecyclerView(new SeckillRecyclerViewAdapter.OnSeckillRecyclerView()
            {
                @Override
                public void onItemClick(int position)
                {
                    Toast.makeText(mContext, "秒杀" + position, Toast.LENGTH_SHORT).show();

                    ResultBeanData.ResultBean.SeckillInfoBean.ListBean listBean = seckill_info.getList().get(position);

                    GoodsBean goodsBean = new GoodsBean();
                    goodsBean.setCover_price(listBean.getCover_price());
                    goodsBean.setFigure(listBean.getFigure());
                    goodsBean.setName(listBean.getName());
                    goodsBean.setProduct_id(listBean.getProduct_id());
                    startGoodsInfoActivity(goodsBean);
                }
            });

            //秒杀倒计时 -毫秒
            dt = Integer.valueOf(seckill_info.getEnd_time()) - Integer.valueOf(seckill_info.getStart_time());


            handler.sendEmptyMessageDelayed(0, 1000);
        }

    }

设置适配器代码 SeckillRecyclerViewAdapter

/**
 * @author: Hashub
 * @WeChat: NGSHMVP
 * @Date: 2018/8/27 16:58
 * @function:秒杀的适配器
 */
public class SeckillRecyclerViewAdapter extends RecyclerView.Adapter<SeckillRecyclerViewAdapter.ViewHodler>
{
    private final List<ResultBeanData.ResultBean.SeckillInfoBean.ListBean> list;
    private final Context mContext;

    public SeckillRecyclerViewAdapter(Context mContext, List<ResultBeanData.ResultBean.SeckillInfoBean.ListBean> list)
    {
        this.list = list;
        this.mContext = mContext;

    }

    @Override
    public SeckillRecyclerViewAdapter.ViewHodler onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View itemView = View.inflate(mContext, R.layout.item_seckill, null);
        return new ViewHodler(itemView);
    }

    @Override
    public void onBindViewHolder(SeckillRecyclerViewAdapter.ViewHodler holder, int position)
    {
//1.根据位置得到对应的数据
        ResultBeanData.ResultBean.SeckillInfoBean.ListBean listBean = list.get(position);

        //2.绑定数据

        Glide.with(mContext)
                .load(Constants.BASE_URL_IMAGE + listBean.getFigure())
                .into(holder.iv_figure);
        holder.tv_cover_price.setText(listBean.getCover_price());
        holder.tv_origin_price.setText(listBean.getOrigin_price());
    }

    @Override
    public int getItemCount()
    {
        return list.size();
    }

    class ViewHodler extends RecyclerView.ViewHolder
    {
        private ImageView iv_figure;
        private TextView tv_cover_price;
        private TextView tv_origin_price;

        public ViewHodler(View itemView)
        {
            super(itemView);
            iv_figure = (ImageView) itemView.findViewById(R.id.iv_figure);
            tv_cover_price = (TextView) itemView.findViewById(R.id.tv_cover_price);
            tv_origin_price = (TextView) itemView.findViewById(R.id.tv_origin_price);

            itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
//                    Toast.makeText(mContext, "秒杀="+getLayoutPosition(), Toast.LENGTH_SHORT).show();
                    if (onSeckillRecyclerView != null)
                    {
                        onSeckillRecyclerView.onItemClick(getLayoutPosition());
                    }
                }
            });
        }
    }

    /**
     * 监听器
     */
    public interface OnSeckillRecyclerView
    {
        /**
         * 当某条被点击的时候回调
         *
         * @param position
         */
        public void onItemClick(int position);
    }

    private OnSeckillRecyclerView onSeckillRecyclerView;

    /**
     * 设置item的监听
     *
     * @param onSeckillRecyclerView
     */
    public void setOnSeckillRecyclerView(OnSeckillRecyclerView onSeckillRecyclerView)
    {
        this.onSeckillRecyclerView = onSeckillRecyclerView;
    }
}

设置 RecyclerView 的 item 的点击事件的监听

    /**
     * 监听器
     */
    public interface OnSeckillRecyclerView
    {
        /**
         * 当某条被点击的时候回调
         *
         * @param position
         */
        public void onItemClick(int position);
    }

    private OnSeckillRecyclerView onSeckillRecyclerView;

    /**
     * 设置item的监听
     *
     * @param onSeckillRecyclerView
     */
    public void setOnSeckillRecyclerView(OnSeckillRecyclerView onSeckillRecyclerView)
    {
        this.onSeckillRecyclerView = onSeckillRecyclerView;
    }

设置倒计时

        /**
         * 相差多少时间-毫秒
         */
        private long dt = 0;

        private Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                super.handleMessage(msg);
                dt = dt - 1000;
                SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                String time = formatter.format(new Date(dt));
                tv_time_seckill.setText(time);

                handler.removeMessages(0);
                handler.sendEmptyMessageDelayed(0, 1000);
                if (dt <= 0)
                {
                    //把消息移除
                    handler.removeCallbacksAndMessages(null);
                }

            }
        };

推荐适配器

RecommendViewHolder

    /**
     * RecommendViewHolder
     */
    class RecommendViewHolder extends RecyclerView.ViewHolder
    {

        private final Context mContext;
        private TextView tv_more_recommend;
        private GridView gv_recommend;
        private RecommendGridViewAdapter adapter;

        public RecommendViewHolder(final Context mContext, View itemView)
        {
            super(itemView);
            this.mContext = mContext;
            tv_more_recommend = (TextView) itemView.findViewById(R.id.tv_more_recommend);
            gv_recommend = (GridView) itemView.findViewById(R.id.gv_recommend);
        }

        public void setData(final List<ResultBeanData.ResultBean.RecommendInfoBean> recommend_info)
        {
            //1.有数据了
            //2.设置适配器
            adapter = new RecommendGridViewAdapter(mContext, recommend_info);
            gv_recommend.setAdapter(adapter);

            gv_recommend.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                {
                    Toast.makeText(mContext, "position==" + position, Toast.LENGTH_SHORT).show();
                    ResultBeanData.ResultBean.RecommendInfoBean recommendInfoBean = recommend_info.get(position);

                    GoodsBean goodsBean = new GoodsBean();
                    goodsBean.setCover_price(recommendInfoBean.getCover_price());
                    goodsBean.setFigure(recommendInfoBean.getFigure());
                    goodsBean.setName(recommendInfoBean.getName());
                    goodsBean.setProduct_id(recommendInfoBean.getProduct_id());
                    startGoodsInfoActivity(goodsBean);
                }
            });
        }

    }

RecommendGridViewAdapter 适配器代码

/**
 * @author: Hashub
 * @WeChat: NGSHMVP
 * @Date: 2018/8/28 15:12
 * @function:推荐的适配器
 */
public class RecommendGridViewAdapter extends BaseAdapter
{
    private final Context mContext;
    private final List<ResultBeanData.ResultBean.RecommendInfoBean> datas;

    public RecommendGridViewAdapter(Context mContext, List<ResultBeanData.ResultBean.RecommendInfoBean> recommend_info)
    {
        this.mContext = mContext;
        this.datas = recommend_info;
    }

    @Override
    public int getCount()
    {
        return datas.size();
    }

    @Override
    public Object getItem(int position)
    {
        return null;
    }

    @Override
    public long getItemId(int position)
    {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder;
        if (convertView == null)
        {
            convertView = View.inflate(mContext, R.layout.item_recommend_grid_view, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //根据位置得到对应的数据
        ResultBeanData.ResultBean.RecommendInfoBean recommendInfoBean = datas.get(position);
        Glide.with(mContext).load(Constants.BASE_URL_IMAGE + recommendInfoBean.getFigure()).into(viewHolder.ivRecommend);
        viewHolder.tvName.setText(recommendInfoBean.getName());
        viewHolder.tvPrice.setText("¥" + recommendInfoBean.getCover_price());

        return convertView;
    }

    static class ViewHolder
    {
        @Bind(R.id.iv_recommend)
        ImageView ivRecommend;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_price)
        TextView tvPrice;

        public ViewHolder(View view)
        {
            ButterKnife.bind(this, view);
        }
    }
}

热卖适配器

HotViewHolder

    /**
     * HotViewHolder
     */
    class HotViewHolder extends RecyclerView.ViewHolder
    {
        private final Context mContext;

        private TextView tv_more_hot;
        private GridView gv_hot;

        public HotViewHolder(final Context mContext, View itemView)
        {
            super(itemView);
            this.mContext = mContext;
            tv_more_hot = (TextView) itemView.findViewById(R.id.tv_more_hot);
            gv_hot = (GridView) itemView.findViewById(R.id.gv_hot);

        }

        public void setData(final List<ResultBeanData.ResultBean.HotInfoBean> hot_info)
        {
            //1.有数据
            //2.设置GridView的适配器
            HotGridViewAdapter adapter = new HotGridViewAdapter(mContext, hot_info);
            gv_hot.setAdapter(adapter);

            //设置item的监听
            gv_hot.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                {
                    Toast.makeText(mContext, "position==" + position, Toast.LENGTH_SHORT).show();
                    //热卖商品信息类
                    ResultBeanData.ResultBean.HotInfoBean hotInfoBean = hot_info.get(position);

                    //商品信息类
                    GoodsBean goodsBean = new GoodsBean();
                    goodsBean.setCover_price(hotInfoBean.getCover_price());
                    goodsBean.setFigure(hotInfoBean.getFigure());
                    goodsBean.setName(hotInfoBean.getName());
                    goodsBean.setProduct_id(hotInfoBean.getProduct_id());
                    startGoodsInfoActivity(goodsBean);
                }
            });
        }
    }

适配器代码

/**
 * @author: Hashub
 * @WeChat: NGSHMVP
 * @Date: 2018/8/28 15:35
 * @function:
 */
public class HotGridViewAdapter extends BaseAdapter
{
    private Context mContext;
    private List<ResultBeanData.ResultBean.HotInfoBean> datas;

    public HotGridViewAdapter(Context mContext, List<ResultBeanData.ResultBean.HotInfoBean> hot_info)
    {
        this.mContext = mContext;
        this.datas = hot_info;
    }

    @Override
    public int getCount()
    {
        return datas.size();
    }

    @Override
    public Object getItem(int position)
    {
        return null;
    }

    @Override
    public long getItemId(int position)
    {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder;
        if (convertView == null)
        {
            convertView = View.inflate(mContext, R.layout.item_hot_grid_view, null);
            viewHolder = new ViewHolder(convertView);

            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //根据位置得到对应的数据
        ResultBeanData.ResultBean.HotInfoBean hotInfoBean = datas.get(position);
        Glide.with(mContext).load(Constants.BASE_URL_IMAGE + hotInfoBean.getFigure()).into(viewHolder.ivHot);
        viewHolder.tvName.setText(hotInfoBean.getName());
        viewHolder.tvPrice.setText("¥" + hotInfoBean.getCover_price());
        return convertView;
    }

    static class ViewHolder
    {
        @Bind(R.id.iv_hot)
        ImageView ivHot;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_price)
        TextView tvPrice;

        ViewHolder(View view)
        {
            ButterKnife.bind(this, view);
        }
    }
}

设置监听 RecyclerView 的位置

隐藏和显示回到顶部按钮

if(resultBean!=null)
        {
            //有数据
            //设置适配器
            adapter=new HomeFragmentAdapter(mContext,resultBean);
            rvHome.setAdapter(adapter);
            GridLayoutManager manager=new GridLayoutManager(mContext,1);
            //设置跨度大小监听
            manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
            {
                @Override
                public int getSpanSize(int position)
                {
                    if(position<=2)
                    {
                        //隐藏
                        ib_top.setVisibility(View.GONE);
                    }
                    else
                    {
                        //显示
                        ib_top.setVisibility(View.VISIBLE);
                    }
                    //只能返回1
                    return 1;
                }
            });
            //设置布局管理者
            rvHome.setLayoutManager(manager);
        }
        else
        {
            //没有数据
        }

设置点击回调顶部

//置顶的监听
        ib_top.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //回到顶部
                rvHome.scrollToPosition(0);
            }
        });

商品信息列表类 GoodsInfoActivity

ScrollViewContainer 介绍

下载地址:

 https://github.com/jingchenUSTC/ScrollViewContainer

Android 仿淘宝商品浏览控件,用手机淘宝浏览商品详情时,商品图片是放在后
面的,在第一个 ScrollView 滚动到最底下时会有提示,继续拖动才能浏览下一
个 ScrollView 里的图片。

商品信息列表类页面实现分析

布局分成三部分:

1:标题栏
2:分割线
3:帧布局

1:线性布局

里面用 ScrollViewContainer 嵌套两个 ScrollView

2:线性布局

客服联系,收藏,购物车等

3:更多

分享, 搜索,首页等

商品信息列表类布局文件文件 activity_goods_info.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#fff"
              android:orientation="vertical"
              tools:context=".app.GoodsInfoActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <ImageButton
            android:id="@+id/ib_good_info_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/top_bar_left_back" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="商品详情"
            android:textColor="#564c4c"
            android:textSize="20sp" />

        <ImageButton
            android:id="@+id/ib_good_info_more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/icon_more" />

    </LinearLayout>
    <!--分割线-->
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#eeee" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="5dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <com.example.shoppingmall.view.ScrollViewContainer
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scrollbars="none">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#fff"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/iv_good_info_image"
                            android:layout_width="match_parent"
                            android:layout_height="350dp"
                            android:layout_gravity="center_horizontal" />

                        <TextView
                            android:id="@+id/tv_good_info_name"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="15dp"
                            android:text="【预售】《来年就交给你啦》珍藏画册 限量礼盒版"
                            android:textColor="#323427" />

                        <TextView
                            android:id="@+id/tv_good_info_desc"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="10dp"
                            android:text="预售截止10月15日,预售期为限量礼盒版,包括特制木盒x1、画集x1、邮票x1套、书签x1、贴纸x1、信封x1、礼盒特"
                            android:textColor="#767f86" />

                        <TextView
                            android:id="@+id/tv_good_info_price"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="10dp"
                            android:text="¥150.00"
                            android:textColor="#ed3f3f"
                            android:textSize="20sp" />

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:layout_margin="10dp"
                            android:background="#eeee" />

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            android:padding="10dp">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="由"
                                android:textColor="#3c3d40" />

                            <TextView
                                android:id="@+id/tv_good_info_store"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="尚硅谷"
                                android:textColor="#ff4040" />

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="发货"
                                android:textColor="#3c3d40" />
                        </LinearLayout>

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="8dp"
                            android:background="#eeee" />

                        <TextView
                            android:id="@+id/tv_good_info_style"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableRight="@drawable/home_arrow_right"
                            android:padding="10dp"
                            android:text="请选择款式"
                            android:textColor="#3c3d40" />

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="8dp"
                            android:background="#eeee" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="10dp"
                            android:text="提示:普通商品享有质量问题7天退货,签收后3个工作日内可换货。其他特殊商品(如手办)的售后以页面说明为准"
                            android:textColor="#3c3d40" />

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="8dp"
                            android:background="#eeee" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:padding="10dp"
                            android:text="图文详情"
                            android:textColor="#3c3d40"
                            android:textSize="15sp" />

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="#ed3f3f" />

                    </LinearLayout>
                </ScrollView>

                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <WebView
                        android:id="@+id/wb_good_info_more"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"></WebView>

                </ScrollView>

            </com.example.shoppingmall.view.ScrollViewContainer>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_goods_root"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#fff">

            <TextView
                android:id="@+id/tv_good_info_callcenter"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawablePadding="5dp"
                android:drawableTop="@drawable/icon_callserver_unpressed"
                android:gravity="center"
                android:padding="2dp"
                android:text="联系客服"
                android:textColor="#393b3e" />

            <TextView
                android:id="@+id/tv_good_info_collection"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawablePadding="5dp"
                android:drawableTop="@drawable/good_uncollected"
                android:gravity="center"
                android:padding="2dp"
                android:text="收藏"
                android:textColor="#393b3e" />


            <TextView
                android:id="@+id/tv_good_info_cart"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawablePadding="5dp"
                android:drawableTop="@drawable/icon_good_detail_cart"
                android:gravity="center"
                android:padding="2dp"
                android:text="购物车"
                android:textColor="#393b3e" />

            <Button
                android:id="@+id/btn_good_info_addcart"
                android:layout_width="120dp"
                android:layout_height="51dp"
                android:layout_gravity="center"
                android:background="@drawable/add_cart_bg_selector"
                android:gravity="center"
                android:text="加入购物车"
                android:textColor="#fff" />
        </LinearLayout>

        <include layout="@layout/more_layout" />
    </FrameLayout>


</LinearLayout>

布局的实例化和设置点击事件

public class GoodsInfoActivity extends Activity
{
    /**
     * 控件
     */
    @Bind(R.id.ib_good_info_back)
    ImageButton ibGoodInfoBack;
    @Bind(R.id.ib_good_info_more)
    ImageButton ibGoodInfoMore;
    @Bind(R.id.iv_good_info_image)
    ImageView ivGoodInfoImage;
    @Bind(R.id.tv_good_info_name)
    TextView tvGoodInfoName;
    @Bind(R.id.tv_good_info_desc)
    TextView tvGoodInfoDesc;
    @Bind(R.id.tv_good_info_price)
    TextView tvGoodInfoPrice;
    @Bind(R.id.tv_good_info_store)
    TextView tvGoodInfoStore;
    @Bind(R.id.tv_good_info_style)
    TextView tvGoodInfoStyle;
    @Bind(R.id.wb_good_info_more)
    WebView wbGoodInfoMore;
    @Bind(R.id.tv_good_info_callcenter)
    TextView tvGoodInfoCallcenter;
    @Bind(R.id.tv_good_info_collection)
    TextView tvGoodInfoCollection;
    @Bind(R.id.tv_good_info_cart)
    TextView tvGoodInfoCart;
    @Bind(R.id.btn_good_info_addcart)
    Button btnGoodInfoAddcart;
    @Bind(R.id.ll_goods_root)
    LinearLayout llGoodsRoot;
    @Bind(R.id.tv_more_share)
    TextView tvMoreShare;
    @Bind(R.id.tv_more_search)
    TextView tvMoreSearch;
    @Bind(R.id.tv_more_home)
    TextView tvMoreHome;
    @Bind(R.id.btn_more)
    Button btnMore;
    @Bind(R.id.ll_root)
    LinearLayout llRoot;

    private GoodsBean goodsBean;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_info);
        ButterKnife.bind(this);

        //接收数据
        goodsBean = (GoodsBean) getIntent().getSerializableExtra("goodsBean");
        if (goodsBean != null)
        {
            setDataForView(goodsBean);
        }


    }

    /**
     * 设置数据
     *
     * @param goodsBean
     */
    private void setDataForView(GoodsBean goodsBean)
    {
        //设置图片
        //iv_good_info_image
        Glide.with(this)
                .load(Constants.BASE_URL_IMAGE + goodsBean.getFigure())
                .into(ivGoodInfoImage);

        //设置文本
        tvGoodInfoName.setText(goodsBean.getName());

        //设置价格
        tvGoodInfoPrice.setText("¥" + goodsBean.getCover_price());


        setWebViewData(goodsBean.getProduct_id());

    }

    private void setWebViewData(String product_id)
    {
        if (product_id != null)
        {
            wbGoodInfoMore.loadUrl("http://www.atguigu.com");

            WebSettings webSettings = wbGoodInfoMore.getSettings();
            //支持双击页面变大变小
            webSettings.setUseWideViewPort(true);
            //设置支持JavaScript
            webSettings.setJavaScriptEnabled(true);
            //优先使用缓存
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            wbGoodInfoMore.setWebViewClient(new WebViewClient()
            {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url)
                {
                    //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
                    view.loadUrl(url);
                    return true;
                }
            });
        }

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.ib_good_info_back, R.id.ib_good_info_more, R.id.tv_good_info_callcenter, R.id.tv_good_info_collection, R.id.tv_good_info_cart, R.id.btn_good_info_addcart, R.id.tv_more_share, R.id.tv_more_search, R.id.tv_more_home})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.ib_good_info_back:
                finish();
                break;

            case R.id.ib_good_info_more:
                Toast.makeText(this, "更多", Toast.LENGTH_SHORT).show();
                break;

            case R.id.tv_good_info_callcenter:
                Toast.makeText(this, "客户中心", Toast.LENGTH_SHORT).show();
                break;

            case R.id.tv_good_info_collection:
                Toast.makeText(this, "收藏", Toast.LENGTH_SHORT).show();
                break
                        ;
            case R.id.tv_good_info_cart:
                Toast.makeText(this, "购物车", Toast.LENGTH_SHORT).show();
                break;

            case R.id.btn_good_info_addcart:
                Toast.makeText(this, "添加到成功了", Toast.LENGTH_SHORT).show();
                break;

            case R.id.tv_more_share:
                Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();
                break;

            case R.id.tv_more_search:
                Toast.makeText(this, "搜索", Toast.LENGTH_SHORT).show();
                break;

            case R.id.tv_more_home:
                Toast.makeText(this, "主页面", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

上一篇 下一篇

猜你喜欢

热点阅读