带有自定义 PageAdapter 的 ViewPager,如何使用 XML 文件而不是在 java 中定义布局

ViewPager with Custom PageAdapter, how to use XML file instead of defining layout in java

我正在编写一个四重奏游戏,因此我需要一个包含每张卡片所有属性的图库。 我找到了一个带有自定义 PagerAdapter 的 ViewPager 示例: http://android-er.blogspot.de/2014/04/example-of-viewpager-with-custom.html

对于简单的画廊视图来说,这非常好,但我想包含一个 XML 文件(例如我的游戏活动),而不是明确定义所有布局参数。谢谢你的想法!

这是我的适配器类:

private class MyPagerAdapter extends PagerAdapter {

        int numberOfPages = cards.size();

        int[] backgroundcolor = {
                0xFF101010};

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

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

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

            //select Card on current position from List of cards
            final Card tempCard = cards.get(position);

            //set TextView with deck title
            TextView textView = new TextView(DeckViewActivity.this);
            textView.setTextColor(Color.WHITE);
            textView.setTextSize(30);
            textView.setTypeface(Typeface.DEFAULT_BOLD);
            textView.setText(tempCard.getTitle() + " (" + String.valueOf(position + 1) + "/" + cards.size() + ")");


            //set ImageView with deckcover
            ImageView imageView = new ImageView(DeckViewActivity.this);
            ViewGroup.LayoutParams imageParams = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(imageParams);

            int finalHeight = viewPager.getMeasuredHeight();
            int finalWidth = viewPager.getMeasuredWidth();

            //Load Bitmap from assets
            InputStream inputStream = null;

            try {
                inputStream = getAssets().open(tempCard.getPicture());
                imageView.setImageBitmap(decodeSampledBitmapFromResource(inputStream, finalWidth, finalHeight));
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            //imageView.setImageResource(res[position]);


            LinearLayout layout = new LinearLayout(DeckViewActivity.this);
            layout.setOrientation(LinearLayout.VERTICAL);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            layout.setBackgroundColor(backgroundcolor[0]);
            layout.setLayoutParams(layoutParams);
            layout.addView(textView);
            layout.addView(imageView);

            final int page = position;
            layout.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {

                }});

            container.addView(layout);
            return layout;
        }

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

    }

您的 instantiateItem() 方法可以使用 LayoutInflater(通过 getLayoutInflater() 从您的 activity 获得)来扩充布局,而不是在 Java.

或者,如果您将布局包裹在片段中,则可以使用 FragmentPagerAdapter