如何通过 ViewPager 显示其他内容?

How to show another content over ViewPager?

我有以下问题:

我有 MainActivity,其中放置了 ViewPagerViewPager的页面是Fragment,包含imageview和几个textviews。当用户点击图像时,应该显示另一个内容。可能我不明白我该怎么做。据我所知,我不能将 FrameLayout 放在我的页面 Fragment 上,这里显示另一个包含所需内容的片段。我也不能将这个片段放在 MainActivity 上,因为我不能从我的片段中更改它(如果我想用需要的内容更改我的片段上的 ViewPager,我无法捕捉到对 ImageView 的点击)。我找到了一个解决方案:在 Page Fragment 上有 2 个布局(第一个用于 ViewPager,第二个用于内容,将在后面显示),并且每次都更改它们的可见性,但我认为这不是最好的主意。那么,也许您有更好的变体?

UPD

我需要这个

点击图片后得到:

你可以使用 VeiwSwitcher 你可以了解它 here

public class ViewSwitcher extends ViewAnimator that switches between two views, and has a factory from which these views are created. You can either use the factory to create the views, or add them yourself. A ViewSwitcher can only have two child views, of which only one is shown at a time.

例如:

<ViewSwitcher
    android:id="@+id/viewSwitcher1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:inAnimation="@android:anim/slide_in_left" >

    <LinearLayout
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    //Content of first layout 

    </LinearLayout>


<LinearLayout
    android:id="@+id/view2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
   //Content of second layout

    </LinearLayout>
</ViewSwitcher>

所以基本上你有两个 ViewGroup,第一个对用户可见,第二个隐藏。你只能在 ViewSwitcher 旁边有两个 View 并且你可以在它们之间切换 Button :

viewSwitcher =   (ViewSwitcher)findViewById(R.id.viewSwitcher1);
    myFirstView= findViewById(R.id.view1);
    mySecondView = findViewById(R.id.view2);
    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if (viewSwitcher.getCurrentView() != myFirstView){

                viewSwitcher.showPrevious(); 
            } else if (viewSwitcher.getCurrentView() != mySecondView){

                viewSwitcher.showNext();
            }
        }
    });

并且对于这两个视图使用哪种 viewid 并不重要。

所以只需将 ViewSwitcher 放入您的 Fragment 布局中,然后通过 xml 或以编程方式放置您的图像。并使用 Two Button one in the first layout 和 one in the second layout 在它们之间切换。希望这对你有用。你也可以使用 Dialog 但在你的情况下它会变得丑陋!