我相信很久以前,大家在谈横向图片轮播是时候,优先会选择具有HorizontalScrollView效果和ViewPager来做,不过自从Google大会之后,系统为我们提供了另一个控件RecyclerView。RecyclerView是listview之后的又一利器,它可以实现高度的定制。今天就利用RecyclerView实现我们需要的相册效果。

先上一个图:

主要实现就是一个RecyclerView+RecyclerView.Adapter实现。

Activity的布局文件:

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:scrollbars="none" />

我这里是自定义的控件,主要代码:

public class SimpleLinearLayout extends LinearLayout {  

    protected Context mContext;
protected View contentView;
protected AtomicBoolean isPreparingData; public SimpleLinearLayout(Context context) {
super(context);
this.mContext = context;
isPreparingData = new AtomicBoolean(false);
initViews();
} public SimpleLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
isPreparingData = new AtomicBoolean(false);
initViews();
}
protected void initViews() { }
}

主页面代码:

public class SpeedHourView extends SimpleLinearLayout {  

    @BindView(R.id.recycler_view)
RecyclerView recyclerView; private SpeedHourAdapter speedHourAdapter=null;
private SpeedHourEntity entity=null; public SpeedHourView(Context context) {
this(context, null);
} public SpeedHourView(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
protected void initViews() {
contentView = inflate(mContext, R.layout.layout_speed_per_hour, this);
ButterKnife.bind(this);
init();
} private void init() {
initData();
initView();
initAdapter();
} private void initData() {
String data = FileUtils.readAssert(mContext, "speenhour.txt");
entity = JsonUtils.parseJson(data, SpeedHourEntity.class);
} private void initView() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(linearLayoutManager); } private void initAdapter() {
speedHourAdapter=new SpeedHourAdapter(mContext);
recyclerView.setAdapter(speedHourAdapter);
if (entity!=null&&entity.topic!=null&&entity.topic.items!=null&&entity.topic.items.size()>){
List<SpeedHourEntity.TopicBean.ItemsBean.ListBean> listBeen=entity.topic.items.get().list;
if (listBeen!=null&&listBeen.size()>)
speedHourAdapter.setList(listBeen);
} speedHourAdapter.setOnItemClickListener(new SpeedHourAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
ProductDetailsActivity.open(mContext);
}
});
} @OnClick(R.id.more_view)
public void moreClick() {
ToastUtils.showToast("更多时速达");
}
}

adapter布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/speed_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:gravity="center"> <ImageView
android:id="@+id/speed_image"
android:layout_width="85dp"
android:layout_height="85dp"
android:scaleType="fitXY"
/> <TextView
android:id="@+id/speed_name"
style="@style/style_c6_s14"
android:layout_marginTop="5dp"
android:text="蜂蜜柚子茶"
android:maxLines=""/> <TextView
android:id="@+id/speed_price"
style="@style/style_c8_s14"
android:layout_marginTop="5dp"
android:text="¥30.0"
android:maxLength=""
android:maxLines=""/>
</LinearLayout>

adapter代码:

public class SpeedHourAdapter extends RecyclerView.Adapter<SpeedHourHolder> {  

    private List<ListBean> specailList;
private LayoutInflater mInflater;
private Context mContext=null; public SpeedHourAdapter(Context context) {
this.mContext=context;
mInflater = LayoutInflater.from(context);
} public void setList(List<ListBean> list) {
this.specailList = list;
notifyDataSetChanged();
} public OnItemClickListener mOnItemClickListener; public interface OnItemClickListener {
void onItemClick(View view, int position);
} public void setOnItemClickListener(OnItemClickListener mOnItemClickLitener) {
this.mOnItemClickListener = mOnItemClickLitener;
} @Override
public SpeedHourHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.item_speedhour_layout, parent, false); SpeedHourHolder holder = new SpeedHourHolder(view);
return holder;
} @Override
public void onBindViewHolder(final SpeedHourHolder holder, final int position) {
ListBean bean = specailList.get(position);
if (bean != null) {
holder.speedImage.setScaleType(ImageView.ScaleType.FIT_XY);
Glide.with(mContext).load(bean.pic).error(R.drawable.welfare_default_icon).into(holder.speedImage);
holder.speedName.setText("同仁堂枸杞茶");
holder.speedPrice.setText("¥"+Math.random()*);
} holder.speedView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mOnItemClickListener!=null){
mOnItemClickListener.onItemClick(holder.speedView,position);
}
}
});
} @Override
public int getItemCount() {
return specailList.size();
}
} class SpeedHourHolder extends RecyclerView.ViewHolder { @BindView(R.id.speed_view)
LinearLayout speedView;
@BindView(R.id.speed_image)
ImageView speedImage;
@BindView(R.id.speed_name)
TextView speedName;
@BindView(R.id.speed_price)
TextView speedPrice; public SpeedHourHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
itemView.setTag(this);
}

代码中用到的实体类:

public class SpeedHourEntity {  

    public TopicBean topic;  

    public static class TopicBean {
public long nextupdatetime;
public List<ItemsBean> items; public static class ItemsBean {
public int id;
public String theme;
public int products;
public int users;
public String href;
public boolean follow;
public int topictype; public List<ListBean> list; public static class ListBean {
public String id;
public int price;
public String pic;
}
}
}
}

