实现GridView的横向滚动

效果如下图:

具体实现的代码

•1. 主界面布局代码:activity_main.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_margin="5dp" >

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_margin="10dp" >

android:id="@+id/home_grid"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center"

android:numColumns="auto_fit"

android:stretchMode="spacingWidthUniform" >

•2.主界面GridView列表子项布局文件:item_homepage_hor_grid.xml

android:id="@+id/item_homepage_hor_grid_item"

android:layout_width="match_parent"

android:layout_height="186dp"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@color/white"

android:gravity="center"

android:orientation="vertical"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:orientation="vertical">

android:id="@+id/home_page_jrtj_img"

android:layout_width="125dp"

android:layout_height="125dp"

android:scaleType="fitXY"

android:src="@mipmap/home_jrtj_sp_1" />

android:paddingStart="@dimen/space_5dp"

android:id="@+id/home_page_jrtj_tv_title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginTop="@dimen/space_5dp"

android:ellipsize="end"

android:maxLines="2"

android:text="小米(MI)小米电视4A 标准版 55英寸 HDR 2GB+8GB 四核64位高性能处理器 4K超高清智能语音网络液晶平板电视(L55M5-AZ)"

android:textColor="#333333"

android:textSize="11sp" />

android:paddingStart="@dimen/space_5dp"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_vertical"

android:orientation="horizontal"

android:paddingBottom="@dimen/space_5dp"

android:paddingTop="@dimen/space_5dp">

android:id="@+id/home_page_jrtj_tv_price"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:ellipsize="end"

android:maxLines="1"

android:text="¥4998"

android:textColor="#f50000"

android:textSize="@dimen/font_size_15sp" />

android:id="@+id/home_page_jrtj_tv_huajia"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginEnd="@dimen/space_10dp"

android:layout_marginStart="@dimen/space_5dp"

android:layout_marginTop="@dimen/space_2dp"

android:ellipsize="end"

android:maxLines="1"

android:text="¥ 4998"

android:textColor="#d2d2d2"

android:textSize="@dimen/font_size_11sp" />

•3.  java实现代码:MainActivity.java

首先是Javabean

public class HomePageJrTjTwoBean {

private int img;

private String title;

private String old_price;

private String price;

public int getImg() {

return img;

}

public void setImg(int img) {

this.img = img;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getPrice() {

return price;

}

public void setPrice(String price) {

this.price = price;

}

public String getOld_price() {

return old_price;

}

public void setOld_price(String old_price) {

this.old_price = old_price;

}

}

Activity代码

public class MainActivity extends Activity {

//横向GridView

@BindView(R.id.home_grid)

GridView home_grid;

// 数据源

private List relist = new ArrayList<>();

private CommonAdapters re_adapter = null;

private List listData_two = new ArrayList<>();

private int[] jrtj_two = new int[]{R.mipmap.home_jrtj_sp_1, R.mipmap.home_jrtj_sp_2, R.mipmap.home_jrtj_sp_3, R.mipmap.home_jrtj_sp_3, R.mipmap.home_jrtj_sp_3};//今日特价横向数据

private String[] sp_name = new String[]{"老板(Roba1m)大吸力免 拆洗触控侧吸式...", "老板(Roba1m)大吸力免 拆洗触控侧吸式...", "老板(Roba1m)大吸力免 拆洗触控侧吸式...", "老板(Roba1m)大吸力免 拆洗触控侧吸式...", "老板(Roba1m)大吸力免 拆洗触控侧吸式..."};

private String[] price = new String[]{"¥5517", "¥5517", "¥5517", "¥5517", "¥5517"};

private String[] hua_price = new String[]{"¥8888", "¥8888", "¥8888", "¥8888", "¥8888"};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//自己造的假数据,实际开发中从后台获取,再去刷新Grid的适配器,调用 re_adapter.notifyDataSetChanged();

for (int i = 0; i < jrtj_two.length; i++) {

HomePageJrTjTwoBean homePageJrTjTwoBean = new HomePageJrTjTwoBean();

homePageJrTjTwoBean.setImg(jrtj_two[i]);

homePageJrTjTwoBean.setTitle(sp_name[i]);

homePageJrTjTwoBean.setPrice(price[i]);

homePageJrTjTwoBean.setOld_price(hua_price[i]);

listData_two.add(homePageJrTjTwoBean);

}

//初始化横向的GridView

initHorGridView();

}

private void initHorGridView() {

int listSize = relist.size();

int wm = TUtil.getScreenWidth(getActivity());

int itemWidth = DisplayUtil.dip2px(getActivity(), 120);

int horizontalSpacing = DisplayUtil.dip2px(getActivity(), 10);

int allWidth = (int) ((listSize) * (itemWidth + horizontalSpacing));

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(allWidth,

LinearLayout.LayoutParams.WRAP_CONTENT);

homepage_hor_grid.setLayoutParams(params);

homepage_hor_grid.setColumnWidth(itemWidth);

homepage_hor_grid.setNumColumns(listSize);

homepage_hor_grid.setHorizontalSpacing(1);

re_adapter = new CommonAdapters(getActivity(), relist, R.layout.item_homepage_hor_grid) {

@Override

public void convert(ViewHolders holder, QgitemBean datas) {

//商品图片

ImageView home_page_jrtj_img = holder.getView(R.id.home_page_jrtj_img);

//商品标题

TextView home_page_jrtj_tv_title = holder.getView(R.id.home_page_jrtj_tv_title);

//商品价格

TextView home_page_jrtj_tv_price = holder.getView(R.id.home_page_jrtj_tv_price);

//商品划价

TextView home_page_jrtj_tv_huajia = holder.getView(R.id.home_page_jrtj_tv_huajia);

home_page_jrtj_tv_huajia.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//设置划价

home_page_jrtj_img.setImageResource(datas.getImg());

home_page_jrtj_tv_title.setText(datas.getTitle());

home_page_jrtj_tv_price.setText(datas.getPrice());

home_page_jrtj_tv_huajia.setText(datas.getOld_price());

}

};

home_grid.setAdapter(re_adapter);

}

}

