在不同的ViewPagers之间切换

时间:2015-03-31 13:32:25

标签: android android-viewpager android-pageradapter

我正在尝试在不同的ViewPagers之间切换(使用例如按钮)。它基本上是一个光学比较不同版本图像的小工具。现在我只实现了一个嵌套在RelativeLayout中的单个ViewPager(需要其他东西的布局)。内容由自定义PagerAdapter填充。

有关如何在多个ViewPagers之间进行更改的任何想法?

MainActivity.java

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mViewPager.setAdapter(new SheetPagerAdapter());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

SheetPagerAdapter.java

public class SheetPagerAdapter extends PagerAdapter {
    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        SheetView sheetView = new SheetView(container.getContext(), position);
        container.addView(sheetView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        return sheetView;
    }

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

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

SheetView.java

public class SheetView extends RelativeLayout {

    private final String[] images = {
            "page01.jpg",
            "page02.jpg",
            "page03.jpg",
            "page04.jpg"
    };
    private SubsamplingScaleImageView scaleImageView;

    public SheetView(Context context, int page) {
        super(context);
        scaleImageView = new SubsamplingScaleImageView(context);
        this.addView(scaleImageView, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        scaleImageView.setImage(ImageSource.asset(images[page]));
    }
}

1 个答案:

答案 0 :(得分:0)

我最终使用FrameLayout多个ViewPagers作为子元素。 只有"活跃"布局设置为可见:

public class MainActivity extends ActionBarActivity {
    private ArrayList<ViewPager> viewPagerList = new ArrayList<>();
    private final int NUM_LAYERS = 10;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FrameLayout container = (FrameLayout) findViewById(R.id.frame_container);

        // Add ViewPagers and fill ArrayList
        for(int i = 0; i < NUM_LAYERS; i++) {
            ViewPager vp = new ViewPager(this);
            vp.setVisibility(View.INVISIBLE); // Make invisible
            viewPagerList.add(vp);
            container.addView(vp);
        }

        // Make first ViewPager visible
        viewPagerList.get(0).setVisibility(View.VISIBLE);
    }

    // Switch to other ViewPager
    public void setViewPager(int number) {
        for (ViewPager vp : viewPagerList) {
            vp.setVisibility(View.INVISIBLE);
        }
        viewPagerList.get(number).setVisibility(View.VISIBLE);
    }
}