简体   繁体   中英

Show/Hide Layout onTouch in framelayout

I have a full image fragment. There is a RelativeLayout and LinearLayout in framelayout. LinearLayout contain viewpager.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearLayoutViewpager">


    <com.bogdwellers.pinchtozoom.view.ImageViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>

<RelativeLayout
    android:id="@+id/relativelayoutforbuttons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/save_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />


</RelativeLayout>

I want When user first open it it should only show image(Viewpager layout) and if user click on it that show other option like - share and save(RelativeLayout). We can find this type of feature in facebook and whatsapp.

Try this type code for visiablity ..

private void share(){
    relativeLayout.setVisibility(View.VISIBLE);
    linearLayoutViewpager.setVisibility(View.GONE);
}


 linearLayoutViewpager=findViewById(R.id.linearLayoutViewpager);
    linearLayoutViewpager.setVisibility(View.VISIBLE);
    relativeLayout=findViewById(R.id.relativelayoutforbuttons);
    relativeLayout.setVisibility(View.GONE);

it only sample code but mange according your needs.

     final boolean flag=true;
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (flag){
                relativeLayout.setVisibility(View.VISIBLE);
                flag=false;
            }
            else{
                relativeLayout.setVisibility(View.GONE);
            }
        }
    });

You can simply hide or unhide the relativeLayout's instance using setVisibility.(..) method on linearLayout or imageviewpager click.

layout:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/framelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearLayoutViewpager">


        <ImageViewPager
            android:id="@+id/viewpager"
            android:onClick="onPagerClick"
            android:layout_width="200dp"
            android:layout_height="200dp" />


    </LinearLayout>

    <RelativeLayout
        android:id="@+id/relative_layout"
        android:visibility="gone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/save_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onSave"
        android:text="Save" />


</RelativeLayout>

</FrameLayout>

Activity:

   public class TestActivity extends AppCompatActivity {

    private boolean isButtonShown = false;
    ImageViewPager viewPager;
    RelativeLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        viewPager = findViewById(R.id.viewpager);

        layout = findViewById(R.id.relative_layout);




    }


    public void onPagerClick(View view) {

        if (isButtonShown) {
            isButtonShown = false;
            layout.setVisibility(View.GONE);
        } else {
            isButtonShown = true;
            layout.setVisibility(View.VISIBLE);
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM