服务(教会你三分钟教学-易学)

1:全部服务的样式表

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.home.AllServiceFragment"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:background="#00BCD4">

        <EditText
            android:id="@+id/home_search"
            android:layout_width="250dp"
            android:layout_height="40dp"
            android:background="@drawable/bg_edt_home"
            android:hint="请输入搜索内容"
            android:textAlignment="center"
            android:textColor="@color/white"
            android:textColorHint="@color/white"
            android:layout_gravity="center"
            android:layout_marginLeft="60dp"
            android:imeOptions="actionSearch"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:orientation="horizontal">

        <RadioGroup
            android:id="@+id/all_services_rg"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/car_rb"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:button="@null"
                android:text="车主服务"
                android:textAlignment="center"/>

            <RadioButton
                android:id="@+id/life_rb"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:button="@null"
                android:text="生活服务"
                android:textAlignment="center"/>

            <RadioButton
                android:id="@+id/convenience_rb"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:button="@null"
                android:text="便民服务"
                android:textAlignment="center"/>

        </RadioGroup>

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#c7c7c7"/>

        <GridView
            android:id="@+id/all_services_icon"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="2"
            android:padding="20dp"/>

    </LinearLayout>
</LinearLayout>
2:回到全部服务的Fragment
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.smartcity.R;
import com.example.smartcity.activity.life.LivePayActivity;
import com.example.smartcity.activity.subway.SubMainActivity;
import com.example.smartcity.activity.movie.MovieActivity;
import com.example.smartcity.adpter.SuperBase;
import com.example.smartcity.bean.SkipActivity;
import com.example.smartcity.http.initHomeHttp;
import com.example.smartcity.info.HomeServices;
import com.example.smartcity.utils.GetNetImage;

import java.util.ArrayList;

import static com.example.smartcity.utils.CacheUtils.*;


public class AllServiceFragment extends Fragment {
    private EditText mHomeSearch;
    private RadioGroup mAllServicesRg;
    private RadioButton mCarRb;
    private RadioButton mLifeRb;
    private RadioButton mConvenienceRb;
    private GridView mAllServicesIcon;
    private ArrayList<HomeServices.RowsDTO> rowsDTOS;

    Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){

            }
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_all_service, container, false);
        mHomeSearch = (EditText) view.findViewById(R.id.home_search);
        mAllServicesRg = (RadioGroup) view.findViewById(R.id.all_services_rg);
        mCarRb = (RadioButton) view.findViewById(R.id.car_rb);
        mLifeRb = (RadioButton) view.findViewById(R.id.life_rb);
        mConvenienceRb = (RadioButton) view.findViewById(R.id.convenience_rb);
        mAllServicesIcon = (GridView) view.findViewById(R.id.all_services_icon);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        initData();
    }

    //初始化数据
    public void initData(){
        new initHomeHttp(handler).servicesData();
        //单选组按钮
        mAllServicesRg.setOnCheckedChangeListener(new servicesRgCheckChange());

        //点击事件
        mAllServicesIcon.setOnItemClickListener(new allServicesItemClick());
    }

    //gridView点击事件,跳转相应界面
    class allServicesItemClick implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String serviceName = rowsDTOS.get(position).getServiceName();
            switch (serviceName){
                case "城市地铁":
                    Intent intentSubway = new Intent(getContext(), SubMainActivity.class);
                    startActivity(intentSubway);
                    return;
                case "看电影":
                    Intent intentMovie = new Intent(getContext(), MovieActivity.class);
                    startActivity(intentMovie);
                    return;
                case "生活缴费":
                    Intent intentPay = new Intent(getContext(), LivePayActivity.class);
                    startActivity(intentPay);
                    return;
            }
            String name = rowsDTOS.get(position).getServiceName();
            SkipActivity.skipEmptyActivity(name,getContext());
        }
    }

    //单选按钮组
    class servicesRgCheckChange implements RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            mCarRb.setBackground(getResources().getDrawable(R.drawable.bg_service_normal));
            mLifeRb.setBackground(getResources().getDrawable(R.drawable.bg_service_normal));
            mConvenienceRb.setBackground(getResources().getDrawable(R.drawable.bg_service_normal));

            switch (checkedId){
                case R.id.car_rb:
                    setAdapters("车主服务");
                    mCarRb.setBackground(getResources().getDrawable(R.drawable.bg_services));
                    break;
                case R.id.life_rb:
                    setAdapters("生活服务");
                    mLifeRb.setBackground(getResources().getDrawable(R.drawable.bg_services));
                    break;
                case R.id.convenience_rb:
                    setAdapters("便民服务");
                    mConvenienceRb.setBackground(getResources().getDrawable(R.drawable.bg_services));
                    break;
            }
        }
    }


    //筛选之后,放入gridView
    public void setAdapters(String servicesType){
        rowsDTOS = new ArrayList<>();
        if(homeServices == null || homeServices.getRows() == null ||homeServices.getRows().size()<1){
            return;
        }
        for (int i = 0; i < homeServices.getRows().size(); i++) {
            if (servicesType.equals(homeServices.getRows().get(i).getServiceType())){
                rowsDTOS.add(homeServices.getRows().get(i));
            }
        }

        SuperBase superBase = new SuperBase(rowsDTOS.size()) {
            private ImageView mServicesIcon;
            private TextView mServicesTxt;
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_services,parent,false);
                mServicesIcon = (ImageView) convertView.findViewById(R.id.services_icon);
                mServicesTxt = (TextView) convertView.findViewById(R.id.services_txt);

                new GetNetImage(getContext(),rowsDTOS.get(position).getImgUrl(),mServicesIcon);
                mServicesTxt.setText(rowsDTOS.get(position).getServiceName());
                return convertView;
            }
        };
        mAllServicesIcon.setAdapter(superBase);
    }
}


版权声明:本文为m0_52625686原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。