1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Android RecyclerView实现横向滚动

Android RecyclerView实现横向滚动

时间:2021-09-11 01:24:20

相关推荐

Android RecyclerView实现横向滚动

我相信很久以前,大家在谈横向图片轮播是时候,优先会选择具有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()>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="/apk/res/android" xmlns:ptr="/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; } } } }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。