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

首先先导入依赖:       

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

import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;


public class HttpUtils {
    private static volatile HttpUtils httpUtils;
    private OkHttpClient client;

    private HttpUtils(){
        HttpLoggingInterceptor logging=new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        client=new OkHttpClient.Builder()
                .addInterceptor(logging)
                .build();
    }

    public static HttpUtils getHttpUtils(){
        if (httpUtils==null){
            synchronized (HttpUtils.class){
                if (httpUtils==null){
                    httpUtils=new HttpUtils();
                }
            }
        }
        return httpUtils;
    }

    /**
     * GET请求
     *
     * @param url
     * @param callback
     */

    public void doGet(String url, Callback callback){
        Request request=new Request.Builder().url(url).build();
        client.newCall(request).enqueue(callback);
    }
}
然后在这里建一个接口来实现model层的方法。
public interface OnNetListener<T> {
    public void onSuccess(T t);//这是成功的方法
    public void onFailure(Exception e);//这是失败的。
}
下面写一个APP类的方法。这个方法是ImageLoader的方法,用来展示图片
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration=new
                ImageLoaderConfiguration.Builder(this).build();

        ImageLoader.getInstance().init(configuration);
    }
}
上面的一些必须的代码就写完了,接下来写model层的接口
public interface IMainModel {
    public void getGoods(OnNetListener<GoodsBean> onNetListener);
}
然后创建一个类来继承这个接口
import android.os.Handler;
import android.os.Looper;

import com.bwie.test.a09ashopcar.bean.GoodsBean;
import com.bwie.test.a09ashopcar.net.Api;
import com.bwie.test.a09ashopcar.net.HttpUtils;
import com.bwie.test.a09ashopcar.net.OnNetListener;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainModel implements IMainModel{
    private Handler handler=new Handler(Looper.getMainLooper());

    @Override
    public void getGoods(final OnNetListener<GoodsBean> onNetListener) {
        HttpUtils.getHttpUtils().doGet(Api.url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string=response.body().string();
                final GoodsBean goodsBean = new Gson().fromJson(string, GoodsBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onSuccess(goodsBean);
                    }
                });
            }
       });
    }
}
model层写完了, 接下来就该写presenter的数据了
public class MainPresenter {
    private IMainModel iMainModel;
    private IShopActivity iMainActivity;
    public MainPresenter(IShopActivity iMainActivity){
        this.iMainActivity=iMainActivity;
        iMainModel=new MainModel();
        }

    public void getGoods(){
        iMainModel.getGoods(new OnNetListener<GoodsBean>() {
            @Override
            public void onSuccess(GoodsBean goodsBean) {
                List<GoodsBean.DataBean> groupList = goodsBean.getData();
                List<List<GoodsBean.DataBean.DatasBean>>childList=new ArrayList<List<GoodsBean.DataBean.DatasBean>>();
                for (int i=0;i<groupList.size();i++){
                    List<GoodsBean.DataBean.DatasBean>datas=groupList.get(i).getDatas();
                    childList.add(datas);
                }
                iMainActivity.showList(groupList,childList);
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }
}
写完这些就该写布局了
 购物车它是二级的,所以布局也比较复杂
下面这个是主要的布局
	main.xml
<LinearLayout xmlns: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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#C0C1A0FA">

        <TextView
            android: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>
    
    <ExpandableListView
        android:id="@+id/elv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

    </ExpandableListView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@android:color/white"
        android:gravity="center_vertical">

        <CheckBox
            android: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"/>

        <TextView
            android: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" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:textSize="18sp"
                android:text="合计 :" />


            <TextView
                android: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" />


            <TextView
                android: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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal"
    android:gravity="center_vertical">

    <CheckBox
        android: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" />

    <TextView
        android:id="@+id/tv_sign"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="标记" />

    <TextView
        android:id="@+id/tv_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="12345678" />


</LinearLayout>
下来是二级目录的布局  child.xml
<LinearLayout xmlns: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">

    <CheckBox
        android: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" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_tel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="iphone6"/>

        <TextView
            android:id="@+id/tv_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="什么手机" />

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="2016-12-10" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_pri"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="¥3000.00" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

            <ImageView
                android:id="@+id/iv_del"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/shopcart_minus_grey" />

            <TextView
                android: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" />

            <ImageView
                android:id="@+id/iv_add"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginLeft="5dp"
                android:src="@drawable/shopcart_add_red" />

        </LinearLayout>

    </LinearLayout>

    <TextView
        android:id="@+id/tv_del"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除"
        />
</LinearLayout>
    接下来我们改写View层的接口
	
 
public interface IShopActivity {
    public void showList(List<GoodsBean.DataBean>groupList,List<List<GoodsBean.DataBean.DatasBean>>childList);
}
然后是View的类
public class ShopActivity extends AppCompatActivity implements IShopActivity {

    private ExpandableListView mElv;
    private CheckBox mQuanCk;
    /**
     * 0
     */
    private TextView mTvPrice;
    /**
     * 结算(0)
     */
    private TextView mTvNum;
    private LinearLayout mActivityMain;
    private ShopAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        EventBus.getDefault().register(this);
        initView();
        new MainPresenter(this).getGoods();
        mQuanCk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adapter.changeAllListCbState(mQuanCk.isChecked());
            }
        });

    }

    private void initView() {
        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);
    }


    @Override
    public void showList(List<GoodsBean.DataBean> groupList, List<List<GoodsBean.DataBean.DatasBean>> childList) {
        adapter=new ShopAdapter(this,groupList,childList);
        mElv.setAdapter(adapter);
        //去掉二级列表的小箭头
        mElv.setGroupIndicator(null);
        //默认让其全部展开
        for (int i=0;i<groupList.size();i++){
            mElv.expandGroup(i);
        }
    }

    @Subscribe
    public void onMessageEvent(MessageEvent event) {
        mQuanCk.setChecked(event.isChecked());
    }

    @Subscribe
    public void onMessageEvent(PriceAndCountEvent event) {
        mTvNum.setText("结算(" + event.getCount() + ")");
        mTvPrice.setText(event.getPrice() + "");
    }
}
在这里 我们用到了EventBus这个控件,所以我们这里还有这样两个类:
public class MessageEvent {
    private boolean checked;
    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}
