首页布局功能

package liwei2018730.bwie.com.myapplication.fragmentpack;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.youth.banner.Banner;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import liwei2018730.bwie.com.myapplication.R;
import liwei2018730.bwie.com.myapplication.SearchActivity;
import liwei2018730.bwie.com.myapplication.adapter.HomePageXRAdapter;
import liwei2018730.bwie.com.myapplication.bean.HomePageBean;
import liwei2018730.bwie.com.myapplication.bean.HomePageNineImgBean;
import liwei2018730.bwie.com.myapplication.common.Api;
import liwei2018730.bwie.com.myapplication.utils.OkHttpUtil;
import okhttp3.Call;
import okhttp3.Response;


/**
 * Created by li li on 2018/8/12.
 */

public class HomePage extends Fragment  {

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    private EditText search_ed;
    private Banner banner_id;
    private GridView gridview_id;
    private RecyclerView recommend_rv;
    private XRecyclerView xrecycler_id;
    private HomePageXRAdapter homePageXRAdapter;
    private List<HomePageNineImgBean.DataBean> nineData;
    private ImageView homepagescan_iv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.frame_homepage, container, false);
        initView(inflate);

        return inflate;
    }

    private void initView(View inflate) {
        search_ed = inflate.findViewById(R.id.search_ed);
        search_ed.setInputType(InputType.TYPE_NULL);
        xrecycler_id = inflate.findViewById(R.id.Xrecycler_id);
        homepagescan_iv = inflate.findViewById(R.id.homepagescan_iv);
        xrecycler_id.setLayoutManager(new LinearLayoutManager(getContext()));
    }

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

        /*
        * 点击进入搜索页面
        * */
        search_ed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            startActivity(new Intent(getActivity(),SearchActivity.class));
            }
        });
        HashMap<String, String>  EmptyHashMap = new HashMap<>();
        //首页九宫换内容请求
        addNiceImage(EmptyHashMap);
        //轮播图、秒杀、推荐、XRecycler适配器
        initdata(EmptyHashMap);
         //扫码
        scan();

    }

    private void scan() {
        homepagescan_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(), CaptureActivity.class));
            }
        });
    }

    private void addNiceImage(HashMap<String, String> emptyHashMap) {
        OkHttpUtil.instance().post(emptyHashMap, Api.HOMENINEIMG, new OkHttpUtil.RequestCallBack() {
            @Override
            public void fail(Call call, IOException e) {

            }
            @Override
            public void success(Call call, Response response) throws IOException {
                int code = response.code();
                Log.d("code",code+"");
                if(200==response.code()){
                    String string = response.body().string();
                    if(!TextUtils.isEmpty(string)){
                        HomePageNineImgBean homePageNineImgBean = new Gson().fromJson(string, HomePageNineImgBean.class);
                        nineData = homePageNineImgBean.getData();
//                        handler.post(new Runnable() {
//                            @Override
//                            public void run() {
//
//                            }
//                        });
                        Log.d("nineData","nineData的长度为:"+nineData.size());
                    }
                }

            }
        });
    }

    /**
     * 加载轮播图和适配器
     * @param emptyHashMap
     */
    private void initdata(HashMap<String, String> emptyHashMap) {


        OkHttpUtil.instance().post(emptyHashMap, Api.HOMEPAGE_UEL, new OkHttpUtil.RequestCallBack() {
            @Override
            public void fail(Call call, IOException e) {

            }

            @Override
            public void success(Call call, Response response) throws IOException {

                if(200==response.code()){
                        String string = response.body().string();
                        HomePageBean homePageBean = new Gson().fromJson(string, HomePageBean.class);
                    final List<HomePageBean.DataBean.BannerBean> banner = homePageBean.getData().getBanner();
                    final HomePageBean.DataBean.MiaoshaBean miaosha = homePageBean.getData().getMiaosha();
                    final List<HomePageBean.DataBean.TuijianBean.ListBeanX> tuijian = homePageBean.getData().getTuijian().getList();
                        if(banner.size()!=0){
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    homePageXRAdapter = new HomePageXRAdapter(getContext(), banner, miaosha, tuijian, nineData);
                                    xrecycler_id.setAdapter(homePageXRAdapter);
                                }
                            });
                        }
                    }
            }
        });
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值