fragment里面所写
package com.bawei.zhaojianfu2019730.view.fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bawei.zhaojianfu2019730.R;
import com.bawei.zhaojianfu2019730.model.bean.BeanZs;
import com.bawei.zhaojianfu2019730.presenter.HomePresenterZs;
import com.bawei.zhaojianfu2019730.view.adapter.RecyAdapter;
import com.bawei.zhaojianfu2019730.view.interfaces.IContract;
import java.util.List;
public class fragmentone extends Fragment implements IContract.recy {
private RecyclerView fargone_recyone;
private RecyclerView fargone_recytwo;
private RecyclerView fargone_recythree;
private List<BeanZs.ResultBean> result;
private HomePresenterZs homePresenterZs;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragmentone, null);
homePresenterZs = new HomePresenterZs();
homePresenterZs.attchview(this);
homePresenterZs.recy();
fargone_recyone = inflate.findViewById(R.id.fargone_recyone);
fargone_recytwo = inflate.findViewById(R.id.fargone_recytwo);
fargone_recythree = inflate.findViewById(R.id.fargone_recythree);
return inflate;
}
@Override
public void sucesszs(BeanZs data) {
result = data.result;
//one
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
fargone_recyone.setLayoutManager(linearLayoutManager);
//绑定
RecyAdapter recyAdapter = new RecyAdapter(result,getActivity());
fargone_recyone.setAdapter(recyAdapter);
//two
LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(getActivity());
linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
fargone_recytwo.setLayoutManager(linearLayoutManager1);
//绑定
RecyAdapter recyAdapter1 = new RecyAdapter(result,getActivity());
fargone_recytwo.setAdapter(recyAdapter1);
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
fargone_recythree.setLayoutManager(staggeredGridLayoutManager);
RecyAdapter recyAdapter2 = new RecyAdapter(result,getActivity());
fargone_recythree.setAdapter(recyAdapter2);
}
}
适配器
package com.bawei.zhaojianfu2019730.view.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bawei.zhaojianfu2019730.R;
import com.bawei.zhaojianfu2019730.model.bean.BeanZs;
import com.bumptech.glide.Glide;
import java.util.List;
public class RecyAdapter extends RecyclerView.Adapter {
private List<BeanZs.ResultBean> result;
private Context context;
public RecyAdapter(List<BeanZs.ResultBean> result, Context context) {
this.result = result;
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item1,null);
ViewHoder viewHoder =new ViewHoder(view);
return viewHoder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
ViewHoder viewHoder = new ViewHoder(viewHolder.itemView);
viewHoder.item_commodityName.setText(result.get(i).commodityName);
viewHoder.item1_price.setText(result.get(i).price+"");
Glide.with(context)
.load(result.get(i).masterPic)
.into(viewHoder.item1_masterPic);
}
@Override
public int getItemCount() {
return result.size();
}
class ViewHoder extends RecyclerView.ViewHolder {
private final ImageView item1_masterPic;
private final TextView item_commodityName;
private final TextView item1_price;
public ViewHoder(@NonNull View itemView) {
super(itemView);
item1_masterPic = itemView.findViewById(R.id.item1_masterPic);
item_commodityName = itemView.findViewById(R.id.item_commodityName);
item1_price = itemView.findViewById(R.id.item1_price);
}
}
}