Android RecyclerView实现横向滚动的更多相关文章

  1. RecyclerView 实现横向滚动效果

    我相信很久以前,大家在谈横向图片轮播是时候,优先会选择具有HorizontalScrollView效果和ViewPager来做,不过自从Google大会之后,系统为我们提供了另一个控件Recycler ...

  2. Android简易实战教程--第四十六话《RecyclerView竖向和横向滚动》

    Android5.X后,引入了RecyclerView,这个控件使用起来非常的方便,不但可以完成listView的效果,而且还可以实现ListView无法实现的效果.当然,在新能方便也做了大大的提高. ...

  3. 【Android】10.5 滚动视图(RecyclerView)

    分类:C#.Android.VS2015: 创建日期:2016-02-19 一.简介 滚动视图(RecyclerView)的用法与布局控件的用法类似,唯一不同的是,所有布局控件中都可以包含多个组件,而 ...

  4. Android RecyclerView 滚动到中间位置

    最近看到QQ音乐的歌词每次滑动后都可以滚回到中间位置.觉得甚是神奇,打开开发者模式显示布局,发现歌词部分不是采用 android 控件的写的,应该是前端写的.于是,我想,能不能用 recyclerVi ...

  5. Android 横向列表GridView 实现横向滚动

    Android 横向列表实现,可左右滑动,如下图 1.主界面布局代码:activity_main.xml a.包裹HorizontalScrollView控件是GirdView横向滚动的基本条件b.G ...

  6. Android TextView 横向滚动(跑马灯效果)

    Android TextView 中当文字比較多时希望它横向滚动显示,以下是一种亲測可行的方法. 效果图: 1.自己定义TextView,重写isFocused()方法返回true,让自己定义Text ...

  7. Android RecyclerView 使用完全解析 体验艺术般的控件

    概述 RecyclerView出现已经有一段时间了,相信大家肯定不陌生了,大家可以通过导入support-v7对其进行使用. 据官方的介绍,该控件用于在有限的窗口中展示大量数据集,其实这样功能的控件我 ...

  8. Android RecyclerView 使用完全解析

    概述 RecyclerView出现已经有一段时间了,相信大家肯定不陌生了,大家可以通过导入support-v7对其进行使用. 据官方的介绍,该控件用于在有限的窗口中展示大量数据集,其实这样功能的控件我 ...

  9. 【转载】Android RecyclerView 使用完全解析 体验艺术般的控件

    崇拜下鸿洋大神,原文地址:http://blog.csdn.net/lmj623565791/article/details/45059587 概述 RecyclerView出现已经有一段时间了,相信 ...

随机推荐

  1. centos下mysql多实例安装3306、3307实例(2014-10-15)

    背景说明       mysql的安装方法有多种,如二进制安装.源代码编译安装.yum安装等.yum安装仅仅能安装mysql 5.1 版本号:源代码安装编译的过程比較长.若没有对源代码进行改动且要求使 ...

  2. HDU 5353 Average

    Problem Description There are n soda sitting around a round table. soda are numbered from 1 to n and ...

  3. Qt Quick 简单介绍

    Qt Quick 是 Qt 提供的一种高级用户界面技术.使用它可轻松地为移动和嵌入式设备创建流畅的用户界面. 在 Android 设备上, Qt Quick 应用默认使用 OpenGL ES ,渲染效 ...

  4. poj--2549--Sumsets(二分查找)

    Sumsets Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  5. Django之ORM数据库增删改查

    总结:ORM的 查.增.删.改 - 查 - client - 有一个展示页面(xxx_show.html) - 这一个页面一输入执行后,get请求向server端发送 - 这个展示页面有添加按钮.删除 ...

  6. firewall 实现数据的端口转发

    端口转发:firewall-cmd --add-port=80/tcp firewall-cmd --add-port=10050/tcp firewall-cmd --add-forward-por ...

  7. 3ds Max绘制青花瓷茶壶

    1.在桌面找到3DMAX软件,左键双击,启动程序: 2.在命令案板中,找到几何体,茶壶,在顶视图绘制一个茶壶: 3.在百度图片中搜索,查找“青花瓷”,找到一个自己喜欢的精美图案,截图保存备用: 4.在 ...

  8. PKI和加密,散列算法

    Day 11-PKI和加密,散列算法 PKI(Public Key Infrastructure公钥基础设施) 1 PKI(Public Key Infrastructure公钥基础设施)回顾学习 - ...

  9. 捕捉soap的xml形式

    下面是我以前对Php的soap接口进行抓包分析出的结果,这个分析在当服务端或者客户端的Php没有安装soap模块时,可以使用构建xml的方式实现相同的功能 服务端: $data = $HTTP_RAW ...

  10. BZOJ 5104 Fib数列(二次剩余+BSGS)

    斐波那契数列的通项: \[\frac{1}{\sqrt{5}}((\frac{1+\sqrt{5}}{2})-(\frac{1-\sqrt{5}}{2}))\] 设T=\(\sqrt{5}*N\),\ ...