alibaba / vlayout

Project vlayout is a powerfull LayoutManager extension for RecyclerView, it provides a group of layouts for RecyclerView. Make it able to handle a complicate situation when grid, list and other layouts in the same recyclerview.
http://tangram.pingguohe.net/
MIT License
10.79k stars 1.79k forks source link

使用vlayout为什么图片显示不正常,滑动有的时候不显示 #142

Open douya40 opened 7 years ago

douya40 commented 7 years ago

使用vlayout为什么图片显示不正常,滑动有的时候不显示

longerian commented 7 years ago

这个应该是图片库的问题,

douya40 commented 7 years ago
        Glide.with(context).load(url).crossFade().placeholder(R.color.tpbj).error(R.color.tpbj).into(iv);

不应该呀!用的Glide

douya40 commented 7 years ago

上下滑动的时候,本来显示的图片又不显示了

douya40 commented 7 years ago

能帮忙看看吗

douya40 commented 7 years ago
package com.laima365.mjmk.ui.fragment.first;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.RecyclablePagerAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.laima365.mjmk.R;
import com.laima365.mjmk.ui.event.StartBrotherEvent;
import com.laima365.mjmk.ui.fragment.BaseMainFragment;
import com.laima365.mjmk.ui.fragment.MainFragment;
import com.laima365.mjmk.ui.model.ShopMeiBaoHomePage;
import com.laima365.mjmk.ui.model.SyMbList;
import com.laima365.mjmk.ui.model.UserLogin;
import com.laima365.mjmk.ui.nohttp.FastJsonRequest;
import com.laima365.mjmk.ui.nohttp.HttpListener;
import com.laima365.mjmk.ui.utils.Constants;
import com.laima365.mjmk.ui.utils.GlideImgManager;
import com.laima365.mjmk.ui.utils.ToastUtils;
import com.laima365.mjmk.ui.utils.Utils;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.titlebar.BGATitleBar;

/**
 */
public class FirstTab2Fragment extends BaseMainFragment implements HttpListener<JSONObject> {

    private static final boolean BANNER_LAYOUT = true;
    private static final boolean GRID_LAYOUT = true;
    private static final boolean LINEAR_LAYOUT1 = true;
    private static final boolean LINEAR_LAYOUT2 = true;
    private static final boolean LINEAR_LAYOUT3 = true;

    Unbinder unbinder;
    @BindView(R.id.titlebar)
    BGATitleBar mTitleBar;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    private  BaseQuickAdapter baseQuickAdapter;
    private  List<String> datalist,datalist3;
    private  List<DelegateAdapter.Adapter> delegtalist;
    private    DelegateAdapter delegateAdapter;
    private   RecyclerView.RecycledViewPool viewPool;
    private int[] imgs = {
            R.drawable.sygd,
            R.drawable.sygd,
            R.drawable.hmjbj,
            R.drawable.sygd
    };
    public static FirstTab2Fragment newInstance() {
        Bundle args = new Bundle();
        FirstTab2Fragment fragment = new FirstTab2Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.firsttabfragment2, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

          initVlayout();
    }

