admin
2021-05-11 e5ca87e89fef96c827ec37d1d91082f626cbb17d
app/src/main/java/com/tejia/lijin/app/ui/recommend/RecommendFragment.java
@@ -5,22 +5,14 @@
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
@@ -40,18 +32,8 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzj.gallery.library.views.BannerViewPager;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.viewpagerindicator.LinePageIndicator;
import com.wpc.library.RetainViewFragment;
import com.wpc.library.content.ConnectivityChangeHelper;
import com.wpc.library.util.NetUtils;
import com.wpc.library.util.SystemCommon;
import com.wpc.library.util.cache.DiskLruCache;
import com.wpc.library.util.common.DimenUtils;
import com.wpc.library.util.common.StorageUtils;
import com.wpc.library.util.common.StringUtils;
import com.wpc.library.widget.MyGridView;
import com.wpc.library.widget.RatioLayout;
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.ShoppingApi;
@@ -68,8 +50,19 @@
import com.tejia.lijin.app.ui.timeLimitedSpike.TimeLimitedSpikeActivity;
import com.tejia.lijin.app.util.FlowLayout;
import com.tejia.lijin.app.util.JumpActivityUtil;
import com.tejia.lijin.app.util.ui.HomeUIUtil;
import com.tejia.lijin.app.util.umengCustomEvent.MainCustomEvent;
import com.tejia.lijin.app.util.user.UserUtil;
import com.wpc.library.RetainViewFragment;
import com.wpc.library.content.ConnectivityChangeHelper;
import com.wpc.library.util.NetUtils;
import com.wpc.library.util.SystemCommon;
import com.wpc.library.util.cache.DiskLruCache;
import com.wpc.library.util.common.DimenUtils;
import com.wpc.library.util.common.StorageUtils;
import com.wpc.library.util.common.StringUtils;
import com.wpc.library.widget.MyGridView;
import com.wpc.library.widget.RatioLayout;
import org.apache.http.Header;
import org.json.JSONException;
@@ -82,6 +75,15 @@
import java.util.Iterator;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import static android.content.Context.MODE_PRIVATE;
/**
@@ -90,26 +92,22 @@
 */
