我相信很久以前,大家在谈横向图片轮播是时候,优先会选择具有HorizontalScrollView效果和ViewPager来做,不过自从Google大会之后,系统为我们提供了另一个控件RecyclerView。RecyclerView是listview之后的又一利器,它可以实现高度的定制。今天就利用RecyclerView实现我们需要的相册效果。
先上一个图:
主要实现就是一个RecyclerView+RecyclerView.Adapter实现。
Activity的布局文件:
[html] view plain copy print?
<android.
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:scrollbars="none"/>
我这里是自定义的控件,主要代码:
[html] view plain copy print?
public class SimpleLinearLayout extends LinearLayout {
protected Context mContext;
protected View contentView;
protected AtomicBoolean isPreparingData;
public SimpleLinearLayout(Context context) {
super(context);
= context;
isPreparingData = new AtomicBoolean(false);
initViews();
}
public SimpleLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
= context;
isPreparingData = new AtomicBoolean(false);
initViews();
}
protected void initViews() {
}
}
主页面代码:
[html] view plain copy print?
public class SpeedHourView extends SimpleLinearLayout {
@BindView)
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);
Bu(this);
init();
}
private void init() {
initData();
initView();
initAdapter();
}
private void initData() {
String data = FileU(mContext, ";);
entity = J(data, S);
}
private void initView() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
linearLayou);
recyclerView.setLayoutManager(linearLayoutManager);
}
private void initAdapter() {
speedHourAdapter=new SpeedHourAdapter(mContext);
recyclerView.setAdapter(speedHourAdapter);
if (entity!=null&&en!=null&&en.items!=null&&en.items.size()>0){
List<S;listBeen=en.items.get(0).list;
if (listBeen!=null&&li()>0)
(listBeen);
}
(new S() {
@Override
public void onItemClick(View view, int position) {
Produc(mContext);
}
});
}
@OnClick)
public void moreClick() {
Toa("更多时速达");
}
}
adapter布局:
[html] view plain copy print?
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android=";
xmlns:ptr=";
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代码:
[html] view plain copy print?
public class SpeedHourAdapter extends RecyclerView.Adapter<SpeedHourHolder> {
private List<ListBean> specailList;
private LayoutInflater mInflater;
private Context mContext=null;
public SpeedHourAdapter(Context context) {
=context;
mInflater = Layou(context);
}
public void setList(List<ListBean> list) {
= list;
notifyDataSetChanged();
}
public OnItemClickListener mOnItemClickListener;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public void setOnItemClickListener(OnItemClickListener mOnItemClickLitener) {
= mOnItemClickLitener;
}
@Override
public SpeedHourHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mIn, parent, false);
SpeedHourHolder holder = new SpeedHourHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final SpeedHourHolder holder, final int position) {
ListBean bean = (position);
if (bean != null) {
);
Glide.with(mContext).load).error).into);
("同仁堂枸杞茶");
("¥"+Ma()*100);
}
(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mOnItemClickListener!=null){
mOnI);
}
}
});
}
@Override
public int getItemCount() {
return ();
}
}
class SpeedHourHolder extends RecyclerView.ViewHolder {
@BindView)
LinearLayout speedView;
@BindView(R.id.speed_image)
ImageView speedImage;
@BindView)
TextView speedName;
@BindView)
TextView speedPrice;
public SpeedHourHolder(View itemView) {
super(itemView);
Bu(this,itemView);
i(this);
}
}
代码中用到的实体类:
[html] view plain copy print?
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;
}
}
}
}
用到的 Json工具类也给大家
[html] view plain copy print?
public class JsonUtils {
private static Gson gson = new Gson();
public static <T> T parseJson(String response, Class<T> clazz) {
try {
return g(response, clazz);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static <T> T parseJson(String response, Type type) {
try {
return g(response, type);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String toJson(Object object) {
try {
return g(object);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static <T> List<T> jsonToList(final String jsonString, final Type type) {
try {
return g(jsonString, type);
} catch (Exception e) {
return null;
}
}
public static <T> List<T> jsonToList(final JsonArray jsonArray, final Class<T> classOfT) {
List<T>list = new ArrayList<T>();
for (int i = 0, size = j(); i <size; i++) {
li(g(i), classOfT));
}
return list;
}
public static <T> T parseJson(final JsonObject jsonObject, final Class<T> classOfT) {
try {
return g(jsonObject, classOfT);
} catch (Exception e) {
return null;
}
}
public static Map toMap(Object jsonString) {
if ())) {
return new HashMap();
}
String js = j();
Gson gson = new Gson();
Type type = new TypeToken<Map>(){
}.getType();
Map map = g(js,type);
return map;
}
}
为了方便测试,给大家提供一段数据,大家放在assert就好了,然后读到实体类就好了:
[html] view plain copy print?
{
topic: {
nextupdatetime: 50,
items: [
{
id: 1000010,
theme: "追韩剧不能少“美眸”心机",
products: 793,
users: 325,
href: "",
list: [
{
id: "50119e86-8d67-4919-a41f-be8202b62b7e",
price: 288,
pic: ";
},
{
id: "936f2615-10bb-4831-8674-9bea0b43b486",
price: 79,
pic: ";
},
{
id: "24f2be35-ba6e-4bb7-b60d-47d9c2a5bb7d",
price: 35,
pic: ";
},
{
id: "6e43adf2-18b0-40aa-a95d-84ad3cf352f7",
price: 29,
pic: ";
},
{
id: "c082d977-e06f-4f24-bf3e-7768b839170c",
price: 758,
pic: ";
},
{
id: "7737d9eb-ae85-4911-8f80-939c4d805b0a",
price: 29,
pic: ";
},
{
id: "bcc4517e-20d5-48da-ae46-708d2e8d03af",
price: 288,
pic: ";
},
{
id: "c717e1bd-7a3b-4f92-9317-84b4147ede71",
price: 78,
pic: ";
},
{
id: "139594cb-833c-4ade-b547-3a9a056b193e",
price: 69,
pic: ";
},
{
id: "4414e81f-fc3b-427c-b1ee-37b03883e0dc",
price: 88,
pic: ";
}
],
follow: false,
topictype: 106
},
{
id: 1001185,
theme: "Tommy Hilfiger男女服饰精选",
products: 553,
users: 111,
href: "",
list: [
{
id: "bd128bbf-243b-4c2e-a1d4-ea698b06142e",
price: 99,
pic: ";
},
{
id: "ea044aaf-32da-4c6e-b867-755fa780036d",
price: 229,
pic: ";
},
{
id: "5b5730a9-3343-4ef6-b78b-8b585c0b78f2",
price: 168,
pic: ";
},
{
id: "e956417a-e476-486b-be19-8fd64c6fe551",
price: 199,
pic: ";
},
{
id: "1d3e6f9b-d5f5-436c-a0d4-2e04a9e08c0d",
price: 299,
pic: ";
},
{
id: "d44ca918-3e73-4b24-a81b-f88813be057e",
price: 258,
pic: ";
},
{
id: "9f9b285a-bd18-409e-a66a-d05c717898d0",
price: 179,
pic: ";
},
{
id: "c13e8714-e1a8-48a7-b47b-7c165aa63c34",
price: 178,
pic: ";
},
{
id: "29aa1c5a-5f6e-4a80-9305-84e83cf652fc",
price: 299,
pic: ";
},
{
id: "d8120a37-aa93-4e4f-934a-4245c4f42c71",
price: 388,
pic: ";
}
],
follow: false,
topictype: 106
},
{
id: 1002054,
theme: "每个都市白领都有一只时尚腕表",
products: 310,
users: 68,
href: "",
list: [
{
id: "386ebe00-cdd0-4324-92f7-6a25179fe557",
price: 1999,
pic: ";
},
{
id: "4d93b2e6-7bf6-4878-830f-6ed22c380731",
price: 1550,
pic: ";
},
{
id: "e6b2eaef-e8ea-4ca1-b9cb-fba630e79572",
price: 398,
pic: ";
},
{
id: "0bf2584d-52eb-403e-b668-fc59eddf7b35",
price: 899,
pic: ";
},
{
id: "756ca0ed-b128-4b72-a308-f5cde1a31fbb",
price: 1788,
pic: ";
},
{
id: "c5c95dd3-0585-4ccd-b34b-b2a1b0c98915",
price: 1460,
pic: ";
},
{
id: "c5a664db-aa70-47d1-b0e6-15ad1faafcbe",
price: 1299,
pic: ";
},
{
id: "a3449aef-fdec-4344-9bca-f98b27a8a3de",
price: 2380,
pic: ";
},
{
id: "cb473f7e-3a40-4eb5-8778-043308c80ad4",
price: 299,
pic: ";
},
{
id: "2ad9f10e-ee94-49a7-95e8-b37104b86dc4",
price: 699,
pic: ";
}
],
follow: false,
topictype: 106
},
{
id: 1000691,
theme: "工薪族必备 一只FURLA手袋",
products: 369,
users: 81,
href: "",
list: [
{
id: "326d57e0-e202-45c9-a418-1f7c5c6cee37",
price: 1599,
pic: ";
},
{
id: "8c0aa3ec-0fba-4ee8-8952-74ebca2dd260",
price: 799,
pic: ";
},
{
id: "3e04f95c-4b0f-4b9f-ae44-0391ab2f1616",
price: 1880,
pic: ";
},
{
id: "05059f48-a6bb-48c1-88df-7ecabe65bd4a",
price: 799,
pic: ";
},
{
id: "0962bf25-c417-409d-b39b-daf84c9a4e9d",
price: 988,
pic: ";
},
{
id: "5c960ae1-27e3-4c94-8233-7456dc9e82b6",
price: 850,
pic: ";
},
{
id: "52184a55-e7e5-4e43-9a11-7b9fcc737136",
price: 1590,
pic: ";
},
{
id: "f11bbaf9-64eb-44ce-9bd3-07e8c8c6d1b3",
price: 2099,
pic: ";
},
{
id: "6dbd4793-2b34-43b5-8b81-7432d301bc34",
price: 2088,
pic: ";
},
{
id: "1d6f1747-a292-4821-98a0-becc67381f98",
price: 1450,
pic: ";
}
],
follow: false,
topictype: 106
},
{
id: 1000335,
theme: "你不穿,怎么知道尖头鞋有多美",
products: 262,
users: 61,
href: "",
list: [
{
id: "5dc85724-e39b-4d29-bb33-bea45910d64e",
price: 795,
pic: ";
},
{
id: "b47bcbea-e115-41be-94d7-a81d012997d9",
price: 699,
pic: ";
},
{
id: "1b27c7c2-e996-4039-b403-505425c7c661",
price: 799,
pic: ";
},
{
id: "86c2f638-f8cb-4e41-9165-b186b90de522",
price: 799,
pic: ";
},
{
id: "93251a4a-9265-47e8-a108-b790250ca5eb",
price: 599,
pic: ";
},
{
id: "522bcfb0-540e-4765-b391-6928e0a63afd",
price: 259,
pic: ";
},
{
id: "565a3590-f8e2-467a-b5ec-a7e648e3da05",
price: 399,
pic: ";
},
{
id: "f0f11042-9d05-4b69-82c2-a8c614025b7c",
price: 699,
pic: ";
},
{
id: "2099167d-39bf-4949-b5ae-fd156d49a38f",
price: 299,
pic: ";
},
{
id: "5314a6cc-1ade-4fac-80ba-c32e2d43d839",
price: 599,
pic: ";
}
],
follow: false,
topictype: 106
},
{
id: 1000981,
theme: "职场男的钱包就应该这么选",
products: 708,
users: 210,
href: "",
list: [
{
id: "0b5d3386-bb9f-46d3-bd4a-826f926c9073",
price: 499,
pic: ";
},
{
id: "d558cf02-35a6-45a9-a84d-90930f85b204",
price: 480,
pic: ";
},
{
id: "b9074e54-190e-491d-8e73-21a94884d252",
price: 499,
pic: ";
},
{
id: "96c9711e-5655-4de2-994e-4a6baa61df8b",
price: 1380,
pic: ";
},
{
id: "62de6949-d5e0-4d60-a6f0-34901ba6a7ce",
price: 1300,
pic: ";
},
{
id: "1cbc7f2e-af91-4c29-9ba8-5609ce3263a5",
price: 189,
pic: ";
},
{
id: "b7531635-edd8-4c83-93e6-196e73fcd7b5",
price: 299,
pic: ";
},
{
id: "731575af-0031-4211-8d3a-75ef19a04d51",
price: 800,
pic: ";
},
{
id: "506225eb-5135-4a8d-8f9a-dedc4d69af57",
price: 488,
pic: ";
},
{
id: "ae673d4e-59c6-4ae3-a7bd-6f4ff7ec0484",
price: 399,
pic: ";
}
],
follow: false,
topictype: 106
},
{
id: 1001855,
theme: "少女派最该入手的唇膏盘点",
products: 221,
users: 121,
href: "",
list: [
{
id: "dd59aefa-0619-40a9-8f44-33b7b980e7e7",
price: 36,
pic: ";
},
{
id: "85b8ce98-b7e6-4323-83b7-ba891f0a0da3",
price: 249,
pic: ";
},
{
id: "9dfe69a1-fc6d-4652-941b-3e1140ef50ec",
price: 199,
pic: ";
},
{
id: "db7ea83c-90a2-4569-a063-2580e4bbe586",
price: 149,
pic: ";
},
{
id: "7e702512-bc7c-49a7-894c-7f79e08635b6",
price: 209,
pic: ";
},
{
id: "8aa440ee-b99c-4047-ad6b-669b0c864266",
price: 85,
pic: ";
},
{
id: "4b845bc6-dde2-4d83-b3cb-5c2aba1629ee",
price: 68,
pic: ";
},
{
id: "43c635e7-01e8-4dfc-be38-5d11d9bd67bd",
price: 195,
pic: ";
},
{
id: "3689fe6f-fc0d-4a4a-a5b1-0553038e154f",
price: 218,
pic: ";
},
{
id: "9a1aa6ec-fcab-4206-bce3-75b0eed4a331",
price: 199,
pic: ";
}
],
follow: false,
topictype: 106
},
{
id: 1002012,
theme: "Kate spade的甜心少女风",
products: 1125,
users: 157,
href: "",
list: [
{
id: "a92b1158-d8cc-4bfc-bba1-8e269a597fc2",
price: 1380,
pic: ";
},
{
id: "5a388eb3-e696-4c78-9b5a-4a6cefb54e2f",
price: 1088,
pic: ";
},
{
id: "c788ead9-44b1-47da-86db-f520e871a959",
price: 875,
pic: ";
},
{
id: "2a8d3aac-0991-41db-bfcc-c89026f92756",
price: 760,
pic: ";
},
{
id: "f13dd52f-b3ae-46cb-b4fc-c3eaeaac6a7e",
price: 498,
pic: ";
},
{
id: "bfa91c51-57a2-46c4-a6de-a420852be9ca",
price: 429,
pic: ";
},
{
id: "16462c30-454d-4566-a40c-1c9af6d066eb",
price: 560,
pic: ";
},
{
id: "6a9d0e09-6d7f-42c1-bfd7-50adb12473a2",
price: 1249,
pic: ";
},
{
id: "484a8967-c843-4d34-8876-4afca275cd58",
price: 1250,
pic: ";
},
{
id: "5e32fbce-be90-4c55-b3ba-2bd05de10f3a",
price: 998,
pic: ";
}
],
follow: false,
topictype: 106
},
{
id: 1000657,
theme: "施华洛世奇 奢华之最",
products: 1284,
users: 147,
href: "",
list: [
{
id: "68775149-2996-40d3-8164-fe51ebaeb247",
price: 390,
pic: ";
},
{
id: "f4ea3d67-957b-44b1-824a-aca71b191975",
price: 290,
pic: ";
},
{
id: "608f9cea-865a-403f-a064-b7020d8b9151",
price: 500,
pic: ";
},
{
id: "5f74f137-6b06-412f-92b0-6ccfabc5e4ff",
price: 990,
pic: ";
},
{
id: "207db2b4-d7b7-40af-b769-a51384030781",
price: 358,
pic: ";
},
{
id: "61e75f97-934f-480a-a253-c4401a692c2f",
price: 799,
pic: ";
},
{
id: "f60f6587-e557-4261-b58f-31f5719df5ba",
price: 580,
pic: ";
},
{
id: "630d7893-6a9e-4fc8-8a93-098fce056920",
price: 659,
pic: ";
},
{
id: "1e52e01f-335e-440b-99d6-7cd79ba6d1ae",
price: 959,
pic: ";
},
{
id: "3b5663c3-c694-4a68-a02f-c6bda164427a",
price: 515,
pic: ";
}
],
follow: false,
topictype: 106
},
{
id: 1002418,
theme: "爆款再见,饰品我只要独一无二",
products: 113,
users: 33,
href: "",
list: [
{
id: "dfff8751-775c-437f-8fa7-5aec934a5ef0",
price: 89,
pic: ";
},
{
id: "ad098f8a-5fcd-42ab-afdd-3ff580a1ac0b",
price: 350,
pic: ";
},
{
id: "bda09ca7-56cb-4458-b6d1-9f988c9f326d",
price: 450,
pic: ";
},
{
id: "23fc3728-a706-42ce-9e5f-7ce742474a73",
price: 680,
pic: ";
},
{
id: "f8ea5393-7972-40b0-90b2-32b22378cb35",
price: 290,
pic: ";
},
{
id: "f249ae53-750e-4237-a53d-7deb1c5102e5",
price: 468,
pic: ";
},
{
id: "460b5d5c-6b6e-4c03-aefa-a56c7a2df8d4",
price: 3100,
pic: ";
},
{
id: "60040ecd-23f4-44e7-a3e9-81e36327a2ca",
price: 129,
pic: ";
},
{
id: "83f60817-e22c-42e9-94cf-331e8ffc1ce3",
price: 3350,
pic: ";
},
{
id: "0e816cf1-4c0d-4ced-a356-45620fbda1b8",
price: 199,
pic: ";
}
],
follow: false,
topictype: 106
},
{
id: 1003396,
theme: "清新淑女的露肩裙美到没朋友",
products: 50,
users: 34,
href: "",
list: [
{
id: "548c381a-2be7-4a6c-9344-b898f18e331e",
price: 650,
pic: ";
},
{
id: "8ca2ca1a-3f7b-4826-9b17-a339f4c4cbf3",
price: 560,
pic: ";
},
{
id: "aed17646-dfe8-4969-bbd4-cfaab80000db",
price: 1899,
pic: ";
},
{
id: "d77c9628-84aa-4e51-861f-f3674d195bd3",
price: 200,
pic: ";
},
{
id: "dee59eeb-d82c-4ff4-838c-c492b3d001ab",
price: 218,
pic: ";
},
{
id: "994f9376-8e3b-4e27-b393-d4ca05fa046a",
price: 229,
pic: ";
},
{
id: "af6efa07-6853-4d6c-acfc-a3043eb24988",
price: 399,
pic: ";
},
{
id: "5a721e93-2ea9-4155-8678-f5c09e518514",
price: 369,
pic: ";
},
{
id: "a878b978-31c0-4284-a6de-f63aafd61312",
price: 1999,
pic: ";
},
{
id: "2215cbde-97a6-46d5-aea2-9440e115f251",
price: 468,
pic: ";
}
],
follow: false,
topictype: 106
},
{
id: 1000302,
theme: "Tod's:无法抗拒的经典舒适",
products: 179,
users: 30,
href: "",
list: [
{
id: "f6c50028-9211-4080-ae58-3beed3728e50",
price: 1850,
pic: ";
},
{
id: "49c37f83-2bdd-473c-b712-610fa642f849",
price: 1800,
pic: ";
},
{
id: "a92f93d6-2ce6-40b2-87cf-19d34cbb03d0",
price: 1250,
pic: ";
},
{
id: "908cc4d0-1e9a-475b-92a5-2da58a184a52",
price: 1980,
pic: ";
},
{
id: "cd3accbc-9c8e-44e8-bee2-0fd2062e54a2",
price: 2580,
pic: ";
},
{
id: "7ace0a0f-2725-4a46-af60-036cb5ccbe83",
price: 1640,
pic: ";
},
{
id: "d8c8fae1-a169-4530-864c-6ba28222d3e7",
price: 1880,
pic: ";
},
{
id: "c475412f-ae29-4690-b46b-c4407db8916b",
price: 1700,
pic: ";
},
{
id: "f5d34f68-d566-4cf2-aa8e-142ef058fd71",
price: 1499,
pic: ";
},
{
id: "cf1a8142-91f2-4b2c-bb86-61ae0ede2b73",
price: 3200,
pic: ";
}
],
follow: false,
topictype: 106
}
]
}
}