Android 使用RadioGroup达到页面刷新的效果

先看下效果图:

1、首先是xml布局,横向布局的RadioGroup包裹五个RadioButton




    

    

    

    

    

2、初始化相关

private void initView() {
        easyRefreshLayout = $(R.id.erl_hair_gallery);
        easyRefreshLayout.addEasyEvent(this);

        LinearLayoutManager manager = new GridLayoutManager(getContext(), 3);
        recyclerView = $(R.id.rv_index_gallery);
        recyclerView.setLayoutManager(manager);
        hairAdapter = new HairAdapter(R.layout.hair_item);
        recyclerView.setAdapter(hairAdapter);

        hairAdapter.setOnItemClickListener((adapter, view, position)
                -> startDetailDelegate(position));
    }

 3、解析后端给的接口数据:

private void requestHairList() {
        RestClient.builder()//网络请求封装方法
                .url("这里填后台给的数据接口")
                .params(getParams())
                .success(this::updateUI)
                .build()
                .post();
    }

    private WeakHashMap getParams() {//需要传递的参数
        WeakHashMap params = new WeakHashMap<>();
        params.put("length", currentHairParams.getLength());
        params.put("sex", currentHairParams.getSex());
        params.put("recommend", currentHairParams.getRecommend());
        params.put("last_id", lastId);
        JLogger.e(params.toString());
        return params;
    }

    private void updateUI(String response) {
        JLogger.json(response);
        if (easyRefreshLayout != null) {
            easyRefreshLayout.refreshComplete();
            easyRefreshLayout.loadMoreComplete();
        }

        final JSONObject jsonObject = JSON.parseObject(response);
        if (JConstants.OK.equals(jsonObject.getString("code"))) {
            List data = HairDataConverter.convert(response);
            if (TYPE_REFRESHING.equals(type)) {
                hairAdapter.setNewData(data);
            } else {
                hairAdapter.addData(data);
            }
            if (data.size() > 1) {
                List hairs = hairAdapter.getData();
                lastId = hairs.get(hairs.size() - 1).getId();
            }
        } else if (TYPE_REFRESHING.equals(type) && hairAdapter.getData().size() == 0) {
            hairAdapter.setEmptyView(R.layout.empty_view, recyclerView);
        }

    }

看下接口返回的数据:

Android 使用RadioGroup达到页面刷新的效果_第1张图片

这里HairDataConverter是解析器类,我们项目中需要手动解析数据,看下解析过程

public class HairDataConverter {

    public static List convert(String json) {
        final List dataList = new ArrayList<>();
        final JSONArray jsonArray = JSON.parseObject(json).getJSONArray("data");

        final int size = jsonArray.size();
        for (int i = 0; i < size; i++) {
            final JSONObject jsonObject = jsonArray.getJSONObject(i);
            final String id = jsonObject.getString("id");
            final String type = jsonObject.getString("type");
            final String images = jsonObject.getString("images");


            final Hair entity = new Hair(id, type, images);

            dataList.add(entity);
        }

        return dataList;
    }
}

Hair实为Entity类

public class Hair implements Parcelable {//此处序列化是为了下一层界面传值


    private String id;
    private String type;
    private String images;


    public Hair(String id, String type, String images) {
        this.id = id;
        this.type = type;
        this.images = images;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getImages() {
        return images;
    }

    public void setImages(String images) {
        this.images = images;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.id);
        dest.writeString(this.type);
        dest.writeString(this.images);
    }

    protected Hair(Parcel in) {
        this.id = in.readString();
        this.type = in.readString();
        this.images = in.readString();
    }

    public static final Creator CREATOR = new Creator() {
        @Override
        public Hair createFromParcel(Parcel source) {
            return new Hair(source);
        }

        @Override
        public Hair[] newArray(int size) {
            return new Hair[size];
        }
    };
}

4、精选、短发、中发、长发的存取

private HairParam currentHairParams;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        paramHashMap.put(R.id.rb_change, new HairParam("1", ""));
        paramHashMap.put(R.id.rb_new, new HairParam("", ""));
        paramHashMap.put(R.id.rb_short_hair, new HairParam("", "0"));
        paramHashMap.put(R.id.rb_mid_hair, new HairParam("", "1"));
        paramHashMap.put(R.id.rb_long_hair, new HairParam("", "2"));

        currentHairParams = paramHashMap.get(R.id.rb_change);

    }
public class HairParam {

    private String recommend;
    private String length;
    private String sex;

    public HairParam(String recommend, String length) {
        this.recommend = recommend;
        this.length = length;
        this.sex = "0";
    }

    public String getRecommend() {
        return recommend;
    }

    public String getLength() {
        return length;
    }

    public String getSex() {
        return sex;
    }
}

上面的方式避免了大量的if else条件句的书写,接口中sex=“0”为女生,1为男生。

currentHairParams = paramHashMap.get(R.id.rb_change);默认点开精选项
private void initListener() {//radioGroup选择事件
        radioGroup.setOnCheckedChangeListener((radioGroup, i) -> {
            resetRefresh();
            currentHairParams = paramHashMap.get(i);
            requestHairList();
        });
    }

 

你可能感兴趣的:(android开发)