public class RecommendFragment extends RetainViewFragment implements View.OnClickListener {
    //    private RecyclerView rv_first_page, rv_special_offer;
    private LinearLayout ll_fragment_activity, ll_special_offer, ll_limit_time_buy, ll_jinbi_exchange_buy;
    private MyGridView gv_limit_time_goods, gv_jinbi_exchange_goods;
    private SwipeRefreshLayout rl_first_page;
    private ImageView iv_activity_bg, iv_slid_top, iv_input;
    //    private MyScrollView sv_content;
    private FrameLayout fl_fragment_activity;
    private RatioLayout rl_recommend_activity2;
    //    private TextView tv_recommend_goods_hint;
    private RecyclerView rv_recommend_goods, rv_special_offer;
    private View v_divider;
    private FlowLayout flow_hot;
    private RatioLayout rl_top_banner;
    ScrollView sv_hot;
    private View bottom;
    private ImageView iv_loading, iv_hot_img;
    private TextView tv_loading;
    ViewPager vp_banner, vp_recommend_activity2;
    private LinePageIndicator indicator_recommend;
    com.lzj.gallery.library.views.BannerViewPager vp_banner;
    ViewPager vp_recommend_activity2;
    private LinearLayout ll_no_net, ll_no_data, ll_request_failture;
    private List<HomeBanner> mBanners = new ArrayList<>();
@@ -120,9 +118,8 @@
    private List<String> lList = new ArrayList<>();
    private List<String> lList2 = new ArrayList<>();
    private RecommendTopAdapter bannerAdapter;//banner  Adapter
    private RecommendActivityAdapter activityAdapter;
    private HotCategoryAdapter specialAdapter2;
    private RecommendCircleSpecialAdapter specialAdapter2;
    private RecommendGoodsAdapter goodsAdapter;//精选商品 Adapter
    private LimitTimeBuyAdapter limitTimeBuyAdapter, jinbiExchangeAdapter;
    private DiskLruCache cache;
@@ -201,13 +198,53 @@
    int visibilyNum = -1;
    RecyclerView.ItemDecoration circleSpecialItemDecoration = new RecyclerView.ItemDecoration() {
        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
        }
        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDrawOver(c, parent, state);
        }
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            int index = parent.getChildAdapterPosition(view);
            int total = parent.getAdapter().getItemCount();
            int minWidth = DimenUtils.dipToPixels(6, view.getContext());
            if (index % 4 == 0) {
                outRect.left = 0; //第一列左边贴边
                outRect.right = minWidth * 3;
            } else if (index % 4 == 1) {
                outRect.left = minWidth * 1;
                outRect.right = minWidth * 2;
            } else if (index % 4 == 2) {
                outRect.left = minWidth * 2;
                outRect.right = minWidth * 1;
            } else {
                outRect.right = 0; //第一列左边贴边
                outRect.left = minWidth * 3;
            }
            int lastRow = total / 4;
            if (lastRow != index / 4) {
                outRect.bottom = minWidth * 4;
            }
        }
    };
    private void init(View contentView) {
        rv_recommend_goods = contentView.findViewById(R.id.rv_recommend_goods);
        rl_first_page = contentView.findViewById(R.id.rl_first_page);
        iv_slid_top = contentView.findViewById(R.id.iv_slid_top);
        final View top = LayoutInflater.from(rv_recommend_goods.getContext())
                .inflate(R.layout.item_recommend_top, null);
        rl_top_banner = top.findViewById(R.id.rl_top_banner);
        top.setBackground(HomeUIUtil.getHomeTopBg("#FF227B", "#FF2A3E"));
        ll_fragment_activity = top.findViewById(R.id.ll_fragment_activity);
        ll_special_offer = top.findViewById(R.id.ll_special_offer);
        ll_limit_time_buy = top.findViewById(R.id.ll_limit_time_buy);
@@ -215,13 +252,11 @@
        fl_fragment_activity = top.findViewById(R.id.fl_fragment_activity);
        rl_recommend_activity2 = top.findViewById(R.id.rl_recommend_activity2);
        iv_activity_bg = top.findViewById(R.id.iv_activity_bg);
        vp_banner = top.findViewById(R.id.vp_recommend);
        indicator_recommend = top.findViewById(R.id.indicator_recommend);
        vp_banner = top.findViewById(R.id.vp_banner);
        rv_special_offer = top.findViewById(R.id.rv_special_offer);
        flow_hot = top.findViewById(R.id.flow_hot);
        iv_input = top.findViewById(R.id.iv_input);
        vp_recommend_activity2 = top.findViewById(R.id.vp_recommend_activity2);
//        tv_recommend_goods_hint = top.findViewById(R.id.tv_recommend_goods_hint);
        v_divider = top.findViewById(R.id.v_divider);
        tv_hour_1 = top.findViewById(R.id.tv_hour_1);
        tv_hour_2 = top.findViewById(R.id.tv_hour_2);
@@ -323,16 +358,22 @@
        getAppPageNotification();
        //设置列表可以水平滑动,两行
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2, GridLayoutManager.HORIZONTAL, false);
//        mRecyclerView = new CustomeRecyclerView(this);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 4, GridLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        rv_special_offer.setLayoutManager(layoutManager);
        specialAdapter2 = new HotCategoryAdapter(getActivity(), circleSpecials);
        specialAdapter2 = new RecommendCircleSpecialAdapter(getActivity(), circleSpecials);
        rv_special_offer.setAdapter(specialAdapter2);
        seekBar = top.findViewById(R.id.slide_indicator_point);
        seekBar.setPadding(0, 0, 0, 0);
        seekBar.setThumbOffset(0);
        seekBar.setVisibility(View.GONE);
        rv_special_offer.addItemDecoration(circleSpecialItemDecoration);
        rv_special_offer.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
