package com.bwie.zhangjunjingdong.view.fragment; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.NonNull; 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.OrientationHelper; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Toast; import com.bwie.zhangjunjingdong.R; import com.bwie.zhangjunjingdong.model.bean.FenLeiBean; import com.bwie.zhangjunjingdong.model.bean.HomeBean; import com.bwie.zhangjunjingdong.presenter.FragmentHomeP; import com.bwie.zhangjunjingdong.util.ApiUtil; import com.bwie.zhangjunjingdong.util.ChenJinUtil; import com.bwie.zhangjunjingdong.util.GlideImageLoader; import com.bwie.zhangjunjingdong.view.Iview.InterFragmentHome; import com.bwie.zhangjunjingdong.view.Iview.OnItemListner; import com.bwie.zhangjunjingdong.view.activity.CustomCaptrueActivity; import com.bwie.zhangjunjingdong.view.activity.DetailActivity; import com.bwie.zhangjunjingdong.view.activity.WebViewActivity; import com.bwie.zhangjunjingdong.view.adapter.HengXiangAdapter; import com.bwie.zhangjunjingdong.view.adapter.MiaoShaAdapter; import com.bwie.zhangjunjingdong.view.adapter.TuiJianAdapter; import com.bwie.zhangjunjingdong.view.custom.ObservableScrollView; import com.dash.zxinglibrary.activity.CodeUtils; import com.scwang.smartrefresh.layout.SmartRefreshLayout; import com.scwang.smartrefresh.layout.api.RefreshLayout; import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener; import com.scwang.smartrefresh.layout.listener.OnRefreshListener; import com.sunfusheng.marqueeview.MarqueeView; import com.youth.banner.Banner; import com.youth.banner.BannerConfig; import com.youth.banner.listener.OnBannerListener; import java.util.ArrayList; import java.util.List; public class FragmentHome extends Fragment implements InterFragmentHome { private FragmentHomeP fragmentHomeP; private Banner banner; private RecyclerView heng_xiang; private RecyclerView tui_jian_recycler; private MarqueeView marqueeView; private RecyclerView miao_sha_recycler; private SmartRefreshLayout smart_refresh; private LinearLayout linear_include; private final String TAG_MARGIN_ADDED = "marginAdded"; private ObservableScrollView observe_scroll_view; private LinearLayout sao_yi_sao; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_home_layout,container,false); banner = view.findViewById(R.id.banner); heng_xiang = view.findViewById(R.id.heng_xiang_recycler); tui_jian_recycler = view.findViewById(R.id.tui_jian_recycler); marqueeView = (MarqueeView) view.findViewById(R.id.marqueeView); miao_sha_recycler = view.findViewById(R.id.miao_sha_recycler); smart_refresh = view.findViewById(R.id.smart_refresh); linear_include = view.findViewById(R.id.linear_include); observe_scroll_view = view.findViewById(R.id.observe_scroll_view); sao_yi_sao = view.findViewById(R.id.sao_yi_sao); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ChenJinUtil.setStatusBarColor(getActivity(), Color.TRANSPARENT); initTitleBar(); fragmentHomeP = new FragmentHomeP(this); fragmentHomeP.getNetData(ApiUtil.HOME_URL); fragmentHomeP.getFenLeiData(ApiUtil.FEN_LEI_URL); initBanner(); initMarqueeView(); smart_refresh.setOnLoadmoreListener(new OnLoadmoreListener() { @Override public void onLoadmore(RefreshLayout refreshlayout) { smart_refresh.finishLoadmore(2000); } }); smart_refresh.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(RefreshLayout refreshlayout) { smart_refresh.finishRefresh(2000); } }); sao_yi_sao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent= new Intent(getActivity(), CustomCaptrueActivity.class); startActivityForResult(intent, 1001); } }); } private void initTitleBar() { addMargin(); ViewTreeObserver viewTreeObserver = linear_include.getViewTreeObserver(); viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { linear_include.getViewTreeObserver().removeGlobalOnLayoutListener(this); final int height = linear_include.getHeight(); observe_scroll_view.setScrollViewListener(new ObservableScrollView.IScrollViewListener() { @Override public void onScrollChanged(int x, int y, int oldx, int oldy) { if (y <= 0) { addMargin(); ChenJinUtil.setStatusBarColor(getActivity(),Color.TRANSPARENT); linear_include.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));//AGB由相关工具获得,或 } else if (y > 0 && y < height) { if (y > ChenJinUtil.getStatusBarHeight(getActivity())) { removeMargin(); ChenJinUtil.setStatusBarColor(getActivity(),getResources().getColor(R.color.colorPrimaryDark)); } float scale = (float) y / height; float alpha = (255 * scale); linear_include.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26)); } else { linear_include.setBackgroundColor(Color.argb((int) 255, 227, 29, 26)); } } }); } }); } private void removeMargin() { if (TAG_MARGIN_ADDED.equals(linear_include.getTag())) { RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) linear_include.getLayoutParams(); params.topMargin -= ChenJinUtil.getStatusBarHeight(getActivity()); linear_include.setLayoutParams(params); linear_include.setTag(null); } } private void addMargin() { if (!TAG_MARGIN_ADDED.equals(linear_include.getTag())) { RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) linear_include.getLayoutParams(); params.topMargin += ChenJinUtil.getStatusBarHeight(getActivity()); linear_include.setLayoutParams(params); linear_include.setTag(TAG_MARGIN_ADDED); } } private void initMarqueeView() { List<String> info = new ArrayList<>(); info.add("欢迎访问京东app"); info.add("大家有没有在 听课"); info.add("是不是还有人在睡觉"); info.add("你妈妈在旁边看着呢"); info.add("赶紧的好好学习吧 马上毕业了"); info.add("你没有事件睡觉了"); marqueeView.startWithList(info); } private void initBanner() { banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR); banner.setImageLoader(new GlideImageLoader()); banner.isAutoPlay(true); banner.setDelayTime(2500); banner.setIndicatorGravity(BannerConfig.CENTER); } @Override public void onHiddenChanged(boolean hidden) { super.onHiddenChanged(hidden); if (! hidden) { if (observe_scroll_view.getScrollY() > ChenJinUtil.getStatusBarHeight(getActivity())) { removeMargin(); ChenJinUtil.setStatusBarColor(getActivity(),getResources().getColor(R.color.colorPrimaryDark)); }else { addMargin(); ChenJinUtil.setStatusBarColor(getActivity(),Color.TRANSPARENT); } } } @Override public void onSuccess(final HomeBean homeBean) { List<HomeBean.DataBean> datas = homeBean.getData(); List<String> imageUrls = new ArrayList<>(); for (int i = 0;i<datas.size();i++){ imageUrls.add(datas.get(i).getIcon()); } banner.setImages(imageUrls); banner.setOnBannerListener(new OnBannerListener() { @Override public void OnBannerClick(int position) { HomeBean.DataBean dataBean = homeBean.getData().get(position); if (dataBean.getType() == 0) { Intent intent = new Intent(getActivity(),WebViewActivity.class); intent.putExtra("detailUrl",dataBean.getUrl()); startActivity(intent); }else { Toast.makeText(getActivity(),"即将跳转商品详情",Toast.LENGTH_SHORT).show(); } } }); banner.start(); miao_sha_recycler.setLayoutManager(new LinearLayoutManager(getActivity(), OrientationHelper.HORIZONTAL,false)); MiaoShaAdapter miaoShaAdapter = new MiaoShaAdapter(getActivity(), homeBean.getMiaosha()); miao_sha_recycler.setAdapter(miaoShaAdapter); miaoShaAdapter.setOnItemListner(new OnItemListner() { @Override public void onItemClick(int position) { Intent intent = new Intent(getActivity(), WebViewActivity.class); String detailUrl = homeBean.getMiaosha().getList().get(position).getDetailUrl(); intent.putExtra("detailUrl",detailUrl); startActivity(intent); } @Override public void onItemLongClick(int position) { } }); tui_jian_recycler.setLayoutManager(new StaggeredGridLayoutManager(2,OrientationHelper.VERTICAL)); TuiJianAdapter tuiJianAdapter = new TuiJianAdapter(getActivity(), homeBean.getTuijian()); tui_jian_recycler.setAdapter(tuiJianAdapter); tuiJianAdapter.setOnItemListner(new OnItemListner() { @Override public void onItemClick(int position) { Intent intent = new Intent(getActivity(), DetailActivity.class); intent.putExtra("pid",homeBean.getTuijian().getList().get(position).getPid()); startActivity(intent); } @Override public void onItemLongClick(int position) { } }); } @Override public void onFenLeiDataSuccess(FenLeiBean fenLeiBean) { heng_xiang.setLayoutManager(new GridLayoutManager(getActivity(),2, OrientationHelper.HORIZONTAL,false)); HengXiangAdapter hengXiangAdapter = new HengXiangAdapter(getActivity(), fenLeiBean); heng_xiang.setAdapter(hengXiangAdapter); hengXiangAdapter.setOnItemListner(new OnItemListner() { @Override public void onItemClick(int position) { Toast.makeText(getActivity(),"点击事件执行",Toast.LENGTH_SHORT).show(); } @Override public void onItemLongClick(int position) { Toast.makeText(getActivity(),"长按事件执行",Toast.LENGTH_SHORT).show(); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1001) { if (null != data) { Bundle bundle = data.getExtras(); if (bundle == null) { return; } if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) { String result = bundle.getString(CodeUtils.RESULT_STRING); if (result.startsWith("http://")) { Intent intent = new Intent(getActivity(),WebViewActivity.class); intent.putExtra("detailUrl",result); startActivity(intent); }else { Toast.makeText(getActivity(), "暂不支持此二维码", Toast.LENGTH_LONG).show(); } } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) { Toast.makeText(getActivity(), "解析二维码失败", Toast.LENGTH_LONG).show(); } } } } }
布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="#F5F5F5" android:layout_width="match_parent" android:layout_height="match_parent"> <com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/smart_refresh" android:layout_width="match_parent" android:layout_height="match_parent"> <com.bwie.zhangjunjingdong.view.custom.ObservableScrollView android:id="@+id/observe_scroll_view" android:scrollbars="none" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.youth.banner.Banner xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="150dp" /> <ImageView android:scaleType="fitXY" android:src="@drawable/timg" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v7.widget.RecyclerView android:id="@+id/heng_xiang_recycler" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> <LinearLayout android:padding="10dp" android:gravity="center_vertical" android:layout_margin="10dp" android:orientation="horizontal" android:background="@drawable/white_back_conner" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="京东快报" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <com.sunfusheng.marqueeview.MarqueeView android:id="@+id/marqueeView" android:layout_marginLeft="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" app:mvAnimDuration="1000" app:mvDirection="bottom_to_top" app:mvInterval="3000" app:mvTextColor="#000000" app:mvTextSize="14sp" app:mvSingleLine="true"/> <View android:layout_width="0.5dp" android:layout_marginTop="2dp" android:layout_marginBottom="2dp" android:background="#000000" android:layout_height="match_parent"/> <TextView android:layout_marginLeft="5dp" android:text="更多" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:id="@+id/miao_sha_recycler" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:textSize="18sp" android:gravity="center" android:padding="5dp" android:background="#66000000" android:textColor="#ff0000" android:text="为你推荐" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v7.widget.RecyclerView android:nestedScrollingEnabled="false" android:id="@+id/tui_jian_recycler" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </LinearLayout> </LinearLayout> </com.bwie.zhangjunjingdong.view.custom.ObservableScrollView> </com.scwang.smartrefresh.layout.SmartRefreshLayout> <include layout="@layout/sao_miao_layout" /> </RelativeLayout>头部布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:gravity="center_vertical" android:id="@+id/linear_include" android:padding="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/sao_yi_sao" android:orientation="vertical" android:gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:src="@drawable/a_s" android:layout_width="20dp" android:layout_height="20dp" /> <TextView android:layout_marginTop="2dp" android:textColor="#ffffff" android:text="扫一扫" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@drawable/white_back_conner_02" android:gravity="center_vertical" android:orientation="horizontal" android:layout_weight="1" android:layout_width="0dp" android:layout_height="30dp"> </LinearLayout> <LinearLayout android:orientation="vertical" android:gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:src="@drawable/a9x" android:layout_width="20dp" android:layout_height="20dp" /> <TextView android:layout_marginTop="2dp" android:textColor="#ffffff" android:text="消息" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> p
package com.bwie.zhangjunjingdong.presenter; import com.bwie.zhangjunjingdong.model.FragmentHomeModel; import com.bwie.zhangjunjingdong.model.bean.FenLeiBean; import com.bwie.zhangjunjingdong.model.bean.HomeBean; import com.bwie.zhangjunjingdong.presenter.inter.FragmentHomePInter; import com.bwie.zhangjunjingdong.view.Iview.InterFragmentHome; public class FragmentHomeP implements FragmentHomePInter { private FragmentHomeModel fragmentHomeModel; private InterFragmentHome interFragmentHome; public FragmentHomeP(InterFragmentHome interFragmentHome) { this.interFragmentHome = interFragmentHome; fragmentHomeModel = new FragmentHomeModel(this); } public void getNetData(String homeUrl) { fragmentHomeModel.getData(homeUrl); } @Override public void onSuccess(HomeBean homeBean) { interFragmentHome.onSuccess(homeBean); } @Override public void onFenLeiDataSuccess(FenLeiBean fenLeiBean) { interFragmentHome.onFenLeiDataSuccess(fenLeiBean); } public void getFenLeiData(String fenLeiUrl) { fragmentHomeModel.getFenLeiData(fenLeiUrl); } }
m
package com.bwie.zhangjunjingdong.model; import com.bwie.zhangjunjingdong.model.bean.FenLeiBean; import com.bwie.zhangjunjingdong.model.bean.HomeBean; import com.bwie.zhangjunjingdong.presenter.inter.FragmentHomePInter; import com.bwie.zhangjunjingdong.util.CommonUtils; import com.bwie.zhangjunjingdong.util.OkHttp3Util_03; import com.google.gson.Gson; import java.io.IOException; import java.util.HashMap; import java.util.Map; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; public class FragmentHomeModel { private FragmentHomePInter fragmentHomePInter; public FragmentHomeModel(FragmentHomePInter fragmentHomePInter) { this.fragmentHomePInter = fragmentHomePInter; } public void getData(String homeUrl) { Map<String, String> params = new HashMap<>(); OkHttp3Util_03.doPost(homeUrl, params, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { String json = response.body().string(); final HomeBean homeBean = new Gson().fromJson(json, HomeBean.class); CommonUtils.runOnUIThread(new Runnable() { @Override public void run() { fragmentHomePInter.onSuccess(homeBean); } }); } } }); } public void getFenLeiData(String fenLeiUrl) { Map<String, String> params = new HashMap<>(); OkHttp3Util_03.doPost(fenLeiUrl, params, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { String json = response.body().string(); final FenLeiBean fenLeiBean = new Gson().fromJson(json, FenLeiBean.class); CommonUtils.runOnUIThread(new Runnable() { @Override public void run() { fragmentHomePInter.onFenLeiDataSuccess(fenLeiBean); } }); } } }); } }