簡體   English   中英

ViewPager ScrollView不起作用

[英]ViewPager ScrollView not working

我正在滾動視圖內實現一個viewpager,但是只要viewpager到達屏幕的底部邊緣,它就會變形,這會使scrollview無法正常工作,並且viewpager底部的元素也不會顯示。 這是我的代碼:

1. Main Acitivity

    public class MainActivity extends AppCompatActivity {

        ViewPager viewPager;
        ViewPager viewPager2;
        PagerAdapter adapter2;
        private static int currentPage = 0;
        private int[] IMAGES = {R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four};
        private ArrayList<Integer> IMAGESArray = new ArrayList<Integer>();
        private ScrollView scrollView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.landing_page);
            scrollView = (ScrollView) findViewById(R.id.scroll);
            viewPager2 = (ViewPager) findViewById(R.id.pager3);
            // Pass results to ViewPagerAdapter Class
            adapter2 = new FirstViewPagerAdapter(TestActivity.this);
            // Binds the Adapter to the ViewPager
            viewPager2.setAdapter(adapter2);
            init();
        }

    private void init() {
        for(int i=0;i<IMAGES.length;i++)
            IMAGESArray.add(IMAGES[i]);
            viewPager = (ViewPager) findViewById(R.id.pager);
            viewPager.setAdapter(new SecondViewPagerAdapter(TestActivity.this, IMAGESArray));
            CircleIndicator indicator = (CircleIndicator) findViewById(R.id.indicator);
            indicator.setViewPager(viewPager);
            // Auto start of viewpager
            final Handler handler = new Handler();
            final Runnable Update = new Runnable() {
                public void run() {
                    if (currentPage == IMAGES.length) {
                        currentPage = 0;
                    }
                    viewPager.setCurrentItem(currentPage++, true);
                }
            };
            Timer swipeTimer = new Timer();
            swipeTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    handler.post(Update);
                }
            }, 2500, 2500);
        }
}


2. The first view pager adapter

public class FirstViewPagerAdapter extends PagerAdapter {

    private Context mContext;
    private ViewPager pager=null;
    private int images [] = {R.drawable.whiskey, R.drawable.vodka, R.drawable.whiskey, R.drawable.vodka, R.drawable.whiskey};
    public FirstViewPagerAdapter(Context context) {
        mContext = context;
    }

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

        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = inflater.inflate(R.layout.page, container, false);
        ImageView image1 = (ImageView)itemView.findViewById(R.id.image);
        image1.setImageResource(images[position]);

        ((ViewPager) container).addView(itemView);
        return itemView;
    }

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

    @Override
    public int getCount() {
        return(5);
    }


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


3. The second view pager adapter

public class SecondViewPagerAdapter extends PagerAdapter {

    private ArrayList<Integer> images;
    private LayoutInflater inflater;
    private Context context;

    public SecondViewPagerAdapter(Context context, ArrayList<Integer> images) {
        this.context = context;
        this.images=images;
        inflater = LayoutInflater.from(context);
    }

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

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

    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        View myImageLayout = inflater.inflate(R.layout.view_pager_item, view, false);
        ImageView myImage = (ImageView) myImageLayout.findViewById(R.id.flag);
        myImage.setImageResource(images.get(position));
        view.addView(myImageLayout, 0);
        return myImageLayout;
    }

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


4. landing_page xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="match_parent"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:fillViewport="true"
android:background="#cccccc"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:vpi="http://schemas.android.com/tools"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:background="#cccccc"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true" />
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="200dp"
android:layout_gravity="center"
android:gravity="center"
app:ci_drawable="@drawable/myindicator"
vpi:snap="true"/>
</RelativeLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:src="@drawable/hdspiritsz"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginLeft="7dp"/>
<ImageView android:layout_width="match_parent"
android:src="@drawable/hdextrasz"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_marginRight="7dp"
android:layout_marginLeft="4dp"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView android:layout_width="match_parent"
android:src="@drawable/hdspiritsz"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginLeft="7dp"/>
<ImageView android:layout_width="match_parent"
android:src="@drawable/hdextrasz"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_marginRight="7dp"
android:layout_marginLeft="4dp"/>
</LinearLayout>
<RelativeLayout android:layout_width="match_parent"
android:background="#cccccc"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/pager3"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView android:layout_width="match_parent"
android:src="@drawable/hdspiritsz"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginLeft="7dp"/>
<ImageView android:layout_width="match_parent"
android:src="@drawable/hdextrasz"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_marginRight="7dp"
android:layout_marginLeft="4dp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>

將android:fillViewport =“ true”添加到ScrollView元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM