瀑布流

两个adapter
 

public class ShopListAdapter  extends RecyclerView.Adapter<ShopListAdapter.MyHolder> {
    private List<ShopBean.DataBean> list=new ArrayList<>();
    private Context context;
    public ShopListAdapter(Context context) {
        this.context=context;
    }


    @NonNull
    @Override
    public ShopListAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=View.inflate(context,R.layout.shop_list_item,null);
        MyHolder myHolder=new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ShopListAdapter.MyHolder myHolder, int i) {
        createRecyclerAdapter(myHolder.mRecyclerView,list.get(i).getList());
    }
/// 穿件瀑布流
    private void createRecyclerAdapter(RecyclerView mRecyclerView, List<ShopBean.DataBean.ListBean> list) {
        ShopListItemAdapter shopListItemAdapter=new ShopListItemAdapter(context,list);
        StaggeredGridLayoutManager staggeredGridLayoutManager=
                new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);
        mRecyclerView.setAdapter(shopListItemAdapter);
    }


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

    public void setList(List<ShopBean.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }
    public class MyHolder extends RecyclerView.ViewHolder {
        RecyclerView mRecyclerView;
        TextView mTitle;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            mTitle=(TextView) itemView.findViewById(R.id.shop_title);
            mRecyclerView= (RecyclerView)itemView.findViewById(R.id.recyclerview);
        }
    }
}

******************************
public class ShopListItemAdapter extends RecyclerView.Adapter<ShopListItemAdapter.MyHolder> {
    private List<ShopBean.DataBean.ListBean> list=new ArrayList<>();
    private Context context;
    public ShopListItemAdapter(Context context, List<ShopBean.DataBean.ListBean> list) {
        this.context=context;
        this.list=list;
    }
    @NonNull
    @Override
    public ShopListItemAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=View.inflate(context,R.layout.shop_list_item_child,null);
         MyHolder myHolder=new MyHolder(view);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ShopListItemAdapter.MyHolder myHolder, int i) {
        myHolder.shopTitle.setText(list.get(i).getTitle());
        String images= list.get(i).getImages();
        String[] imageUrl=images.split("\\|");
        Glide.with(context)
                .load(imageUrl[0])
                .into(myHolder.shopImage);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public class MyHolder extends RecyclerView.ViewHolder {

        TextView shopTitle;
        ImageView shopImage;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            shopImage=(ImageView)itemView.findViewById(R.id.shop_image);
            shopTitle=(TextView)itemView.findViewById(R.id.shop_title);
        }
    }
}


**************************************请求数据
public class ShowActitcyPressenter extends AppDelegate {
    private String url="http://www.zhaoapi.cn/product/getCarts?uid=71";
    private RecyclerView mRecyclerView;
    private ShopListAdapter mShopListAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_show;
    }

    @Override
    public void initData() {
        mRecyclerView= get(R.id.recyclerview);
        mShopListAdapter=new ShopListAdapter(context);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mShopListAdapter);

        doHttp();
    }
  ///请求数据
    private void doHttp() {

 OkHttpClient client=new OkHttpClient();
 Request request=new Request.Builder()
         .url(url)
         .build();
 client.newCall(request).enqueue(new Callback() {
     @Override
     public void onFailure(Call call, IOException e) {

     }

     @Override
     public void onResponse(Call call, Response response) throws IOException {
         Message message=Message.obtain();
              message.obj=response.body().string();
            handler.sendMessage(message);
     }
 });
    }

    private  Context context;
    @Override
    public void getContext(Context context) {
   this.context=context;
    }


     Handler handler=new Handler(){
         @Override
         public void handleMessage(Message msg) {
             super.handleMessage(msg);
             String data= (String) msg.obj;
             ShopBean bean=new Gson().fromJson(data,ShopBean.class);
             mShopListAdapter.setList(bean.getData());
         }
     };
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值