适配器

public abstract class CommonAdapters extends BaseAdapter {

protected Context mContext;

protected List mDatas;

protected LayoutInflater mInflater;

protected int layoutId;

private int mCurPosition = 0;

public int getCurPosition() {

return mCurPosition;

}

public void setCurPosition(int mCurPosition) {

this.mCurPosition = mCurPosition;

}

public CommonAdapters(Context context, List datas, int layoutId)

{

this.mContext = context;

mInflater = LayoutInflater.from(context);

this.mDatas = datas;

this.layoutId = layoutId;

}

public CommonAdapters(Context mContext, T xx, int item_home_list_in) {

this.mContext = mContext;

mInflater = LayoutInflater.from(mContext);

this.mDatas = (List) xx;

this.layoutId = item_home_list_in;

}

public void upDataList(List datas)

{

if (null == datas)

return;

if (mDatas != datas) {

mDatas.clear();

mDatas.addAll(datas);

}

notifyDataSetChanged();

}

@Override

public int getCount()

{

if (null == mDatas)

return 0;

return mDatas.size();

}

@Override

public T getItem(int position)

{

return mDatas.get(position);

}

@Override

public long getItemId(int position)

{

return position;

}

@Override

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

{

ViewHolders holder = ViewHolders.get(mContext, convertView, parent,

layoutId, position);

convert(holder, getItem(position));

return holder.getConvertView();

}

public abstract void convert(ViewHolders holder, T datas);

}

总结

以上所述是小编给大家介绍的Android通过实现GridView的横向滚动实现仿京东秒杀效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

