Android RecyclerView实现横向滚动
2017-08-15 13:44
666 查看
我相信很久以前,大家在谈横向图片轮播是时候,优先会选择具有HorizontalScrollView效果和ViewPager来做,不过自从Google大会之后,系统为我们提供了另一个控件RecyclerView。RecyclerView是listview之后的又一利器,它可以实现高度的定制。今天就利用RecyclerView实现我们需要的相册效果。
先上一个图:
主要实现就是一个RecyclerView+RecyclerView.Adapter实现。
Activity的布局文件:
我这里是自定义的控件,主要代码:
主页面代码:
adapter布局:
adapter代码:
代码中用到的实体类:
先上一个图:
主要实现就是一个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()>0){ List<SpeedHourEntity.TopicBean.ItemsBean.ListBean> listBeen=entity.topic.items.get(0).list; if (listBeen!=null&&listBeen.size()>0) 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="1"/> <TextView android:id="@+id/speed_price" style="@style/style_c8_s14" android:layout_marginTop="5dp" android:text="¥30.0" android:maxLength="6" android:maxLines="1"/> </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()*100); } 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 实现横向滚动效果
- 安卓的那些事儿-android之RecyclerView的使用,实现列表横向滚动
- Android RecyclerView自定义横向分割线实现时间轴
- Android Touch事件总结 二 (双指实现RecyclerView的快速滚动)
- Android使用RecyclerView实现水平滚动控件
- RecyclerView 实现横向滚动与瀑布流布局
- Android 如何实现RecyclerView横向滑动
- Android中RecyclerView实现分页滚动的方法详解
- Android 在滚动列表中实现视频的播放(ListView & RecyclerView)
- Android开发之实现水平滚动效果—RecyclerView
- 【Android实战】RecyclerView实现水平可滚动gridview
- RecyclerView实现横向滚动
- Android代码实现AdapterViews和RecyclerView无限滚动
- RecyclerView实现横向滚动
- RecyclerView实现横向分页滚动效果,欢迎大家前来交流
- (更新版)Android VideoPlayer 在滚动列表实现item视频播放(ListView控件和RecyclerView)
- RecyclerView 实现横向滚动效果
- Android在滚动列表中实现视频的播放(ListView & RecyclerView)
- Android简易实战教程--第四十六话《RecyclerView竖向和横向滚动》
- Android TV横向滚动网格布局——RecyclerView的使用