    private void showView(final ShopMeiBaoHomePage.DataBeanXXXXX shopdata) {
        if(BANNER_LAYOUT){
            delegtalist.add(new SubAdapter(_mActivity, new LinearLayoutHelper(), 1) {
                @Override
                public void onViewRecycled(MainViewHolder holder) {
                    if (holder.itemView instanceof RollPagerView) {
                        ((RollPagerView) holder.itemView).setAdapter(null);
                    }
                }

                @Override
                public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    if (viewType == 1)
                        return new MainViewHolder(
                                LayoutInflater.from(_mActivity).inflate(R.layout.view_pager, parent, false));

                    return super.onCreateViewHolder(parent, viewType);
                }

                @Override
                public int getItemViewType(int position) {
                    return 1;
                }

                @Override
                protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

                }

                @Override
                public void onBindViewHolder(MainViewHolder holder, int position) {
                    if (holder.itemView instanceof RollPagerView) {
                        RollPagerView viewPager = (RollPagerView) holder.itemView;

                        viewPager.setLayoutParams(new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400));
                        viewPager.setAdapter(new PagerAdapter(_mActivity,this, viewPool,shopdata.getAdvertising().getData()));

                    }
                }
            });
        }

        if (GRID_LAYOUT) {
            GridLayoutHelper layoutHelper;
            layoutHelper = new GridLayoutHelper(4);
            delegtalist.add(new SubAdapter2(_mActivity, layoutHelper, shopdata.getCategory().getData()));
        }

        if(LINEAR_LAYOUT1){
            List<SyMbList> smliat=new ArrayList<>();
            SyMbList aa=new SyMbList();
            List<SyMbList.DataBean>   sy1=new ArrayList<>();
            List<SyMbList.DataBean>   sy2=new ArrayList<>();
            List<SyMbList.DataBean>   sy3=new ArrayList<>();

            for(int i=0;i<shopdata.getDaily().getData().size();i++){
                SyMbList.DataBean aym=new SyMbList.DataBean();
                aym.setIcon(shopdata.getDaily().getData().get(i).getIcon());
                aym.setId(shopdata.getDaily().getData().get(i).getId());
                aym.setName(shopdata.getDaily().getData().get(i).getName());
                aym.setMeibao(shopdata.getDaily().getData().get(i).getMeibao());
                sy1.add(aym);
                aa.setData(sy1);
            }
            smliat.add(aa);
            for(int i=0;i<shopdata.getHotsale().getData().size();i++){
                SyMbList.DataBean aym=new SyMbList.DataBean();
                aym.setIcon(shopdata.getDaily().getData().get(i).getIcon());
                aym.setId(shopdata.getDaily().getData().get(i).getId());
                aym.setName(shopdata.getDaily().getData().get(i).getName());
                aym.setMeibao(shopdata.getDaily().getData().get(i).getMeibao());
                sy2.add(aym);
                aa.setData(sy2);
            }
            smliat.add(aa);
            for(int i=0;i<shopdata.getPreference().getData().size();i++){
                SyMbList.DataBean aym=new SyMbList.DataBean();
                aym.setIcon(shopdata.getDaily().getData().get(i).getIcon());
                aym.setId(shopdata.getDaily().getData().get(i).getId());
                aym.setName(shopdata.getDaily().getData().get(i).getName());
                aym.setMeibao(shopdata.getDaily().getData().get(i).getMeibao());
                sy3.add(aym);
                aa.setData(sy3);
            }
            smliat.add(aa);

            LinearLayoutHelper layoutHelper = new LinearLayoutHelper();
            delegtalist.add(new SubAdapter3(_mActivity, layoutHelper,smliat));
        }

//        if(LINEAR_LAYOUT1){
//            LinearLayoutHelper layoutHelper = new LinearLayoutHelper();
//            delegtalist.add(new SubAdapter3(_mActivity, layoutHelper,shopdata.getDaily()));
//        }
//
//        if(LINEAR_LAYOUT1){
//            LinearLayoutHelper layoutHelper = new LinearLayoutHelper();
//            delegtalist.add(new SubAdapter3(_mActivity, layoutHelper,shopdata.getDaily()));
//        }

//        if(LINEAR_LAYOUT1){
//            LinearLayoutHelper layoutHelper = new LinearLayoutHelper();
//            datalist3 = new ArrayList<>();
//            datalist3.add("食品");
//            datalist3.add("箱包");
//            delegtalist.add(new SubAdapter3(_mActivity, layoutHelper,datalist3));
//        }
//
//        if(LINEAR_LAYOUT1){
//            LinearLayoutHelper layoutHelper = new LinearLayoutHelper();
//            datalist3 = new ArrayList<>();
//            datalist3.add("食品");
//            datalist3.add("箱包");
//            datalist3.add("百货");
//            delegtalist.add(new SubAdapter3(_mActivity, layoutHelper,datalist3));
//        }

        delegateAdapter.setAdapters(delegtalist);
    }

    private void initVlayout() {
        final VirtualLayoutManager layoutManager = new VirtualLayoutManager(_mActivity);
        recyclerview.setLayoutManager(layoutManager);
        viewPool = new RecyclerView.RecycledViewPool();
        recyclerview.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 10);

         delegateAdapter = new DelegateAdapter(layoutManager, false);
         recyclerview.setAdapter(delegateAdapter);
        delegtalist = new LinkedList<>();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        shop_meibaoHomePage();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    static class SubAdapter extends DelegateAdapter.Adapter<MainViewHolder> {
        private Context mContext;
        private LayoutHelper mLayoutHelper;
        private VirtualLayoutManager.LayoutParams mLayoutParams;
        private int mCount = 0;

        public SubAdapter(Context context, LayoutHelper layoutHelper, int count) {
            this(context, layoutHelper, count, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300));
        }

        public SubAdapter(Context context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
            this.mContext = context;
            this.mLayoutHelper = layoutHelper;
            this.mCount = count;
            this.mLayoutParams = layoutParams;
        }

        @Override
        public LayoutHelper onCreateLayoutHelper() {
            return mLayoutHelper;
        }

        @Override
        public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item, parent, false));
        }

        @Override
        public void onBindViewHolder(MainViewHolder holder, int position) {
            // only vertical
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        @Override
        protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {
//            ((RollPagerView) holder.itemView.findViewById(R.id.rollpagerview)).setText(Integer.toString(offsetTotal));
        }

        @Override
        public int getItemCount() {
            return mCount;
        }
    }

