九宫格

九宫格

接口
public static final String GRIDEURL=“http://www.zhaoapi.cn/product/getCatagory”;

Activity
	@Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_show);
	        ButterKnife.bind(this);
	        recyclerView = findViewById(R.id.recyclerView)
	
	        OkGo.<String>get(Constant.GRIDEURL).execute(new StringCallback() {
	
	            private ArrayList<User.DataBean> beanList;
	
	            @Override
	            public void onSuccess(Response<String> response) {
	                String data = response.body().toString();
	                User user = new Gson().fromJson(data, User.class);
	                //数据解析
	                //1、数据源
	                beanList = (ArrayList<User.DataBean>) user.getData();
	                //2、*布局管理器
	                //①线性   LinearLayoutManager
	                //②网格   GridLayoutManager
	                //③瀑布流 StaggeredGridLayoutManager
	
	                //LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
	                //GridLayoutManager manager = new GridLayoutManager(context, 2,GridLayoutManager.VERTICAL,false);
	                StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL);
	                recyclerView.setLayoutManager(manager);
	                //3、*适配器
	                LinerAdapter beautyAdapter = new LinerAdapter(beanList, ShowActivity.this);
	
	                //设置适配器
	                recyclerView.setAdapter(beautyAdapter);
	                beautyAdapter.notifyDataSetChanged();
	
	            }
	        });
	    }

//适配器

public class LinerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<User.DataBean> list;
    private Context context;

    public LinerAdapter(List<User.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        String images = list.get(i).getIcon();
            Glide.with(context).load(images).into(((ViewHolder) viewHolder).imageView);
    }

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

        private final ImageView imageView;
        public ImageView imageView2;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image);
        }
    }
}

依赖

//butterKnife寻找资源的第三方
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    //OKHttp:网络请求框架
    implementation 'com.squareup.okhttp3:okhttp:3.12.0'
    //Dagger2 依赖注入框架解决耦合性问题
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'com.google.dagger:dagger:2.10'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.10'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:design:28.+'
    implementation 'com.lzy.net:okgo:3.0.4'
    implementation files('libs/universal-image-loader-1.9.5.jar')

    //RecyclerView
    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值