如何使用RecyclerView在Glide中预加载?

时间:2017-04-28 23:03:46

标签: android view android-glide

我一直在尝试在我的recyclerView(片段)中使用预加载器,但出于某种原因我无法调用它。我的代码:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View ShipList = inflater.inflate(R.layout.ships, container, false);
        ((Homescreen)getActivity()).setActionBarTitle("Ships");
        ShipsDbHelper = new ShipsDbHelper(getActivity());

        shipView = (RecyclerView) ShipList.findViewById(R.id.ShipRec);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        shipView.setLayoutManager(layoutManager);
        shipView.setHasFixedSize(true);
        shipAdapter = new GreenAdapter();
        shipView.addItemDecoration(new DividerItemDecoration(getActivity()));
        shipView.addOnScrollListener(new ShipViewPreloader(,,10));
        shipView.setAdapter(shipAdapter);
        return ShipList;

    }

我想我需要在10之前加点东西,但我无法确定我需要的东西。

这也是我的Preloader:

public final class ShipViewPreloader<T> extends RecyclerView.OnScrollListener {
        private final recyclerShipListener recyclerShipListener;


        public ShipViewPreloader(ListPreloader.PreloadModelProvider<T> preloadModelProvider,
                                 ListPreloader.PreloadSizeProvider<T> preloadDimensionProvider, int maxPreload) {

            ListPreloader<T> listPreloader = new ListPreloader<>(preloadModelProvider,
                    preloadDimensionProvider, maxPreload);
            recyclerShipListener = new recyclerShipListener(listPreloader);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            recyclerShipListener.onScrolled(recyclerView, dx, dy);}
    }

我希望你们中的一个能帮助我,我是Glide的新手,发现文档并不像Androids文档那么好。

适配器:

public class GreenAdapter extends RecyclerView.Adapter<GreenAdapter.NumberViewHolder> {

    private final String TAG = GreenAdapter.class.getSimpleName();

    GreenAdapter() {
    }

    @Override
    public NumberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        Context context = viewGroup.getContext();
        int layoutIdForListItem;

            layoutIdForListItem = R.layout.ship;

        LayoutInflater inflater = LayoutInflater.from(context);
        boolean shouldAttachToParentImmediately = false;

        View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
        NumberViewHolder viewHolder = new NumberViewHolder(view);
        return viewHolder;
    }



    @Override
    public void onBindViewHolder(NumberViewHolder holder, int position) {
        holder.bind(position);
    }



    @Override
    public int getItemCount() {
        int i=99
    }


    class NumberViewHolder extends RecyclerView.ViewHolder {



        public NumberViewHolder(View itemView) {
            super(itemView);
        }




        void bind(int listIndex) {
                                    Glide
                                .with(getContext())
                                .load(getResources().getIdentifier("AST" + String.valueOf(listIndex), getContext().getPackageName()))
                                .into((ImageView)itemView.findViewById(R.id.Image));

                    break;
            }
        }
    }
}

0 个答案:

没有答案