//
//    static class MyViewHolder extends RecyclerView.ViewHolder {
//
//        TextView tv;
//
//        public MyViewHolder(View view) {
//            super(view);
//            tv = (TextView) view.findViewById(R.id.id_num);
//        }
//    }

    static class MainViewHolder extends RecyclerView.ViewHolder {

        public static volatile int existing = 0;
        public static int createdTimes = 0;

        public MainViewHolder(View itemView) {
            super(itemView);
            createdTimes++;
            existing++;
        }

        @Override
        protected void finalize() throws Throwable {
            existing--;
            super.finalize();
        }
    }

    static class SubAdapter2 extends DelegateAdapter.Adapter<MainViewHolder> {
        private Context mContext;
        private LayoutHelper mLayoutHelper;
        private VirtualLayoutManager.LayoutParams mLayoutParams;
        private  List<ShopMeiBaoHomePage.DataBeanXXXXX.CategoryBean.DataBeanX> datalist ;

        public SubAdapter2(Context context, LayoutHelper layoutHelper, List<ShopMeiBaoHomePage.DataBeanXXXXX.CategoryBean.DataBeanX> datalist) {
            this(context, layoutHelper, datalist, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }

        public SubAdapter2(Context context, LayoutHelper layoutHelper,  List<ShopMeiBaoHomePage.DataBeanXXXXX.CategoryBean.DataBeanX> datalist, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
            this.mContext = context;
            this.mLayoutHelper = layoutHelper;
            this.mLayoutParams = layoutParams;
            this.datalist=datalist;
        }

        @Override
        public LayoutHelper onCreateLayoutHelper() {
            return mLayoutHelper;
        }

        @Override
        public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.grid_item, parent, false));
        }

        @Override
        public void onBindViewHolder(MainViewHolder holder, int position) {
            // only vertical
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));

                    ((RelativeLayout) holder.itemView.findViewById(R.id.item_relayout)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            EventBus.getDefault().post(new StartBrotherEvent(ShopListFragment.newInstance()));
                        }
                    });

            ((TextView) holder.itemView.findViewById(R.id.tv_item)).setText(datalist.get(position).getName());

            GlideImgManager.loadImage(mContext,datalist.get(position).getIcon() , ((ImageView) holder.itemView.findViewById(R.id.iv_item)));

