Sergei Mikhailovskii
Sergei Mikhailovskii

Reputation: 2465

How to show another content over ViewPager?

I have the following problem:

I have MainActivity, where ViewPager is placed. Page of ViewPager is Fragment, which contains imageview and several textviews. When user clicks on the image, another content should be shown. And probably I don't understand how can I do it. As I know, I can't place FrameLayout on my Page Fragment and here show another fragment with needable content. Also I cannot place this fragment on MainActivity, as I cannot change it from my fragment (and if I want to change ViewPager on my Fragment with needable content, I cannot catch clicks on ImageView). I found one solution: to have 2 layouts on Page Fragment (first for ViewPager and second for content, which will be shown after), and change their visibility each time, but I don't think that this is the best idea. So, maybe you have better variants?

UPD

I need form this

enter image description here

after clicking on image get this:

enter image description here

Upvotes: 0

Views: 46

Answers (1)

Alireza Sharifi
Alireza Sharifi

Reputation: 1162

you can use VeiwSwitcher wich you can learn about it 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.

for example :

<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>

so basically you have Two ViewGroup and first one is visible to user and second one is hidden . and you can only have Two View in side of ViewSwitcher and you can switch between them with a 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();
            }
        }
    });

and it dos not matter what kind of view or id used for those Two views.

so just put ViewSwitcher in your Fragment layout and put your image through xml or programmatically . and use Two Button one in the first layout and one in the second layout to switch between them. hope this works for you. you can also use Dialog but in your case it will make it ugly!

Upvotes: 1

Related Questions