@@ -383,19 +424,49 @@
            }
        });
        vp_banner.setCurrentItem(0, true);
        vp_recommend_activity2.setCurrentItem(0, true);
        bannerAdapter = new RecommendTopAdapter(mBanners, getActivity(), false);
        vp_banner.setAdapter(bannerAdapter);
        activityAdapter = new RecommendActivityAdapter(mBanners2, getActivity());
        vp_recommend_activity2.setAdapter(activityAdapter);
        rv_recommend_goods.setLayoutManager(new LinearLayoutManager(getContext()));
        goodsAdapter = new RecommendGoodsAdapter(this, mList, "jingxuan");
        goodsAdapter = new RecommendGoodsAdapter(getContext(), mList, "jingxuan");
        rv_recommend_goods.setAdapter(goodsAdapter);
        goodsAdapter.addFooterView(bottom);
        goodsAdapter.addHeaderView(top);
        initTimeHandle();
        setBannerData();
    }
    private void setBannerData() {
        List<String> urlList = new ArrayList<>();
        for (HomeBanner ad : mBanners)
            urlList.add(ad.getSrc());
        if (urlList.size() > 0) {
            if (!vp_banner.hasAdapter()) {
                vp_banner.initBanner(urlList, false, 0.4375f)//开启3D画廊效果
                        .addPageMargin(10, 10)//参数1page之间的间距,参数2中间item距离边界的间距
                        .addStartTimer(3)//自动轮播3秒间隔
                        .addPointBottom(0)//底部间距
                        .addPoint(5, R.drawable.shape_banner_indicator_highlight, R.drawable.shape_banner_indicator)
                        .addDefaultImg(R.drawable.shape_banner_default)
                        .addRoundCorners(DimenUtils.dip2px(getContext(), 10))//圆角
                        .finishConfig()//这句必须加
                        .addBannerListener(new BannerViewPager.OnClickBannerListener() {
                            @Override
                            public void onBannerClick(int position) {
                                if (position >= mBanners.size())
                                    return;
                                JumpActivityUtil.jumpPage(getActivity(), mBanners.get(position).getJumpDetail(), mBanners.get(position).getParams());
                            }
                        });
            } else {
                vp_banner.notifyDataSetChanged();
            }
        }
    }
    private void addHot(final RecommendHot recommendHot) {
        if (recommendHot == null) {
@@ -609,22 +680,9 @@
                        if (mBanners.size() > 0) {
                            mBanners.clear();
                        }
                        float width = SystemCommon.getScreenWidth(getContext());
                        indicator_recommend.setLineWidth((width - (DimenUtils.dip2px(getContext(), 30))) / list1.size());
                        indicator_recommend.setGapWidth(0);
                        indicator_recommend.setSelectedColor(getResources().getColor(R.color.white));
                        indicator_recommend.setUnselectedColor(getResources().getColor(R.color.transport_white3));
                        indicator_recommend.setStrokeWidth(DimenUtils.dip2px(getContext(), 2));
                        if (list1 == null || list1.size() == 0) {
                            rl_top_banner.setVisibility(View.GONE);
                        } else {
                            rl_top_banner.setVisibility(View.VISIBLE);
                        }
                        mBanners.clear();
                        mBanners.addAll(list1);
                        bannerAdapter.notifyDataSetChanged();
                        setBannerData();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
@@ -779,27 +837,16 @@
    public void onResume() {
        super.onResume();
        mChangeHelper.registerReceiver();
        indicator_recommend.setViewPager(vp_banner);
        vp_banner.postDelayed(mAutoScroller, 2000);
        vp_recommend_activity2.postDelayed(mAutoScroller2, 2000);
        sv_hot.postDelayed(mAutoScroller3, 2000);
        scanSpecial(System.currentTimeMillis());
    }
    private void scanSpecial(Long curTime) {
        long stayTime1 = curTime - bannerAdapter.clickTime;
        long stayTime2 = curTime - specialAdapter2.clickTime;
        long stayTime3 = curTime - activityAdapter.clickTime;
        long stayTime4 = curTime - clickTime4;
        if (bannerAdapter.clickTime > 0 && stayTime1 > 15 * 1000) {
            PushEventData info = new PushEventData();
            info.setId(bannerAdapter.clickId);
            Gson gson = new GsonBuilder().serializeNulls().create();
            String data = gson.toJson(info);
            bannerAdapter.clickTime = 0;
            bannerAdapter.clickId = "";
        }
        if (specialAdapter2.clickTime > 0 && stayTime2 > 15 * 1000) {
            PushEventData info = new PushEventData();
            info.setId(specialAdapter2.clickId);
@@ -825,19 +872,6 @@
            clickId4 = "";
        }
    }
    private Runnable mAutoScroller = new Runnable() {
        @Override
        public void run() {
            PagerAdapter adapter = vp_banner.getAdapter();
            if (adapter != null && adapter.getCount() != 0) {
                vp_banner.setCurrentItem((vp_banner.getCurrentItem() + 1)
                        % adapter.getCount(), true);
            }
            vp_banner.postDelayed(this, 5000);
        }
    };
    private Runnable mAutoScroller2 = new Runnable() {
@@ -878,7 +912,6 @@
    public void onPause() {
        super.onPause();
        mChangeHelper.unregisterReceiver();
        vp_banner.removeCallbacks(mAutoScroller);
        vp_recommend_activity2.removeCallbacks(mAutoScroller2);
        sv_hot.removeCallbacks(mAutoScroller3);
    }
@@ -917,20 +950,8 @@
                    if (mBanners.size() > 0) {
                        mBanners.clear();
                    }
                    float width = SystemCommon.getScreenWidth(getContext());
                    indicator_recommend.setLineWidth((width - (DimenUtils.dip2px(getContext(), 30))) / list1.size());
                    indicator_recommend.setGapWidth(0);
                    indicator_recommend.setSelectedColor(getResources().getColor(R.color.white));
                    indicator_recommend.setUnselectedColor(getResources().getColor(R.color.transport_white3));
                    indicator_recommend.setStrokeWidth(DimenUtils.dip2px(getContext(), 2));
                    mBanners.addAll(list1);
                    bannerAdapter.notifyDataSetChanged();
                    if (list1 == null || list1.size() == 0) {
                        rl_top_banner.setVisibility(View.GONE);
                    } else {
                        rl_top_banner.setVisibility(View.VISIBLE);
                    }
                    setBannerData();
                    DiskLruCache.Editor editor0 = cache
                            .edit(getKey("BannerAd"));