//            if (position == 0) {
//                ((ImageView) holder.itemView.findViewById(R.id.iv_item)).setImageResource(R.drawable.food);
//            } else if (position == 1) {
//                ((ImageView) holder.itemView.findViewById(R.id.iv_item)).setImageResource(R.drawable.box);
//            } else if (position == 2) {
//                ((ImageView) holder.itemView.findViewById(R.id.iv_item)).setImageResource(R.drawable.shoop);
//            } else if (position == 3) {
//                ((ImageView) holder.itemView.findViewById(R.id.iv_item)).setImageResource(R.drawable.nvzhuang);
//            } else if (position == 4) {
//                ((ImageView) holder.itemView.findViewById(R.id.iv_item)).setImageResource(R.drawable.shuma);
//            } else if (position == 5) {
//                ((ImageView) holder.itemView.findViewById(R.id.iv_item)).setImageResource(R.drawable.jiaju);
//            } else if (position == 6) {
//                ((ImageView) holder.itemView.findViewById(R.id.iv_item)).setImageResource(R.drawable.xiemao);
//            } else if (position== 7) {
//                ((ImageView) holder.itemView.findViewById(R.id.iv_item)).setImageResource(R.drawable.all);
//            }
        }

        @Override
        protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {
//            ((ImageView) holder.itemView.findViewById(R.id.iv_item)).setImageResource(R.drawable.food);
//            ((TextView) holder.itemView.findViewById(R.id.tv_item)).setText(Integer.toString(offsetTotal));
        }

        @Override
        public int getItemCount() {
            return datalist.size();
        }
    }
    static class SubAdapter3 extends DelegateAdapter.Adapter<MainViewHolder> {
        private Context mContext;
        private LayoutHelper mLayoutHelper;
        private VirtualLayoutManager.LayoutParams mLayoutParams;
        private  List<SyMbList>    datalist;

        public SubAdapter3(Context context, LayoutHelper layoutHelper, List<SyMbList> datalist  ) {
            this(context, layoutHelper, datalist, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }

        public SubAdapter3(Context context, LayoutHelper layoutHelper, List<SyMbList> datalist, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
            this.mContext = context;
            this.mLayoutHelper = layoutHelper;
            this.mLayoutParams = layoutParams;
            this.datalist=datalist;
        }

        @Override
        public LayoutHelper onCreateLayoutHelper() {
            return mLayoutHelper;
        }

        @Override
        public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item3, parent, false));
        }

        @Override
        public void onBindViewHolder(MainViewHolder holder,final int position) {
            // only vertical
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));

                if(position==0){
                    ((TextView) holder.itemView.findViewById(R.id.syhdbqtv)).setText("每日特价");
                }else if(position==1){
                    ((TextView) holder.itemView.findViewById(R.id.syhdbqtv)).setText("热销单品");
                }else{
                    ((TextView) holder.itemView.findViewById(R.id.syhdbqtv)).setText("优选清单");
                }

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            ((RecyclerView) holder.itemView.findViewById(R.id.tjsprecycleview)).setLayoutManager(linearLayoutManager);
            BaseQuickAdapter   baseQuickAdapter2 = new BaseQuickAdapter<SyMbList.DataBean,BaseViewHolder>(R.layout.tjshpxq_item, datalist.get(position).getData()) {
                @Override
                protected void convert(final BaseViewHolder holder,final SyMbList.DataBean data) {
                    GlideImgManager.loadImage(mContext,data.getIcon() , ((ImageView) holder.itemView.findViewById(R.id.iv_item)));
                    ((TextView) holder.itemView.findViewById(R.id.tv_item)).setText(data.getName());
                    ((TextView) holder.itemView.findViewById(R.id.tv_mbdh)).setText(data.getMeibao());

                }
            };
            ((RecyclerView) holder.itemView.findViewById(R.id.tjsprecycleview)).setAdapter(baseQuickAdapter2);

        }

        @Override
        protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {
//            ((ImageView) holder.itemView.findViewById(R.id.iv_item)).setImageResource(R.drawable.food);
//            ((TextView) holder.itemView.findViewById(R.id.tv_item)).setText(Integer.toString(offsetTotal));
        }

        @Override
        public int getItemCount() {
            return datalist.size();
        }
    }

    static class PagerAdapter extends RecyclablePagerAdapter<MainViewHolder> {
        private List<ShopMeiBaoHomePage.DataBeanXXXXX.AdvertisingBean.DataBean> advertisingBean;
        private Context _context;

        public PagerAdapter(Context context, SubAdapter adapter, RecyclerView.RecycledViewPool pool, List<ShopMeiBaoHomePage.DataBeanXXXXX.AdvertisingBean.DataBean> advertisingBean) {
            super(adapter, pool);
            this._context=context;
            this.advertisingBean=advertisingBean;
        }

        @Override
        public int getCount() {
            return advertisingBean.size();
        }

        @Override
        public void onBindViewHolder(MainViewHolder viewHolder, int position) {
            // only vertical
            viewHolder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            GlideImgManager.loadImage(_context,advertisingBean.get(position).getPath(),((ImageView) viewHolder.itemView.findViewById(R.id.image)));

        }

        @Override
        public int getItemViewType(int position) {
            return 0;
        }
    }

    public void shop_meibaoHomePage() {
        Request<com.alibaba.fastjson.JSONObject> request = new FastJsonRequest(Constants.API.USER_REGISTER, RequestMethod.POST);
        request.add("service", "shop_meibaoHomePage");
        request.add("cipherText","{\"advertNumber\":\"3\",\"categoryNumber\":\"8\",\"dailyNumber\":\"3\",\"hotsaleNumber\":\"3\",\"preferenceNumber\":\"3\"}");
        request(SHOPMEIBAOHOMEPAGE, request, this, false, true);
    }

    @Override
    public void onSucceed(int what, Response<com.alibaba.fastjson.JSONObject> response) {
        try {
            if(what==SHOPMEIBAOHOMEPAGE){
                ShopMeiBaoHomePage info = JSON.parseObject(response.get().toString(), ShopMeiBaoHomePage.class);
                if(info.getCode().equals(Constants.SUCCEED)){
                    showView(info.getData());
                }else{
                    ToastUtils.show(info.getMsg());
                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onFailed(int what, Response<com.alibaba.fastjson.JSONObject> response) {

    }
}
douya40 commented 7 years ago

能帮忙看看问题不,第一次使用vlayout,也看不出什么问题

longerian commented 7 years ago

之前有人反馈用glide不能加载图片,用picasso是可以的。 至于glide,没有研究过其原理,还不知道哪里有冲突。

shenxiaolin commented 7 years ago

那岂不是为了用这个Vlayout ,要将图片加载库先替换了??

longerian commented 7 years ago

代码没看出来有什么明显的不妥,可以先缩小一下问题的范围,比如替换图片库、图片是否下载成功、加载本地图片等等,排除一下。

blesslp commented 7 years ago

Glide这个问题我也遇到过,没找到解决办法,首次拉进屏幕的时候图片不显示,上下再拉一下才显示出来

longerian commented 7 years ago

我之前写Demo也用Glide试过,没测出来问题,所以需要各位结合业务代码再排查排查问题,缩小一下问题的范围。

xuluming commented 7 years ago

@longerian 我也碰到了glide 冲突的问题,滚动列表会跳动,希望之后能更正更正,谢谢