android – 在RecyclerView里面的ViewPager作为行项目

android – 在RecyclerView里面的ViewPager作为行项目,第1张

概述我需要在RecyclerView中有一个ViewPager(类似于水平图库),它垂直显示一个列表. RecyclerView的每一行都有一个ViewPager,允许在某些图像之间滑动. ViewPager还将支持将传播到父RecyclerView的点击事件. 目前,我有以下实现: 列表适配器: @Overridepublic void onBindViewHolder(MyHolder hold 我需要在RecyclerVIEw中有一个VIEwPager(类似于水平图库),它垂直显示一个列表. RecyclerVIEw的每一行都有一个VIEwPager,允许在某些图像之间滑动. VIEwPager还将支持将传播到父RecyclerVIEw的点击事件.

目前,我有以下实现:

列表适配器:

@OverrIDepublic voID onBindVIEwHolder(MyHolder holder,int position) {    super.onBindVIEwHolder(holder,position);    Item ListItem = get(position);    ...    galleryAdapter adapter =                    new galleryAdapter(getActivity().getSupportFragmentManager(),item.mediagallery);    holder.imagegallery.setAdapter(adapter);    ...}

图库适配器:

public class galleryAdapter extends FragmentStatePagerAdapter {    private final List<Item.gallery> mItems;    @Bind(R.ID.gallery_item)    ImageVIEw galleryVIEw;    public SearchResultsgalleryPagerAdapter(FragmentManager fm,@NonNull ArrayList<Item.gallery> mediagallery) {        super(fm);        mItems = mediagallery;    }    @OverrIDe    public Fragment getItem(int position) {        galleryFragment fragment = galleryFragment.newInstance(mItems.get(position));        ...        return fragment;    }    @OverrIDe    public int getCount() {        return null == mItems ? 0 : mItems.size();    }    @OverrIDe    public int getItemposition(Object object) {        //return super.getItemposition(object);        return PagerAdapter.position_NONE;    }}

图库片段:

public class galleryFragment extends Fragment {    private static final String galLERY_ITEM_BUNDLE_KEY = "gallery_item_bundle_key";    @Bind(R.ID.gallery_item)    ImageVIEw mgalleryVIEw;    private Item.gallery mgalleryItem;    // Empty constructor,required as per Fragment docs    public galleryFragment() {}    public static SearchResultsgalleryFragment newInstance(Item.gallery galleryItem) {        galleryFragment fragment = new galleryFragment();        // Add the item in the bundle which will be set to the fragment        Bundle bundle = new Bundle();        bundle.putSerializable(galLERY_ITEM_BUNDLE_KEY,galleryItem);        fragment.setArguments(bundle);        return fragment;    }    @OverrIDe    public voID onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mgalleryItem = (Item.gallery) getArguments().getSerializable(galLERY_ITEM_BUNDLE_KEY);    }    @Nullable    @OverrIDe    public VIEw onCreateVIEw(LayoutInflater inflater,@Nullable VIEwGroup container,@Nullable Bundle savedInstanceState) {        VIEw vIEw = inflater.inflate(R.layout.fragment_gallery_item,container,false);        ButterKnife.bind(this,vIEw);        displaygalleryItem();        return vIEw;    }    private voID displaygalleryItem() {        if (null != mgalleryItem) {            GlIDe.with(getContext()) // Bind it with the context of the actual vIEw used                 .load(mgalleryItem.getimageUrl()) // Load the image                 .centerCrop() // scale type                 .placeholder(R.drawable.default_product_400_land) // temporary holder displayed while the image loads                 .crossFade()                 .into(mgalleryVIEw);        }    }}

我遇到的问题是VIEwPager的片段没有正确创建和显示.有时它们出现在手动滚动之后(但并非总是如此),在大多数情况下它们根本不出现.

有没有人对我实施的错误有所了解?

解决方法 我已经设法通过直接使用PagerAdapter解决了这个问题.
import androID.content.Context;import androID.support.annotation.NonNull;import androID.support.v4.vIEw.PagerAdapter;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ImageVIEw;import com.bumptech.glIDe.GlIDe;import com.bumptech.glIDe.load.DecodeFormat;import com.peoplepost.androID.R;import com.peoplepost.androID.common.Listener.ItemClickSupport;import com.peoplepost.androID.network.merv.model.Product;import java.util.ArrayList;import java.util.List;/** * <p> * Custom pager adapter which will manually create the pages needed for showing an slIDe pages gallery. * </p> * Created by Ionut Negru on 13/06/16. */public class galleryAdapter extends PagerAdapter {    private static final String TAG = "galleryAdapter";    private final List<Item> mItems;    private final LayoutInflater mLayoutInflater;    /**     * The click event Listener which will propagate click events to the parent or any other Listener set     */    private ItemClickSupport.SimpleOnItemClickListener mOnItemClickListener;    /**     * Constructor for gallery adapter which will create and screen slIDe of images.     *     * @param context     *         The context which will be used to inflate the layout for each page.     * @param mediagallery     *         The List of items which need to be displayed as screen slIDe.     */    public galleryAdapter(@NonNull Context context,@NonNull ArrayList<Item> mediagallery) {        super();        // Inflater which will be used for creating all the necessary pages        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        // The items which will be displayed.        mItems = mediagallery;    }    @OverrIDe    public int getCount() {        // Just to be safe,check also if we have an valID List of items - never return invalID size.        return null == mItems ? 0 : mItems.size();    }    @OverrIDe    public boolean isVIEwFromObject(VIEw vIEw,Object object) {        // The object returned by instantiateItem() is a key/IDentifIEr. This method checks whether        // the VIEw passed to it (representing the page) is associated with that key or not.        // It is required by a PagerAdapter to function properly.        return vIEw == object;    }    @OverrIDe    public Object instantiateItem(VIEwGroup container,final int position) {        // This method should create the page for the given position passed to it as an argument.        // In our case,we inflate() our layout resource to create the hIErarchy of vIEw objects and then        // set resource for the ImageVIEw in it.        // Finally,the inflated vIEw is added to the container (which should be the VIEwPager) and return it as well.        // inflate our layout resource        VIEw itemVIEw = mLayoutInflater.inflate(R.layout.fragment_gallery_item,false);        // display the resource on the vIEw        displaygalleryItem((ImageVIEw) itemVIEw.findVIEwByID(R.ID.gallery_item),mItems.get(position));        // Add our inflated vIEw to the container        container.addVIEw(itemVIEw);        // Detect the click events and pass them to any Listeners        itemVIEw.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                if (null != mOnItemClickListener) {                    mOnItemClickListener.onItemClicked(position);                }            }        });        // Return our vIEw        return itemVIEw;    }    @OverrIDe    public voID destroyItem(VIEwGroup container,int position,Object object) {        // Removes the page from the container for the given position. We simply removed object using removeVIEw()        // but Could’ve also used removeVIEwAt() by passing it the position.        try {            // Remove the vIEw from the container            container.removeVIEw((VIEw) object);            // Try to clear resources used for displaying this vIEw            GlIDe.clear(((VIEw) object).findVIEwByID(R.ID.gallery_item));            // Remove any resources used by this vIEw            unbindDrawables((VIEw) object);            // InvalIDate the object            object = null;        } catch (Exception e) {            Log.w(TAG,"destroyItem: Failed to destroy item and clear it's used resources",e);        }    }    /**     * Recursively unbind any resources from the provIDed vIEw. This method will clear the resources of all the     * children of the vIEw before invalIDating the provIDed vIEw itself.     *     * @param vIEw     *         The vIEw for which to unbind resource.     */    protected voID unbindDrawables(VIEw vIEw) {        if (vIEw.getBackground() != null) {            vIEw.getBackground().setCallback(null);        }        if (vIEw instanceof VIEwGroup) {            for (int i = 0; i < ((VIEwGroup) vIEw).getChildCount(); i++) {                unbindDrawables(((VIEwGroup) vIEw).getChildAt(i));            }            ((VIEwGroup) vIEw).removeAllVIEws();        }    }    /**     * Set an Listener which will notify of any click events that are detected on the pages of the vIEw pager.     *     * @param onItemClickListener     *         The Listener. If {@code null} it will disable any events from being sent.     */    public voID setonItemClickListener(ItemClickSupport.SimpleOnItemClickListener onItemClickListener) {        mOnItemClickListener = onItemClickListener;    }    /**     * display the gallery image into the image vIEw provIDed.     *     * @param galleryVIEw     *         The vIEw which will display the image.     * @param galleryItem     *         The item from which to get the image.     */    private voID displaygalleryItem(ImageVIEw galleryVIEw,Item galleryItem) {        if (null != galleryItem) {            GlIDe.with(galleryVIEw.getContext()) // Bind it with the context of the actual vIEw used                 .load(galleryItem.getimageUrl()) // Load the image                 .asBitmap() // All our images are static,we want to display them as bitmaps                 .format(DecodeFormat.PREFER_RGB_565) // the decode format - this will not use Alpha at all                 .centerCrop() // scale type                 .placeholder(R.drawable.default_product_400_land) // temporary holder displayed while the image loads                 .animate(R.anim.fade_in) // need to manually set the animation as bitmap cannot use cross fade                 .thumbnail(0.2f) // make use of the thumbnail which can display a down-sized version of the image                 .into(galleryVIEw); // Voilla - the target vIEw        }    }}

并且更新了父RecyclerVIEw的onBindVIEwHolder():

@OverrIDepublic voID onBindVIEwHolder(MyHolder holder,position);    Item ListItem = get(position);    ...    galleryAdapter adapter =                    new galleryAdapter(getActivity(),product.mediagallery);    // Set the custom click Listener on the adapter directly    adapter.setonItemClickListener(new ItemClickSupport.SimpleOnItemClickListener() {        @OverrIDe        public voID onItemClicked(int position) {            // inner vIEw pager page was clicked        }    });    // Set the adapter on the vIEw pager    holder.imagegallery.setAdapter(adapter);    ...}

我注意到内存使用量略有增加,但UI非常流畅.我想可以进一步优化保存多少页面以及如何销毁和恢复它们.

我希望这能帮助处于类似情况的其他人.

总结

以上是内存溢出为你收集整理的android – 在RecyclerView里面的ViewPager作为行项目全部内容,希望文章能够帮你解决android – 在RecyclerView里面的ViewPager作为行项目所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1137353.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存