项目在ViewPager中不可见

时间:2018-02-19 10:43:15

标签: android android-viewpager

我有一个包含viewPager的屏幕。 viewPager中的项目包含一个videoView持有者,其中包含该视频的缩略图。当用户点击该缩略图时,它将在另一个屏幕中打开视频。我的问题是我实现了viewPager,但它没有显示具有单独布局的项目。它显示了指示器和它的滑动,但我看不到该项目。

这是我的ViewHolder:

public class PaidProgramVideoViewHolder extends RecyclerView.ViewHolder implements ViewPager.OnPageChangeListener {

    private static final int INDICATOR_MARGIN = 5;
    ViewPager mProScreenpager;
    LinearLayout mpageIndicator;
    VideoScreenCarousal mAdapter;
    Context context;
    private ImageView[] pageIndicatorItems;

    public PaidProgramVideoViewHolder(View itemView,Context context) {
        super(itemView);
        this.context = context;
        setupUI(itemView, context);
        setPager(itemView);
        setScreenAdapter();
        setPagerChangeListener();

    }

    private void setPagerChangeListener() {
        mProScreenpager.addOnPageChangeListener(this);
    }


    private void setScreenAdapter() {
        int[] pro_screens_image = {R.drawable.anoop_core, R.drawable.anoop_core};
        mAdapter = new PaidProgramVideoViewHolder.VideoScreenCarousal(pro_screens_image, context);
        setupPageIndicator(pro_screens_image.length);
        mProScreenpager.setAdapter(mAdapter);
    }

    //Setting page indicator dots with trainer list size and setting first indicator active
    private void setupPageIndicator(int size) {
        mpageIndicator.removeAllViews();

        pageIndicatorItems = new ImageView[size];

        for (int i = 0; i < size; i++) {

            pageIndicatorItems[i] = new ImageView(context);

            pageIndicatorItems[i].setImageDrawable(ContextCompat.getDrawable(context, R.drawable.dot_inactive));

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );
            params.setMargins(INDICATOR_MARGIN, 0, INDICATOR_MARGIN, 0);
            mpageIndicator.addView(pageIndicatorItems[i], params);
            setActiveIndicator(0);

        }
    }

    private void setActiveIndicator(int position) {

        pageIndicatorItems[position].setImageDrawable(ContextCompat.getDrawable(context, R.drawable.dot_active));

    }

    private void resetIndicators() {

        for (int i = 0; i < 2; i++) {

            pageIndicatorItems[i].setImageDrawable(ContextCompat.getDrawable(context, R.drawable.dot_inactive));

        }

    }

    private void setupUI(View itemView, Context context) {
        mpageIndicator = (LinearLayout) itemView.findViewById(R.id.carouselPageIndicator);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        resetIndicators();

        setActiveIndicator(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void setPager(View view) {
        PagerContainer container = (PagerContainer) view.findViewById(R.id.pager_container_video);
        mProScreenpager = container.getViewPager();
        mProScreenpager.setClipChildren(false);
        //
        mProScreenpager.setOffscreenPageLimit(15);

        // Disable clip to padding
        mProScreenpager.setClipToPadding(false);
        // set padding manually, the more you set the padding the more you see of prev & next page
        mProScreenpager.setPadding(15, 0, 55, 0);
        // sets a margin b/w individual pages to ensure that there is a gap b/w them
        mProScreenpager.setPageMargin(20);
        mProScreenpager.setPageTransformer(false, new ViewPager.PageTransformer() {
            private static final float MIN_SCALE = 0.80f;

            @Override
            public void transformPage(View view, float position) {

                final float normalizedposition = Math.abs(Math.abs(position) - 1);
                view.setScaleY(Math.max(MIN_SCALE, normalizedposition / 2 + 0.5f));
                view.setPivotY((float) view.getHeight());
            }
        });
        Log.d("###", "pager1 width:" + 150 * context.getResources().getDisplayMetrics().density);
    }

    private class VideoScreenCarousal extends PagerAdapter{

        int[] mScreenImages;
        Context context;

        public VideoScreenCarousal(int[] objectToReturn, Context context){
            this.mScreenImages=objectToReturn;
            this.context= context;

        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            View view = LayoutInflater.from(context).inflate(R.layout.video_container_pro_screen, null);
            final ImageView imageView = (ImageView) view.findViewById(R.id.thumbnailView);
            container.addView(view);
            imageView.setImageResource(mScreenImages[position]);

            return view;

        }

        @Override
            public int getCount() {
            return mScreenImages.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return false;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
           container.removeView((View)object);
        }
    }
}

这是我的列表项布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/black">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/thumbnailView"
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:layout_gravity="center"
            android:scaleType="centerCrop"/>


        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/youtubePlayBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@null"
            android:gravity="center_vertical"
            app:srcCompat="@drawable/icon_play_white"/>
    </FrameLayout>


</android.support.constraint.ConstraintLayout>

谁能告诉我我做错了什么?

0 个答案:

没有答案