您的位置:首页 > 其它

RecyclerView和购物车的简单结合使用

2017-11-20 19:44 381 查看
首先先导入依赖:    
compile'com.squareup.okhttp3:okhttp:3.9.0'
compile'com.squareup.okhttp3:logging-interceptor:3.9.0'
compile'com.google.code.gson:gson:2.8.2'
compile'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile'org.greenrobot:eventbus:3.1.1'
然后分类建包,这个就不用在这里展示了,
下来就先写网络请求OkHttpUtils
importokhttp3.Callback;importokhttp3.OkHttpClient;importokhttp3.Request;importokhttp3.logging.HttpLoggingInterceptor;publicclassHttpUtils{privatestaticvolatileHttpUtilshttpUtils;privateOkHttpClientclient;privateHttpUtils(){HttpLoggingInterceptorlogging=newHttpLoggingInterceptor();logging.setLevel(HttpLoggingInterceptor.Level.BODY);client=newOkHttpClient.Builder().addInterceptor(logging).build();}publicstaticHttpUtilsgetHttpUtils(){if(httpUtils==null){synchronized(HttpUtils.class){if(httpUtils==null){httpUtils=newHttpUtils();}}}returnhttpUtils;}/***GET请求**@paramurl*@paramcallback*/publicvoiddoGet(Stringurl,Callbackcallback){Requestrequest=newRequest.Builder().url(url).build();client.newCall(request).enqueue(callback);}}
然后在这里建一个接口来实现model层的方法。
publicinterfaceOnNetListener<T>{publicvoidonSuccess(Tt);//这是成功的方法publicvoidonFailure(Exceptione);//这是失败的。}
下面写一个APP类的方法。这个方法是ImageLoader的方法,用来展示图片
publicclassMyAppextendsApplication{@OverridepublicvoidonCreate(){super.onCreate();ImageLoaderConfigurationconfiguration=newImageLoaderConfiguration.Builder(this).build();ImageLoader.getInstance().init(configuration);}}
上面的一些必须的代码就写完了,接下来写model层的接口
publicinterfaceIMainModel{publicvoidgetGoods(OnNetListener<GoodsBean>onNetListener);}
然后创建一个类来继承这个接口
importandroid.os.Handler;importandroid.os.Looper;importcom.bwie.test.a09ashopcar.bean.GoodsBean;importcom.bwie.test.a09ashopcar.net.Api;importcom.bwie.test.a09ashopcar.net.HttpUtils;importcom.bwie.test.a09ashopcar.net.OnNetListener;importcom.google.gson.Gson;importjava.io.IOException;importokhttp3.Call;importokhttp3.Callback;importokhttp3.Response;publicclassMainModelimplementsIMainModel{privateHandlerhandler=newHandler(Looper.getMainLooper());@OverridepublicvoidgetGoods(finalOnNetListener<Goods1fff8Bean>onNetListener){HttpUtils.getHttpUtils().doGet(Api.url,newCallback(){@OverridepublicvoidonFailure(Callcall,IOExceptione){}@OverridepublicvoidonResponse(Callcall,Responseresponse)throwsIOException{Stringstring=response.body().string();finalGoodsBeangoodsBean=newGson().fromJson(string,GoodsBean.class);handler.post(newRunnable(){@Overridepublicvoidrun(){onNetListener.onSuccess(goodsBean);}});}});}}
model层写完了,接下来就该写presenter的数据了
publicclassMainPresenter{privateIMainModeliMainModel;privateIShopActivityiMainActivity;publicMainPresenter(IShopActivityiMainActivity){this.iMainActivity=iMainActivity;iMainModel=newMainModel();}publicvoidgetGoods(){iMainModel.getGoods(newOnNetListener<GoodsBean>(){@OverridepublicvoidonSuccess(GoodsBeangoodsBean){List<GoodsBean.DataBean>groupList=goodsBean.getData();List<List<GoodsBean.DataBean.DatasBean>>childList=newArrayList<List<GoodsBean.DataBean.DatasBean>>();for(inti=0;i<groupList.size();i++){List<GoodsBean.DataBean.DatasBean>datas=groupList.get(i).getDatas();childList.add(datas);}iMainActivity.showList(groupList,childList);}@OverridepublicvoidonFailure(Exceptione){}});}}
写完这些就该写布局了
购物车它是二级的,所以布局也比较复杂
下面这个是主要的布局
main.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.bwie.test.a09ashopcar.view.ShopActivity"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:background="#C0C1A0FA"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="购物车"android:textSize="22sp"android:layout_centerVertical="true"android:layout_centerHorizontal="true"android:textColor="#ff000036"/></RelativeLayout><ExpandableListViewandroid:id="@+id/elv"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"></ExpandableListView><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:layout_alignParentBottom="true"android:background="@android:color/white"android:gravity="center_vertical"><CheckBoxandroid:id="@+id/quan_ck"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:focusable="false"/><TextViewandroid:layout_width="wrap_content"android:layout_height="50dp"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:layout_toRightOf="@+id/quan_ck"android:gravity="center_vertical"android:text="全选"android:textSize="20sp"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:textSize="18sp"android:text="合计:"/><TextViewandroid:id="@+id/tv_price"android:layout_width="wrap_content"android:layout_height="50dp"android:layout_marginLeft="10dp"android:paddingRight="10dp"android:text="0"android:textColor="@android:color/holo_red_light"/><TextViewandroid:id="@+id/tv_num"android:layout_width="wrap_content"android:layout_height="50dp"android:background="#ff3306"android:gravity="center"android:padding="10dp"android:text="结算(0)"android:textColor="@android:color/white"/></LinearLayout></RelativeLayout></LinearLayout>
下来写一级目录的布局吧。Group.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="100dp"android:orientation="horizontal"android:gravity="center_vertical"><CheckBoxandroid:id="@+id/cb_group"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="30dp"android:layout_marginLeft="20dp"android:layout_marginTop="30dp"android:focusable="false"/><TextViewandroid:id="@+id/tv_sign"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="标记"/><TextViewandroid:id="@+id/tv_number"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="12345678"/></LinearLayout>
下来是二级目录的布局child.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/darker_gray"android:gravity="center_vertical"android:orientation="horizontal"><CheckBoxandroid:id="@+id/cb_child"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="30dp"android:layout_marginLeft="40dp"android:layout_marginTop="30dp"android:focusable="false"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/tv_tel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="iphone6"/><TextViewandroid:id="@+id/tv_content"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="什么手机"/><TextViewandroid:id="@+id/tv_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="2016-12-10"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="vertical"><TextViewandroid:id="@+id/tv_pri"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="¥3000.00"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"><ImageViewandroid:id="@+id/iv_del"android:layout_width="20dp"android:layout_height="20dp"android:src="@drawable/shopcart_minus_grey"/><TextViewandroid:id="@+id/tv_num"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:background="@drawable/shopcart_add_btn"android:paddingBottom="2dp"android:paddingLeft="20dp"android:paddingRight="20dp"android:paddingTop="2dp"android:text="1"/><ImageViewandroid:id="@+id/iv_add"android:layout_width="20dp"android:layout_height="20dp"android:layout_marginLeft="5dp"android:src="@drawable/shopcart_add_red"/></LinearLayout></LinearLayout><TextViewandroid:id="@+id/tv_del"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="删除"/></LinearLayout>
接下来我们改写View层的接口
publicinterfaceIShopActivity{publicvoidshowList(List<GoodsBean.DataBean>groupList,List<List<GoodsBean.DataBean.DatasBean>>childList);}
然后是View的类
publicclassShopActivityextendsAppCompatActivityimplementsIShopActivity{privateExpandableListViewmElv;privateCheckBoxmQuanCk;/***0*/privateTextViewmTvPrice;/***结算(0)*/privateTextViewmTvNum;privateLinearLayoutmActivityMain;privateShopAdapteradapter;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_shop);EventBus.getDefault().register(this);initView();newMainPresenter(this).getGoods();mQuanCk.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){adapter.changeAllListCbState(mQuanCk.isChecked());}});}privatevoidinitView(){mElv=(ExpandableListView)findViewById(R.id.elv);mQuanCk=(CheckBox)findViewById(R.id.quan_ck);mTvPrice=(TextView)findViewById(R.id.tv_price);mTvNum=(TextView)findViewById(R.id.tv_num);mActivityMain=(LinearLayout)findViewById(R.id.activity_main);}@OverridepublicvoidshowList(List<GoodsBean.DataBean>groupList,List<List<GoodsBean.DataBean.DatasBean>>childList){adapter=newShopAdapter(this,groupList,childList);mElv.setAdapter(adapter);//去掉二级列表的小箭头mElv.setGroupIndicator(null);//默认让其全部展开for(inti=0;i<groupList.size();i++){mElv.expandGroup(i);}}@SubscribepublicvoidonMessageEvent(MessageEventevent){mQuanCk.setChecked(event.isChecked());}@SubscribepublicvoidonMessageEvent(PriceAndCountEventevent){mTvNum.setText("结算("+event.getCount()+")");mTvPrice.setText(event.getPrice()+"");}}
在这里我们用到了EventBus这个控件,所以我们这里还有这样两个类:
publicclassMessageEvent{privatebooleanchecked;publicbooleanisChecked(){returnchecked;}publicvoidsetChecked(booleanchecked){this.checked=checked;}}
publicclassPriceAndCountEvent{privateintprice;privateintcount;publicintgetPrice(){returnprice;}publicvoidsetPrice(intprice){this.price=price;}publicintgetCount(){returncount;}publicvoidsetCount(intcount){this.count=count;}}
接下来我们来写最重要的购物车的适配器
publicclassShopAdapterextendsBaseExpandableListAdapter{privateContextcontext;privateList<GoodsBean.DataBean>groupList;privateList<List<GoodsBean.DataBean.DatasBean>>childList;privatefinalLayoutInflaterinflater;publicShopAdapter(Contextcontext,List<GoodsBean.DataBean>groupList,List<List<GoodsBean.DataBean.DatasBean>>childList){this.context=context;this.groupList=groupList;this.childList=childList;inflater=LayoutInflater.from(context);}@OverridepublicintgetGroupCount(){returngroupList.size();}@OverridepublicintgetChildrenCount(inti){returnchildList.get(i).size();}@OverridepublicObjectgetGroup(inti){returngroupList.get(i);}@OverridepublicObjectgetChild(inti,inti1){returnchildList.get(i).get(i1);}@OverridepubliclonggetGroupId(inti){returni;}@OverridepubliclonggetChildId(inti,inti1){returni1;}@OverridepublicbooleanhasStableIds(){returnfalse;}@OverridepublicViewgetGroupView(finalinti,booleanb,ViewconvertView,ViewGroupviewGroup){Viewview;finalGroupViewHoldergholder;if(convertView==null){gholder=newGroupViewHolder();view=inflater.inflate(R.layout.item_group,null);gholder.cb_group=view.findViewById(R.id.cb_group);gholder.tv_number=view.findViewById(R.id.tv_number);view.setTag(gholder);}else{view=convertView;gholder=(GroupViewHolder)view.getTag();}finalGoodsBean.DataBeangroupBean=groupList.get(i);gholder.cb_group.setChecked(groupBean.isChecked());gholder.tv_number.setText(groupBean.getTitle());//一级checkboxgholder.cb_group.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){groupBean.setChecked(gholder.cb_group.isChecked());changeChildCbState(i,gholder.cb_group.isChecked());EventBus.getDefault().post(compute());changeAllCbState(isAllGroupCbSelected());notifyDataSetChanged();}});returnview;}@OverridepublicViewgetChildView(finalinti,finalinti1,booleanb,finalViewconvertView,ViewGroupviewGroup){Viewview;finalChildViewHoldercholder;if(convertView==null){cholder=newChildViewHolder();view=inflater.inflate(R.layout.item_child,null);cholder.cb_child=view.findViewById(R.id.cb_child);cholder.tv_tel=view.findViewById(R.id.tv_tel);cholder.tv_content=view.findViewById(R.id.tv_content);cholder.tv_time=view.findViewById(R.id.tv_time);cholder.tv_pri=view.findViewById(R.id.tv_pri);cholder.tv_del=view.findViewById(R.id.tv_del);cholder.iv_add=view.findViewById(R.id.iv_add);cholder.iv_del=view.findViewById(R.id.iv_del);cholder.tv_num=view.findViewById(R.id.tv_num);view.setTag(cholder);}else{view=convertView;cholder=(ChildViewHolder)view.getTag();}finalGoodsBean.DataBean.DatasBeanchildBean=childList.get(i).get(i1);cholder.cb_child.setChecked(childBean.isChecked());cholder.tv_tel.setText(childBean.getType_name());cholder.tv_content.setText(childBean.getMsg());cholder.tv_time.setText(childBean.getAdd_time());cholder.tv_pri.setText(childBean.getPrice()+"");cholder.tv_num.setText(childBean.getNum()+"");//二级checkbox//给holder.cbChild设置点击事件cholder.cb_child.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){//设置该条目对象里的checked属性值childBean.setChecked(cholder.cb_child.isChecked());PriceAndCountEventpriceAndCountEvent=compute();EventBus.getDefault().post(priceAndCountEvent);if(cholder.cb_child.isChecked()){//当前checkbox是选中状态if(isAllChildCbSelected(i)){changGroupCbState(i,true);changeAllCbState(isAllGroupCbSelected());}}else{changGroupCbState(i,false);changeAllCbState(isAllGroupCbSelected());}notifyDataSetChanged();}});//加号cholder.iv_add.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){intnum=childBean.getNum();cholder.tv_num.setText(++num+"");childBean.setNum(num);if(cholder.cb_child.isChecked()){PriceAndCountEventpriceAndCountEvent=compute();EventBus.getDefault().post(priceAndCountEvent);}}});//减号cholder.iv_del.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){intnum=childBean.getNum();if(num==1){Toast.makeText(context,"最少留一个呗!",Toast.LENGTH_SHORT).show();return;}cholder.tv_num.setText(--num+"");childBean.setNum(num);if(cholder.cb_child.isChecked()){PriceAndCountEventpriceAndCountEvent=compute();EventBus.getDefault().post(priceAndCountEvent);}}});//删除cholder.tv_del.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){List<GoodsBean.DataBean.DatasBean>datasBeen=childList.get(i);GoodsBean.DataBean.DatasBeanremove=datasBeen.remove(i1);if(datasBeen.size()==0){childList.remove(i);groupList.remove(i);}EventBus.getDefault().post(compute());notifyDataSetChanged();}});returnview;}@OverridepublicbooleanisChildSelectable(inti,inti1){returnfalse;}classGroupViewHolder{CheckBoxcb_group;TextViewtv_number;}classChildViewHolder{CheckBoxcb_child;TextViewtv_tel;TextViewtv_content;TextViewtv_time;TextViewtv_pri;TextViewtv_del;ImageViewiv_del;ImageViewiv_add;TextViewtv_num;}/***改变全选的状态**@paramflag*/privatevoidchangeAllCbState(booleanflag){MessageEventmessageEvent=newMessageEvent();messageEvent.setChecked(flag);EventBus.getDefault().post(messageEvent);}/***改变一级列表checkbox状态**@paramgroupPosition*/privatevoidchangGroupCbState(intgroupPosition,booleanflag){GoodsBean.DataBeandataBean=groupList.get(groupPosition);dataBean.setChecked(flag);}/***改变二级列表checkbox状态**@paramgroupPosition*@paramflag*/privatevoidchangeChildCbState(intgroupPosition,booleanflag){List<GoodsBean.DataBean.DatasBean>datasBeen=childList.get(groupPosition);for(inti=0;i<datasBeen.size();i++){GoodsBean.DataBean.DatasBeandatasBean=datasBeen.get(i);datasBean.setChecked(flag);}}/***判断一级列表是否全部选中**@return*/privatebooleanisAllGroupCbSelected(){for(inti=0;i<groupList.size();i++){GoodsBean.DataBeandataBean=groupList.get(i);if(!dataBean.isChecked()){returnfalse;}}returntrue;}/***判断二级列表是否全部选中**@paramgroupPosition*@return*/privatebooleanisAllChildCbSelected(intgroupPosition){List<GoodsBean.DataBean.DatasBean>datasBeen=childList.get(groupPosition);for(inti=0;i<datasBeen.size();i++){GoodsBean.DataBean.DatasBeandatasBean=datasBeen.get(i);if(!datasBean.isChecked()){returnfalse;}}returntrue;}/***计算列表中,选中的钱和数量*/privatePriceAndCountEventcompute(){intcount=0;intprice=0;for(inti=0;i<childList.size();i++){List<GoodsBean.DataBean.DatasBean>datasBeen=childList.get(i);for(intj=0;j<datasBeen.size();j++){GoodsBean.DataBean.DatasBeandatasBean=datasBeen.get(j);if(datasBean.isChecked()){price+=datasBean.getNum()*datasBean.getPrice();count+=datasBean.getNum();}}}PriceAndCountEventpriceAndCountEvent=newPriceAndCountEvent();priceAndCountEvent.setCount(count);priceAndCountEvent.setPrice(price);returnpriceAndCountEvent;}/***设置全选、反选**@paramflag*/publicvoidchangeAllListCbState(booleanflag){for(inti=0;i<groupList.size();i++){changGroupCbState(i,flag);changeChildCbState(i,flag);}EventBus.getDefault().post(compute());notifyDataSetChanged();}}
这些就可以实现我们的二级购物车了。
我们接下来把RecyclerView的数据展示出来,用瀑布流来展示图片吧。
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><ImageViewandroid:id="@+id/re_img"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>
这是瀑布流的子布局。
这是我们RecyclerView的适配器
publicclassRecyAdapterextendsRecyclerView.Adapter<RecyclerView.ViewHolder>{privateContextcontext;privateList<String>list;publicRecyAdapter(Contextcontext,List<String>list){this.context=context;this.list=list;}@OverridepublicRecyclerView.ViewHolderonCreateViewHolder(ViewGroupparent,intviewType){Viewview=LayoutInflater.from(context).inflate(R.layout.item_recy,parent,false);returnnewMyViewHolder(view);}@OverridepublicvoidonBindViewHolder(RecyclerView.ViewHolderholder,intposition){Stringstr=list.get(position);MyViewHoldermyviewHolder=(MyViewHolder)holder;ImageLoader.getInstance().displayImage(str,myviewHolder.re_img);myviewHolder.re_img.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){Intentintent=newIntent(context,ShopActivity.class);context.startActivity(intent);}});}@OverridepublicintgetItemCount(){returnlist.size();}classMyViewHolderextendsRecyclerView.ViewHolder{ImageViewre_img;publicMyViewHolder(ViewitemView){super(itemView);re_img=(ImageView)itemView.findViewById(R.id.re_img);intwidth=((Activity)context).getWindowManager().getDefaultDisplay().getWidth();ViewGroup.LayoutParamsparams=re_img.getLayoutParams();//设置图片的相对于屏幕的宽高比params.width=width/3;params.height=(int)(200+Math.random()*400);re_img.setLayoutParams(params);}}}
然后是我们的Activity
publicclassMainActivityextendsAppCompatActivity{privateRecyclerViewmRecy;privateString[]imgs=newString[]{"http://img.my.csdn.net/uploads/201309/01/1378037235_3453.jpg","http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg","http://img.my.csdn.net/uploads/201309/01/1378037235_9280.jpg","http://img.my.csdn.net/uploads/201309/01/1378037234_3539.jpg","http://img.my.csdn.net/uploads/201309/01/1378037234_6318.jpg","http://img.my.csdn.net/uploads/201309/01/1378037194_2965.jpg","http://img.my.csdn.net/uploads/201309/01/1378037193_1687.jpg","http://img.my.csdn.net/uploads/201309/01/1378037193_1286.jpg","http://img.my.csdn.net/uploads/201309/01/1378037192_8379.jpg","http://img.my.csdn.net/uploads/201309/01/1378037178_9374.jpg","http://img.my.csdn.net/uploads/201309/01/1378037177_1254.jpg","http://img.my.csdn.net/uploads/201309/01/1378037177_6203.jpg","http://img.my.csdn.net/uploads/201309/01/1378037152_6352.jpg","http://img.my.csdn.net/uploads/201309/01/1378037151_9565.jpg","http://img.my.csdn.net/uploads/201309/01/1378037151_7904.jpg","http://img.my.csdn.net/uploads/201309/01/1378037148_7104.jpg","http://img.my.csdn.net/uploads/201309/01/1378037129_8825.jpg","http://img.my.csdn.net/uploads/201309/01/1378037128_5291.jpg","http://img.my.csdn.net/uploads/201309/01/1378037128_3531.jpg","http://img.my.csdn.net/uploads/201309/01/1378037127_1085.jpg","http://img.my.csdn.net/uploads/201309/01/1378037095_7515.jpg","http://img.my.csdn.net/uploads/201309/01/1378037094_8001.jpg","http://img.my.csdn.net/uploads/201309/01/1378037093_7168.jpg","http://img.my.csdn.net/uploads/201309/01/1378037091_4950.jpg","http://img.my.csdn.net/uploads/201308/31/1377949643_6410.jpg","http://img.my.csdn.net/uploads/201308/31/1377949642_6939.jpg","http://img.my.csdn.net/uploads/201308/31/1377949630_4505.jpg","http://img.my.csdn.net/uploads/201308/31/1377949630_4593.jpg","http://img.my.csdn.net/uploads/201308/31/1377949629_7309.jpg","http://img.my.csdn.net/uploads/201308/31/1377949629_8247.jpg","http://img.my.csdn.net/uploads/201308/31/1377949615_1986.jpg","http://img.my.csdn.net/uploads/201308/31/1377949614_8482.jpg","http://img.my.csdn.net/uploads/201308/31/1377949614_3743.jpg","http://img.my.csdn.net/uploads/201308/31/1377949614_4199.jpg","http://img.my.csdn.net/uploads/201308/31/1377949599_3416.jpg","http://img.my.csdn.net/uploads/201308/31/1377949599_5269.jpg","http://img.my.csdn.net/uploads/201308/31/1377949598_7858.jpg","http://img.my.csdn.net/uploads/201308/31/1377949598_9982.jpg","http://img.my.csdn.net/uploads/201308/31/1377949578_2770.jpg","http://img.my.csdn.net/uploads/201308/31/1377949578_8744.jpg","http://img.my.csdn.net/uploads/201308/31/1377949577_5210.jpg","http://img.my.csdn.net/uploads/201308/31/1377949577_1998.jpg","http://img.my.csdn.net/uploads/201308/31/1377949482_8813.jpg","http://img.my.csdn.net/uploads/201308/31/1377949481_6577.jpg","http://img.my.csdn.net/uploads/201308/31/1377949480_4490.jpg","http://img.my.csdn.net/uploads/201308/31/1377949455_6792.jpg","http://img.my.csdn.net/uploads/201308/31/1377949455_6345.jpg","http://img.my.csdn.net/uploads/201308/31/1377949442_4553.jpg","http://img.my.csdn.net/uploads/201308/31/1377949441_8987.jpg","http://img.my.csdn.net/uploads/201308/31/1377949441_5454.jpg","http://img.my.csdn.net/uploads/201308/31/1377949454_6367.jpg","http://img.my.csdn.net/uploads/201308/31/1377949442_4562.jpg"};privateRecyAdapteradapter;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mRecy=(RecyclerView)findViewById(R.id.recy);//给rv设置布局管理器mRecy.setLayoutManager(newStaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));List<String>list=newArrayList<>();for(inti=0;i<imgs.length;i++){list.add(imgs[i]);}//创建适配器adapter=newRecyAdapter(MainActivity.this,list);mRecy.setAdapter(adapter);}}
这样我们的瀑布流也展示出来了。过程比较复杂,但是效果还是很棒的!

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: