全部服务功能模块

 

在接口里添加全部服务的api 然后创建全部服务的实体类 这里的实体类我就不写了 因为服务器可能已经被关掉了哦

 

e267ac1710e14d799e805481bc645057.png

 

public class RetrofitClient {

    public static  final  String IP="http://124.93.196.45:10091";
    private static Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(IP)
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    private RetrofitClient(){


    }
  public  static  Retrofit getRetrofit(){
        return  retrofit;
  }
  public static  AppService apiService(){
        return retrofit.create(AppService.class);
  }

这里是主布局文件中的代码home-layout

 <GridView
       android:id="@+id/Grid_Service"
       android:layout_width="match_parent"
       android:layout_height="150dp"
       android:layout_marginEnd="16dp"
       android:layout_marginStart="16dp"
       android:layout_marginBottom="8dp"
       android:numColumns="5"
       android:layout_marginTop="8dp"/>

接下来 要写一个item  用来实例化全部服务的Gridview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="match_parent">
   <androidx.cardview.widget.CardView
       android:layout_width="40dp"
       android:layout_height="40dp"
       android:layout_marginTop="10dp"
       app:cardCornerRadius="10dp"
       android:layout_gravity="center_horizontal">
       <ImageView
           android:id="@+id/item_grid_img"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="@color/teal_700"
           android:padding="10dp"/>
   </androidx.cardview.widget.CardView>
    <TextView
        android:id="@+id/item_grid_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="2dp"
        android:textSize="15sp"
        android:textColor="#000"/>
</LinearLayout>

最重要的就是接下来的代码  这里是写在HomeFragment.java中的

 root = inflater.inflate(R.layout.fragment_home, container, false);
        initView();
   
         setServiceData();



        return root;
    }
    //    全部服务
    private void setServiceData() {
        RetrofitClient.apiService().getServiceData().enqueue(new Callback<ServiceBean>() {
            @Override
            public void onResponse(Call<ServiceBean> call, Response<ServiceBean> response) {
                ServiceBean body = response.body();
                if (body!=null&&body.getCode()==200) {
                    serviceRows = body.getRows();
                    Log.i(TAG,"全部服务-------------->"+serviceRows.size());
                    ServiceBean.RowsBean rowsBean = new ServiceBean.RowsBean();
                    rowsBean.setServiceName("全部服务");
                    rowsBean.setImgUrl("all");
                    serviceRows.add(rowsBean);
                    Log.i(TAG,"获取全部图片------------>"+serviceRows.size());
                    ServiceAdapter serviceAdapter = new ServiceAdapter(getActivity().getApplicationContext(), R.layout.item_service, serviceRows);
                    gridView.setAdapter(serviceAdapter);
                    gridView.setOnItemClickListener(((parent, view, position, id) -> {
                        switch (position){
                            case 0:
                                Toast.makeText(getActivity(),"1",Toast.LENGTH_SHORT).show();
                                break;
                            case 1:
                                Toast.makeText(getActivity(),"2",Toast.LENGTH_SHORT).show();
                                break;
                        }
                    }));

                }
            }

            @Override
            public void onFailure(Call<ServiceBean> call, Throwable throwable) {

            }
        });
    }
private void initView() {
       
        gridView = root.findViewById(R.id.Grid_Service);

    }

ServiceAdapter.java   别忘记它是继承ArrayAdapter的哦

public class ServiceAdapter  extends ArrayAdapter<ServiceBean.RowsBean> {
    public ServiceAdapter(@NonNull Context context, int resource, @NonNull List<ServiceBean.RowsBean> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHodler viewHodler;
        if (convertView==null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_service, parent, false);
            viewHodler = new ViewHodler();
            viewHodler.itemGridImg=convertView.findViewById(R.id.item_grid_img);
            viewHodler.itemGridText=convertView.findViewById(R.id.item_grid_text);
            convertView.setTag(viewHodler);
        }else {
            viewHodler= (ViewHodler) convertView.getTag();

        }
        ServiceBean.RowsBean item = getItem(position);
        viewHodler.itemGridText.setText(item.getServiceName());
        if (item.getImgUrl().equals("all")){
            Glide.with(getContext().getApplicationContext()).load(IP+item.getImgUrl())
                    .placeholder(R.drawable.ic_launcher_background)
                    .into(viewHodler.itemGridImg);

        }else {
            Glide.with(getContext()).load(IP+item.getImgUrl())
                    .placeholder(R.drawable.ic_launcher_background)
                    .into(viewHodler.itemGridImg);
        }
        return convertView;
    }

    private class ViewHodler {
            ImageView itemGridImg;
            TextView itemGridText;


    }
}

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是称称呗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值