public class PriceAndCountEvent {
    private int price;
    private int count;

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
接下来我们来写最重要的购物车的适配器
public class ShopAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<GoodsBean.DataBean>groupList;
    private List<List<GoodsBean.DataBean.DatasBean>>childList;
    private final LayoutInflater inflater;

    public ShopAdapter(Context context, List<GoodsBean.DataBean> groupList, List<List<GoodsBean.DataBean.DatasBean>> childList) {
        this.context = context;
        this.groupList = groupList;
        this.childList = childList;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return childList.get(i).size();
    }

    @Override
    public Object getGroup(int i) {
        return groupList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return childList.get(i).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int i, boolean b, View convertView, ViewGroup viewGroup) {
        View view;
        final GroupViewHolder gholder;
        if (convertView==null){
            gholder=new GroupViewHolder();
            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();
        }

        final GoodsBean.DataBean groupBean=groupList.get(i);
        gholder.cb_group.setChecked(groupBean.isChecked());
        gholder.tv_number.setText(groupBean.getTitle());

        //一级checkbox
        gholder.cb_group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                groupBean.setChecked(gholder.cb_group.isChecked());
                changeChildCbState(i, gholder.cb_group.isChecked());
                EventBus.getDefault().post(compute());
                changeAllCbState(isAllGroupCbSelected());
                notifyDataSetChanged();
            }
        });

        return view;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, final View convertView, ViewGroup viewGroup) {

        View view;
        final ChildViewHolder cholder;
        if (convertView==null){
            cholder=new ChildViewHolder();
            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();
        }

        final GoodsBean.DataBean.DatasBean childBean=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(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //设置该条目对象里的checked属性值
                childBean.setChecked(cholder.cb_child.isChecked());
                PriceAndCountEvent priceAndCountEvent=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(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num=childBean.getNum();
                cholder.tv_num.setText(++num+"");
                childBean.setNum(num);
                if (cholder.cb_child.isChecked()){
                    PriceAndCountEvent priceAndCountEvent=compute();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });

        //减号
        cholder.iv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num=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()){
                    PriceAndCountEvent priceAndCountEvent=compute();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });
        //删除
        cholder.tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<GoodsBean.DataBean.DatasBean> datasBeen = childList.get(i);
                GoodsBean.DataBean.DatasBean remove = datasBeen.remove(i1);
                if (datasBeen.size()==0){
                    childList.remove(i);
                    groupList.remove(i);
                }
                EventBus.getDefault().post(compute());
                notifyDataSetChanged();
            }
        });

        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    class GroupViewHolder{
        CheckBox cb_group;
        TextView tv_number;
    }

    class ChildViewHolder{
        CheckBox cb_child;
        TextView tv_tel;
        TextView tv_content;
        TextView tv_time;
        TextView tv_pri;
        TextView tv_del;
        ImageView iv_del;
        ImageView iv_add;
        TextView tv_num;
    }

    /**
     * 改变全选的状态
     *
     * @param flag
     */

        private void changeAllCbState(boolean flag){
            MessageEvent messageEvent = new MessageEvent();
            messageEvent.setChecked(flag);
            EventBus.getDefault().post(messageEvent);
        }

    /**
     * 改变一级列表checkbox状态
     *
     * @param groupPosition
     */
    private void changGroupCbState(int groupPosition,boolean flag){
        GoodsBean.DataBean dataBean = groupList.get(groupPosition);
        dataBean.setChecked(flag);
    }

    /**
     * 改变二级列表checkbox状态
     *
     * @param groupPosition
     * @param flag
     */
    private void changeChildCbState(int groupPosition, boolean flag) {
        List<GoodsBean.DataBean.DatasBean> datasBeen = childList.get(groupPosition);
        for (int i = 0; i <datasBeen.size() ; i++) {
            GoodsBean.DataBean.DatasBean datasBean = datasBeen.get(i);
            datasBean.setChecked(flag);
        }

    }

    /**
     * 判断一级列表是否全部选中
     *
     * @return
     */

    private boolean isAllGroupCbSelected() {
        for (int i = 0; i <groupList.size() ; i++) {
            GoodsBean.DataBean dataBean = groupList.get(i);
            if (!dataBean.isChecked()){
                return false;
            }
        }
        return true;
    }

    /**
     * 判断二级列表是否全部选中
     *
     * @param groupPosition
     * @return
     */

    private boolean isAllChildCbSelected(int groupPosition){
        List<GoodsBean.DataBean.DatasBean> datasBeen = childList.get(groupPosition);
        for (int i=0;i<datasBeen.size();i++){
            GoodsBean.DataBean.DatasBean datasBean = datasBeen.get(i);
            if (!datasBean.isChecked()){
                return false;
            }
        }
        return true;
    }

    /**
     * 计算列表中,选中的钱和数量
     */

    private PriceAndCountEvent compute(){
        int count = 0;
        int price = 0;
        for (int i = 0; i <childList.size() ; i++) {
            List<GoodsBean.DataBean.DatasBean> datasBeen = childList.get(i);
            for (int j = 0; j <datasBeen.size() ; j++) {
                GoodsBean.DataBean.DatasBean datasBean = datasBeen.get(j);
                if (datasBean.isChecked()){
                    price+=datasBean.getNum()*datasBean.getPrice();
                    count+=datasBean.getNum();
                }
            }
        }

        PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
        priceAndCountEvent.setCount(count);
        priceAndCountEvent.setPrice(price);
        return priceAndCountEvent;
    }

    /**
     * 设置全选、反选
     *
     * @param flag
     */
    public void changeAllListCbState(boolean flag) {
        for (int i = 0; i <groupList.size() ; i++) {
            changGroupCbState(i,flag);
            changeChildCbState(i,flag);
        }
        EventBus.getDefault().post(compute());
        notifyDataSetChanged();
    }

}
这些就可以实现我们的二级购物车了。
我们接下来把RecyclerView的数据展示出来,用瀑布流来展示图片吧。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/re_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>
这是瀑布流的子布局。
这是我们RecyclerView的适配器
public class RecyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<String> list;

    public RecyAdapter( Context context, List<String> list) {

        this.context = context;
        this.list = list;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_recy,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        String str = list.get(position);
        MyViewHolder myviewHolder = (MyViewHolder) holder;
        ImageLoader.getInstance().displayImage(str,myviewHolder.re_img);

        myviewHolder.re_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, ShopActivity.class);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView re_img;

        public MyViewHolder(View itemView) {
            super(itemView);
            re_img=(ImageView)itemView.findViewById(R.id.re_img);
            int width = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
            ViewGroup.LayoutParams params = re_img.getLayoutParams();
            //设置图片的相对于屏幕的宽高比
            params.width = width/3;
            params.height =  (int) (200 + Math.random() * 400) ;
            re_img.setLayoutParams(params);

        }
    }
}
然后是我们的Activity
public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecy;
    private String[] imgs=new String[]{
        "https://img-my.csdn.net/uploads/201309/01/1378037235_3453.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037235_7476.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037235_9280.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037234_3539.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037234_6318.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037194_2965.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037193_1687.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037193_1286.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037192_8379.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037178_9374.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037177_1254.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037177_6203.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037152_6352.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037151_9565.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037151_7904.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037148_7104.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037129_8825.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037128_5291.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037128_3531.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037127_1085.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037095_7515.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037094_8001.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037093_7168.jpg",
            "https://img-my.csdn.net/uploads/201309/01/1378037091_4950.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949643_6410.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949642_6939.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949630_4505.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949630_4593.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949629_7309.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949629_8247.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949615_1986.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949614_8482.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949614_3743.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949614_4199.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949599_3416.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949599_5269.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949598_7858.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949598_9982.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949578_2770.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949578_8744.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949577_5210.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949577_1998.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949482_8813.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949481_6577.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949480_4490.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949455_6792.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949455_6345.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949442_4553.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949441_8987.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949441_5454.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949454_6367.jpg",
            "https://img-my.csdn.net/uploads/201308/31/1377949442_4562.jpg"
    };
    private RecyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecy=(RecyclerView)findViewById(R.id.recy);
        //给rv设置布局管理器
        mRecy.setLayoutManager(new StaggeredGridLayoutManager
                (3,StaggeredGridLayoutManager.VERTICAL));

        List<String>list=new ArrayList<>();
        for (int i = 0; i <imgs.length ; i++) {
            list.add(imgs[i]);
        }
        //创建适配器
        adapter=new RecyAdapter(MainActivity.this,list);
        mRecy.setAdapter(adapter);
    }


}
这样我们的瀑布流也展示出来了。过程比较复杂,但是效果还是很棒的!
大笑


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值