android高仿京东秒杀,Android通过实现GridView的横向滚动实现仿京东秒杀效果相关推荐

  1. android gridview横向显示图片,Android使用Gridview单行横向滚动显示

    本文实例为大家分享了Android使用Gridview单行横向滚动显示的具体代码,供大家参考,具体内容如下 要想实现滚动显示,layout布局里必须要使用HorizontalScrollView,才能 ...

  2. android高分段进阶攻略(9)——ViewPager补间动画实现京东广告Banner

    最近工作较忙,勿发私信,最近看了一篇不错的博客(http://blog.csdn.net/t12x3456/article/details/13290349),使用第三方插件实现京东广告Banner, ...

  3. android 横向滚动 导航,仿今日头条横向滚动导航栏--原生js

    咳咳!先打一波小广告,在上一篇里忘记了,那啥--我的那个个人博客做好了-->(我的博客) 好嘞,言归正传,说说我们的效果. 其实就是实现横向滑动,进行选择. 原理: 鼠标按下,获取当前鼠标坐标, ...

  4. Android GridView实现横向滚动

    1:Activity布局,data_test.xml: <?xml version="1.0" encoding="utf-8"?> <Lin ...

  5. android高仿京东秒杀,Android仿京东首页秒杀倒计时

    本文实例为大家分享了Android仿京东首页秒杀倒计时的具体代码,供大家参考,具体内容如下 xml配置 android:layout_width="wrap_content" an ...

  6. android高仿京东秒杀,Android实现京东秒杀界面

    本文实例为大家分享了Android实现京东秒杀界面展示的具体代码,供大家参考,具体内容如下 效果图: 京东秒杀是两个小时一个场次,判断本机的时间进行场次定时,然后在这两个小时里面进行倒计时. Main ...

  7. android高仿京东快报(垂直循环滚动新闻栏)

    的android高仿京东快报(垂直循环滚动新闻栏) 标签: 机器人 2016年3月20日03:08 2676阅读人 评论(15)收藏举报    分类: 机器人(71)  版权声明:本文为博主原创文章, ...

  8. android高仿京东app

    仿京东app 采用组件化架构 屏幕适配方案可以较好解决多分辨率及同分辨率不同dpi适配: 全新组件化架构升级,相比之前的方案模块间更为解耦且使用更为方便: 本项目为仿京东项目,资源为抓包获取,项目框架 ...

  9. android 高仿京东首页,android 粗略的 仿京东首页 嵌套方式滚动

    简单述说 最近没有什么事情,就看到了京东的首页,感觉还不错,但是我目前也只是粗略的模仿了出来. github 地址 V2版本 仿京东首页.gif 设计思路 简单画图.png 滚动黄色的recycleV ...

最新文章

  1. java 将对象转_如何将Java对象转换为C对象?
  2. 用linux命令通常做什么,如何知道你在 Linux 里最常使用的几个命令?
  3. 5G to B核心网建设白皮书发布:2025年运营商toB市场高达6020亿美元
  4. linux 打开db文件怎么打开方式,linux服务器打开数据库文件
  5. 【Python】 Python数据类型
  6. 反序列化时恢复transient字段
  7. php 数据导出到excel文件,PHP - 如何将数据动态导出到Excel文件(.xls)?
  8. AutoJs4.1.0实战教程---终极福利Apk
  9. matlab遗传算法工具箱及应用 pdf,MATLAB遗传算法工具箱及应用(雷英杰)
  10. 国内遥感卫星资源综述
  11. 直播app服务器部署,直播视频服务器的选择!
  12. 世界期货市场竞争格局的变迁
  13. 杜比AC-3与DTS的音效对比 浅解
  14. python jupyter notebook 多个excel文档合并
  15. mybatis-plus 分页类型转换工具类
  16. 快速排序 php内存超限,数据结构与算法设计
  17. 如何让解决devcpp编译提示main' must return 'int'?
  18. 存储测试报告模板1.0
  19. 计算机硬件图标准画法,机械制图培训中剖视图的画法详解
  20. 矛与盾:用VB打造驱动级键盘记录器,能过QQ密码框(源码)

热门文章

  1. linux职位,linux现在最火的职位是什么
  2. XML Httprequest对象
  3. VB 2010 (20)多态性
  4. Neo4j图数据库从入门到精通
  5. 加工你的OO精华 工厂模式
  6. python爬取豆瓣电影排行前250获取电影名称和网络链接[静态网页]————爬虫实例(1)
  7. 英国加密货币流动性提供商获得金融监管机构批准
  8. c语言中国象棋ai算法,【中国象棋】AI算法中的棋子价值是怎么衡定的?
  9. android两个项目依赖关系图,关于android:Android组件化项目搭建遇到的问题记录
  10. Jenkins使用时间插件Date Parameter