我相信很久以前,大家在談橫向圖片輪播是時候,優先會選擇具有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="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; } } } }