将下一个和上一个按钮添加到ViewPager

时间:2016-03-31 04:56:34

标签: android android-viewpager slider viewpage

我想用Android Studio制作图像滑块。 我发现了一个新的滑块代码,使用ViewPager构建,我尝试了它并且工作了.. 但我想为下一个和上一个添加2个按钮..

单击按钮时,必须转到下一张图片或上一张图片。

这里是mainactivity

package org.trial.slider;   
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImageAdapter adapter = new ImageAdapter(this);
        viewPager.setAdapter(adapter);  
     }          
}

ImageAdapter.java档案,

    package org.trial.slider;

    import android.content.Context;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageView;

    /**
     * Created by ismail on 30.03.16.
     */
    public class ImageAdapter extends PagerAdapter {
        Context context;


        private int[] GalImages = new int[] {
                R.drawable.one,
                R.drawable.two,
                R.drawable.three
        };
        ImageAdapter(Context context){
            this.context=context;
        }
        @Override
        public int getCount() {
            return GalImages.length;
        }

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(context);
            int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
            imageView.setPadding(padding, padding, padding, padding);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(GalImages[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

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

我有2个XML文件; fragment_pagerswipe_fragment

3 个答案:

答案 0 :(得分:2)

方法名称viewPager.setCurrentItem(position) 此方法将整数值作为索引并使用此方法在viewPager对象上设置,如果是pager项,则可以更改位置。

答案 1 :(得分:1)

  1. 添加按钮MainActivityactivity_main.xml
  2. button.setonclicklister(this);
  3. 中添加代码OnCreate()
  4. 此处的示例代码:

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            //prev button
            case R.id.btn_list_detail_img_prev:
                viewPager.setCurrentItem(mPager.getCurrentItem()-1,true);
                break;
            //nextbutton, 1st argument : pageindex, 2nd argument : Enable smooth 
            case R.id.btn_list_detail_img_next:
                viewPager.setCurrentItem(mPager.getCurrentItem()+1,true);
                break;
    }
    

答案 2 :(得分:0)

class MyClass extends Fragment implements ViewPager.OnPageChangeListener {

    private int selectedViewPager = 0;

    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.backward:
                if (selectedViewPager > 0) {
                    viewPager.setCurrentItem(selectedViewPager - 1, true);
                }
                break;

            case R.id.fwd:
                if (viewPager.getAdapter().getCount() > selectedViewPager) {
                    viewPager.setCurrentItem(selectedViewPager + 1, true);
                }
                break;
        }
    }


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

    }

    @Override
    public void onPageSelected(int position) {
        selectedViewPager = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}